# PatternsAndUseCases.textile

Path: | docs/PatternsAndUseCases.textile
Last Update: | Wed Sep 26 08:26:23 +0000 2012

# @title Ruby AMQP gem: Patterns and Use Cases

h1. Patterns and Use Cases

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":www.eaipatterns.com.

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

h2. Covered versions

This guide covers "Ruby amqp gem":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="gist.github.com/1207763.js"> </script>

h4. Server code

<script src="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="gist.github.com/1207758.js"> </script>

h4. Consumer (Recipient)

<script src="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="gist.github.com/1207750.js"> </script>

h4. Consumer (Handler)

<script src="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":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":twitter.com/michaelklishin and edited by "Chris Duncan":twitter.com/celldee

h2. Tell us what you think!

Please take a moment and tell us what you think about this guide "on Twitter":twitter.com/rubyamqp or "Ruby AMQP mailing list":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":michael@novemberain.com?subject=amqp%20gem%20documentation

[Validate]