File: PatternsAndUseCases.textile

package info (click to toggle)
ruby-amqp 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,508 kB
  • sloc: ruby: 8,272; sh: 11; makefile: 10
file content (346 lines) | stat: -rw-r--r-- 10,509 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# @title Ruby AMQP gem: Patterns and Use Cases

h1. Patterns and Use Cases

h2. This Documentation Has Moved to rubyamqp.info

amqp gem documentation guides are now hosted on "rubyamqp.info":http://rubyamqp.info.


h2. About this guide

This guide explains typical messaging patterns and use cases. It only covers the most common scenarios. For comprehensive list
of messaging patterns, consult books on this subject, for example, "Enterprise Integration Patterns":http://www.eaipatterns.com.

This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a> (including images & stylesheets). The source is available "on Github":https://github.com/ruby-amqp/amqp/tree/master/docs.


h2. Covered versions

This guide covers "Ruby amqp gem":http://github.com/ruby-amqp/amqp v0.8.0 and later.



h2. Introduction

Messaging patterns are a lot like object-oriented design patterns: they are generalized reusable solutions to specific problems. They are not
recipes, however, and their exact implementation may and will vary from application to application. Just like OO design patterns,
they can classified:

 * Message construction patterns describe form, content and purpose of messages.
 * Message routing patterns outline how messages can be directed from producers to consumers.
 * Message transformation patterns change message content or metadata.

There are other, more specialized group of messaging patterns that are out of scope of this guide.

This guide demonstrates implementation of several common routing patterns plus explains how built-in AMQP 0.9.1 features
can be used to implement message construction and message transformation patterns.

Note that guide is a work in progress. There are many messaging patterns and new variations are being discovered every year.
This guide thus strives to be useful to the 80% of developers instead of being "complete".



h2. Request/Reply pattern

h3. Description & Use cases

Request/Reply is a simple way of integration when one application issues a request and another application responds
to it. This pattern is often referred to as"Remote Procedure Call", even when it is not entirely correct. Request/Reply
pattern is a 1:1 communication pattern.

Some examples of Request/Reply pattern are:

 * The 1st application requests a document that the 2nd application generates or loads and returns.
 * End-user application issues a search request and another application returns results back.
 * One application requests a progress report from another application.

h3. AMQP-based implementation

Implementation of Request/Reply pattern on top of AMQP 0.9.1 involves two messages: a request (Req) and a response (Res).
Client app generates a request identifier and sets :message_id attribute on Req. Client also uses a server-named
exclusive queue to receive replies and thus sets :reply_to Req attribute to the name of that queue.

Server app uses a well-known queue name to receive requests and sets :correlation_id to :message_id of the original
request message (Req) to make it possible for the client to identify what request this reply is for.

h4. Request message attributes

<dl>
  <dt>:message_id</dt>
  <dd>Unique message identifier</dd>

  <dt>:reply_to</dt>
  <dd>Queue name server should send the response to</dd>
</dl>

h4. Response message attributes

<dl>
  <dt>:correlation_id</dt>
  <dd>Identifier of the original request message (set to request's :correlation_id)</dd>

  <dt>:routing_key</dt>
  <dd>Client's replies queue name (set to request's :reply_to)</dd>
</dl>



h3. Code example

h4. Client code

<script src="https://gist.github.com/1207763.js"> </script>


h4. Server code

<script src="https://gist.github.com/1207764.js"> </script>

In the examples above messages are published with the :immediate attribute set. This is not necessary in all
cases: sometimes it is OK for requests to sit in the queue without active consumers. Replies, on the other hand,
assume an active consumer and existing replies queue, so if routing or immediate delivery do not succeed,
server application will log returned messages. More on this in the {file:docs/Exchanges.textile Working With Exchanges} guide.



h3. Related patterns

Request/Reply demonstrates two common techniques that are sometimes referred to as messaging patterns of its own:

 * "Correlation Identifier":http://www.eaipatterns.com/CorrelationIdentifier.html (for identifying what request incoming response is for)
 * "Return Address":http://www.eaipatterns.com/ReturnAddress.html (for identifying where replies should be sent)

Other related patterns are

 * Scatter/Gather
 * Smart Proxy





h2. Command pattern

h3. Description & Use cases

Command pattern is very similar to Request/Reply, except that there is no reply and messages are typed. For example, most modern Web
applications have at least one "background task processor" that carries out a number of operations asynchronously,
without sending any responses back. Command pattern usually assumes 1:1 communication.

Some specific examples of Command pattern are:

 * Account termination in a Web app triggers information archiving (or deletion) that is done by a separate app "in the background".
 * After a document or profile update, a Web app sends out commands to a search indexer application.
 * Virtual machines control dashboard app sends virtual machine controller application a command to reboot.


h3. AMQP-based implementation

Implementation of Command pattern on top of AMQP 0.9.1 involves well-known durable queues. Application that issues the command
then can use default exchange to publish messages to well-known services directly. Request message :type attribute then indicates
command type and message body (or body and headers) carry additional information consumer needs to carry it out.

h4. Request message attributes

<dl>
  <dt>:type</dt>
  <dd>Message type as a string. For example: gems.install or commands.shutdown</dd>
</dl>

h3. Code example

h4. Producer (Sender)

<script src="https://gist.github.com/1207758.js"> </script>


h4. Consumer (Recipient)

<script src="https://gist.github.com/1207761.js"> </script>


h3. Related patterns

 * Event
 * Request/Reply





h2. Event pattern

h3. Description & Use cases

Event pattern is a version of the Command pattern, but with 1 or more receivers (1:N communication).
The world we live in is full of events, so applications of this pattern are endless.

Some specific use cases of Event pattern are

 * Event logging (one application asks event collector to record certain event and possibly take action)
 * Event propagation in MMO games
 * Live sport score updates
 * Various "push notifications" for mobile applications

The Event pattern is very similar to the Command pattern, however, there is typically certain differences between the two:

 * Event listeners often do not respond back to event producers
 * Event listeners are often concerned with data collection: they update counters, persist event information and so on
 * There may be more than event listener in the system. Commands are often carried out by one particular application


h3. AMQP-based implementation

Because Event pattern is a 1:N communication pattern, it typically uses a fanout exchange. Event listeners
then use server-named exclusive queues and all bind to that exchange. Event messages use :type message
attribute to indicate event type and message body (plus, possibly, message headers) to pass event
context information.

h4. Request message attributes

<dl>
  <dt>:type</dt>
  <dd>Message type as a string. For example: files.created, files.indexed or pages.viewed</dd>
</dl>

<span class="note">
Due to misconfiguration or different upgrade time/policy, applications may receive events they do not know how to handle.
It is important for developers to handle such cases, otherwise it is likely that consumers may crash.
</span>

More on fanout exchange type in the {file:docs/Exchanges.textile Working With Exchanges} guide.


h3. Code example

h4. Producer (Sender)

<script src="https://gist.github.com/1207750.js"> </script>


h4. Consumer (Handler)

<script src="https://gist.github.com/1207749.js"> </script>


h3. Related patterns

 * Command
 * Publish/Subscribe




h2. Document Message pattern

h3. Description & Use cases

Document Message pattern is very similar to Command and Event patterns. The difference is in the intent: whereas a Command message tells
the receiver to invoke certain behavior, a Document Message just passes data and lets the receiver decide what, if anything, to do with the data.

Message payload is a single logical entity, for example, one (or a group of closely related) database rows or documents.

Use cases for the Document Message pattern often have something to do with processing of documents:

 * Indexing
 * Archiving
 * Content extraction
 * Transformation (translation, transcoding and so on) of document data



h2. Competing Consumers pattern

h3. Description & Use cases

"Competing Consumers":http://www.eaipatterns.com/CompetingConsumers.html are multiple consumers that process messages from a shared queue.

TBD

h3. AMQP-based implementation

TBD

h3. Code example

TBD



h2. Publish/Subscribe pattern

h3. Description & Use cases

TBD

h3. AMQP-based implementation

TBD

h3. Code example

TBD



h2. Scatter/Gather pattern

h3. Description & Use cases

TBD

h3. AMQP-based implementation

TBD

h3. Code example

TBD



h2. Smart Proxy pattern

h3. Description & Use cases

TBD

h3. AMQP-based implementation

TBD

h3. Code example

TBD




h2. Multistep Processing (Routing Slip) pattern

h3. Description & Use cases

TBD

h3. AMQP-based implementation

TBD

h3. Code example

TBD


h2. Authors

These guides were written by "Michael Klishin":http://twitter.com/michaelklishin and edited by "Chris Duncan":https://twitter.com/celldee


h2. Tell us what you think!

Please take a moment and tell us what you think about this guide "on Twitter":http://twitter.com/rubyamqp or "Ruby AMQP mailing list":http://groups.google.com/group/ruby-amqp:
what was unclear? what wasn't covered? maybe you don't like guide style or grammar and spelling are incorrect? Readers feedback is
key to making documentation better.

If mailing list communication is not an option for you for some reason, you can "contact guides author directly":mailto:michael@novemberain.com?subject=amqp%20gem%20documentation