You are on page 1of 20

R

Previous Next

DOWNLOAD PDF

Previous Next
FEBRUARY 2014

ZeroMQ:
Previous Next

The Design
Previous Next

of Messaging
Download

Middleware
A look at how one of the most popular messaging
Subscribe

layers was designed and implemented. >>

Powered by

PLUS: 10 Things Tech Writers Want Developers to Know >>


Register
Previous Next

DOWNLOAD PDF

Previous Next
ZeroMQ: The Design of Messaging Middleware
Previous Next
Building a large-scale distributed system in a systematic manner
By Martin Sstrik
Previous Next

Download

Subscribe MQ is a messaging system, or


message-oriented middleware if
you will. It is used in environments
as diverse as financial services,
game development, embedded
systems, academic research, and aerospace.
Messaging systems work basically as instant
messaging for applications. An application
decides to communicate an event to another
application (or multiple applications), it as-
MQ was originally conceived as an ultra-fast
messaging system for stock trading and so the
focus was on extreme optimization. The first
year of the project was spent devising bench-
marking methodology and trying to define an
architecture that was as efficient as possible.
Later on, approximately in the second year
of development, the focus shifted to provid-
ing a generic system for building distributed
applications and supporting arbitrary mes-
architecture of MQ, and provide some tips
for those who are struggling with the same
problems.
Since its third year, MQ has outgrown its
codebase; there is an initiative to standard-
ize the wire protocols it uses, and an experi-
mental implementation of a MQ-like mes-
saging system inside the Linux kernel, etc.
These topics are not covered here. However,
you can check online resources for further
sembles the data to be sent, hits the send saging patterns, various transport mecha- details (http://250bpm.com/concepts and
Education And Networking
button, and the messaging system takes care nisms, arbitrary language bindings, etc. http://is.gd/giDEEh).
Learn how cloud computing,
software-defined networking,
of the rest. Unlike instant messaging, though, During the third year, the focus was mainly
virtualization, wireless, and other messaging systems have no GUI and assume on improving usability and flattening the Application vs. Library
key technologies work together no human beings at the endpoints capable learning curve. We adopted the BSD Sockets MQ is a library, not a messaging server. It
to drive business at Interop Las
Vegas. It happens March 31 of intelligent intervention when something API, tried to clean up the semantics of indi- took us several years working on the AMQP
to April 4. goes wrong. Messaging systems thus have to vidual messaging patterns, and so on. protocol, which was a financial industry at-
be both fault-tolerant and much faster than This article will give insight into how the tempt to standardize the wire protocol for
Register common instant messaging. three goals above translated into the internal business messaging writing a reference
Previous Next

www.drdobbs.com February 2014 2


Previous Next
Register
Previous Next [ZEROMQ]

implementation for it and participating in make the situation better. To solve this prob- One of the unintended consequences was
Previous Next
several large-scale projects heavily based lem, we need a fully distributed architecture, that opting for the library model improved
on messaging technology to realize that an architecture where every component can the usability of the product. Over and over
Previous Next
theres something wrong with the classic cli- be possibly governed by a different business again users express their happiness about
ent/server model of a smart messaging server entity. Given that the unit of management the fact that they dont have to install and
(broker) and dumb messaging clients. in server-based architecture is the server, we manage a stand-alone messaging server. It
Previous Next
Our primary concern was with the perfor- can solve the problem by installing a separate turns out that not having a server is a pre-
mance: If theres a server in the middle, each server for each component. In such a case we ferred option as it cuts operational cost (no
message has to pass the network twice (from can further optimize the design by making need to have a messaging server admin)
Download the sender to the broker and from the broker the server and the component share the same and improves time-to-market (no need to
to the receiver) inducing a penalty in terms of processes. What we end up with is a messag- negotiate the need to run the server with the
both latency and throughput. Moreover, if all ing library. client, the management or the operations
the messages are passed through the broker, MQ was started when we got an idea team).
at some point, the server is bound to become about how to make messaging work with- The lesson learned is that when starting a
Subscribe the bottleneck. out a central server. It required turning the new project, you should opt for the library
A secondary concern was related to large- whole concept of messaging upside down design if at all possible. Its pretty easy to cre-
scale deployments: when the deployment and replacing the model of an autonomous ate an application from a library by invoking
crosses organizational boundaries the con- centralized store of messages in the center it from a trivial program; however, its almost
cept of a central authority managing the of the network with a smart endpoint, dumb impossible to create a library from an existing
whole message flow doesnt apply anymore. network architecture based on the end-to- executable. A library offers much more flex-
No company is willing to cede control to a end principle. The technical consequence of ibility to the users, at the same time sparing
server in a different company due to trade se- that decision was that MQ, from the very them non-trivial administrative effort.
crets and legal liability. The result in practice beginning, was a library, not an application.
is that theres one messaging server per com- Weve been able to prove that this archi- Global State
pany, with hand-written bridges to connect tecture is both more efficient (lower latency, Global variables dont play well with libraries.
it to messaging systems in other companies. higher throughput) and more flexible (its A library may be loaded several times in the
The whole ecosystem is thus heavily frag- easy to build arbitrary complex topologies in- process but even then theres only a single
mented, and maintaining a large number of stead of being tied to classic hub-and-spoke set of global variables. Figure 1 shows a MQ
bridges for every company involved doesnt model) than standard approaches. library being used from two different and
www.drdobbs.com February 2014 3
Register
Previous Next [ZEROMQ]

application likely to break when it happens to be instanti- A B


Previous Next
ated twice in the same process.
0 sec
Previous Next IibA IibB
Performance 1 sec
When MQ was started, its primary goal
OMQ was to optimize performance. Performance 2 sec
Previous Next
of messaging systems is expressed using
two metrics: throughput how many mes- 3 sec
Figure 1: MQ being used by different libraries.
sages can be passed during a given amount
4 sec
Download independent libraries. The application then of time; and latency how long it takes for
uses both of those libraries. a message to get from one endpoint to the 5 sec
When such a situation occurs, both in- other.
stances of MQ access the same variables, Which metric should we focus on? Whats 6 sec
resulting in race conditions, strange failures the relationship between the two? Isnt it ob-
Subscribe and undefined behavior. To prevent this prob- vious? Run the test, divide the overall time of Figure 2: Sending messages from A to B.

lem, the MQ library has no global variables. the test by number of messages passed and
Instead, a user of the library is responsible for what you get is latency. Divide the number the throughput is 0.83 messages/sec (5/6)
creating the global state explicitly. The ob- of messages by time and what you get is and the latency is 1.2 sec (6/5), right?
ject containing the global state is called con- throughput. In other words, latency is the in- Have a look at the diagram again. It takes a
text. While from the users perspective con- verse value of throughput. Trivial, right? different time for each message to get from
text looks more or less like a pool of worker Instead of starting coding straight away we A to B: 2 sec, 2.5 sec, 3 sec, 3.5 sec, 4 sec. The
threads, from MQs perspective its just an spent some weeks investigating the perfor- average is 3 seconds, which is pretty far away
object to store any global state that we hap- mance metrics in detail and we found out that from our original calculation of 1.2 second.
pen to need. In the picture above, libA would the relationship between throughput and This example shows the misconceptions peo-
have its own context and libB would have its latency is much more subtle than that, and ple are intuitively inclined to make about per-
own as well. There would be no way for one of often the metrics are quite counter-intuitive. formance metrics.
them to break or subvert the other one. Imagine A sending messages to B (see Fig- Now have a look at the throughput. The
The lesson here is pretty obvious: Dont use ure 2). The overall time of the test is 6 sec- overall time of the test is 6 seconds. However,
global state in libraries. If you do, the library is onds. There are 5 messages passed. Therefore, at A it takes just 2 seconds to send all the mes-
www.drdobbs.com February 2014 4
Register
Previous Next [ZEROMQ]

sages. From As perspective the throughput is ship; however, the formula involves integrals sages we can transfer between two endpoints
Previous Next
2.5 msgs/sec (5/2). At B it takes 4 seconds to and we wont discuss it here. For more infor- during a given amount of time. Alternatively,
receive all messages. So from Bs perspective, mation, read the literature on queuing theory. we may be interested in how long it takes for a
Previous Next
the throughput is 1.25 msgs/sec (5/4). Neither There are many more pitfalls in benchmarking message to get from one endpoint to another.
of these numbers matches our original calcu- the messaging systems that we wont go fur- However, given that MQ is designed for
lation of 1.2 msgs/sec. ther into. The stress should rather be placed on scenarios with long-lived connections, the
Previous Next
To make a long story short, latency and the lesson learned: Make sure you understand time it takes to establish a connection or the
throughput are two different metrics; that the problem you are solving. Even a problem as time needed to handle a connection error is
much is obvious. The important thing is to un- simple as make it fast can take lot of work to basically irrelevant. These events happen very
Download derstand the difference between the two and understand properly. Whats more, if you dont rarely and so their impact on overall perfor-
their relationship. Latency can be measured understand the problem, you are likely to build mance is negligible.
only between two different points in the sys- implicit assumptions and popular myths into The part of a codebase that gets used very
tem; there is no such thing as latency at point your code, making the solution either flawed or frequently, over and over again, is called the
A. Each message has its own latency. You can at least much more complex or much less use- critical path; optimization should focus on the
Subscribe average the latencies of multiple messages; ful than it could possibly be. critical path.
however, theres no such thing as latency of a Lets have a look at an example: MQ is not
stream of messages. Critical Path extremely optimized with respect to memory
Throughput, on the other hand, can be We discovered during the optimization pro- allocations. For example, when manipulating
measured only at a single point of the sys- cess that three factors have a crucial impact strings, it often allocates a new string for each
tem. Theres a throughput at the sender, on performance: intermediate phase of the transformation.
theres a throughput at the receiver, theres However, if we look strictly at the critical
a throughput at any intermediate point be- 1. Number of memory allocations path the actual message passing well
tween the two, but theres no such thing as 2. Number of system calls find out that it uses almost no memory al-
overall throughput of the whole system. And 3. Concurrency model locations. If messages are small, its just one
throughput make sense only for a set of mes- memory allocation per 256 messages (these
sages; theres no such thing as throughput of However, not every memory allocation or messages are held in a single large allocated
a single message. every system call has the same effect on per- memory chunk). If, in addition, the stream
As for the relationship between throughput formance. The performance we are interested of messages is steady, without huge traffic
and latency, it turns out there really is a relation- in messaging systems is the number of mes- peaks, the number of memory allocations on
www.drdobbs.com February 2014 5
Register
Previous Next [ZEROMQ]

the critical path drops to zero (the allocated memory chunks are not
returned to the system, but reused repeatedly).
Previous Next Small Message Large Message
Lesson learned: Optimize where it makes difference. Optimizing
pieces of code that are not on the critical path is wasted effort. handle handle
Previous Next
data pointer
Allocating Memory
Assuming that all the infrastructure was initialized and a connection
Previous Next
between two endpoints has been established, theres only one thing
to allocate when sending a message: the message itself. Thus, to opti- data
mize the critical path we had to look into how messages are allocated
dynamically allocated memory
Download and passed up and down the stack.
Figure 3: Message copying (or not).
Its common knowledge in the high-performance networking
field that the best performance is achieved by carefully balancing
the cost of message allocation and the cost of message copying (for It makes sense to allocate the message once and pass a pointer
example, different handling of small, medium, and large messages; to the allocated block, instead of copying the data. This approach is
Subscribe see http://hal.inria.fr/docs/00/29/28/31/PDF/Open-MX-IOAT.pdf ). called zero-copy.
For small messages, copying is much cheaper than allocating mem- MQ handles both cases in a transparent manner. A MQ mes-
ory. It makes sense to allocate no new memory chunks at all and sage is represented by an opaque handle. The content of very small
instead to copy the message to preallocated memory whenever messages is encoded directly in the handle. So making a copy of
needed. For large messages, on the other hand, copying is much the handle actually copies the message data. When the message
more expensive than memory allocation. is larger, its allocated in a separate buffer and the handle contains

www.drdobbs.com February 2014 6


Register
Previous Next [ZEROMQ]

App Lesson learned: When thinking about per- especially if the messages are small and hun-
Previous Next
formance, dont assume theres a single best dreds of them can be packed into a single
MQ solution. batch.
Previous Next
It may happen that there are several sub- On the other hand, batching can have
TCP classes of the problem (for example, small negative impact on latency. Lets take, for
messages vs. large messages), each having its example, the well-known Nagles algorithm,
Previous Next IP
own optimal algorithm. as implemented in TCP. It delays the out-
NIC bound messages for a certain amount of
Figure 4: Sending four messages.
Batching time and merges all the accumulated data
Download It has already been mentioned that the sheer into a single packet. Obviously, the end-
number of system calls in a messaging system to-end latency of the first message in the
App can result in a performance bottleneck. Actu- packet is much worse than the latency of
ally, the problem is much more generic than the last one. Thus, its common for applica-
MQ
that. Theres a non-trivial performance pen- tions that need consistently low latency to
Subscribe TCP
alty associated with traversing the call stack switch Nagles algorithm off. Its even com-
and thus, when creating high-performance mon to switch off batching on all levels of
IP applications, its wise to avoid as much stack the stack (for example, a NICs interrupt
traversing as possible. coalescing feature). But no batching means
NIC Consider Figure 4. To send four messages, extensive traversing of the stack and results
you have to traverse the entire network stack in low message throughput. We seem to
Figure 5: Batching messages.
four times (MQ, glibc, user/kernel space be caught in a throughput versus latency
just a pointer to the buffer. Making a copy boundary, TCP implementation, IP implemen- dilemma.
of the handle doesnt result in copying the tation, Ethernet layer, the NIC itself and back MQ tries to deliver consistently low laten-
message data, which makes sense when the up the stack again). cies combined with high throughput using
message is megabytes long (Figure 3). It However, if you decide to join those mes- the following strategy: When message flow is
should be noted that in the latter case the sages into a single batch, there would be only sparse and doesnt exceed the network stacks
buffer is reference-counted so that it can be one traversal of the stack (Figure 5). The im- bandwidth, MQ turns all the batching off to
referenced by multiple handles without the pact on message throughput can be over- improve latency. The trade-off here is some-
need to copy the data. whelming: up to two orders of magnitude, what higher CPU usage we still have to
www.drdobbs.com February 2014 7
Register
Previous Next [ZEROMQ]

network
traverse the stack frequently. However, that isnt considered to be a
Previous Next
problem in most cases.
When the message rate exceeds the bandwidth of the network stack,
Previous Next
the messages have to be queued stored in memory till the stack is worker: thread 1 worker: thread 2
ready to accept them. Queuing means the latency is going to grow. If
the message spends one second in the queue, end-to-end latency will be engine engine
Previous Next
at least one second. Whats even worse, as the size of the queue grows,
listener session session
latencies will increase gradually. If the size of the queue is not bound, the creates
latency can exceed any limit.
Download It has been observed that even though the network stack is tuned for
pipes
lowest possible latency (Nagles algorithm switched off, NIC interrupt
coalescing turned off, and so on) latencies can still be dismal because
of the queuing effect, as described above.
In such situations it makes sense to start batching aggressively.
Subscribe Theres nothing to lose as the latencies are already high anyway. On
the other hand, aggressive batching improves throughput and can socket
empty the queue of pending messages which in turn means the
latency will gradually drop as the queuing delay decreases. Once
there are no outstanding messages in the queue, the batching can be
API
turned off to improve the latency even further.
application thread
One additional observation is that the batching should only be done
at the topmost level. If the messages are batched there, the lower lay- Figure 6: MQ architecture.
ers have nothing to batch anyway, and so all the batching algorithms
underneath do nothing except introduce additional latency. Architecture Overview
Lesson learned: To get optimal throughput combined with optimal Up to this point I have focused on generic principles that make MQ
response time in an asynchronous system, turn off all the batching fast. Now, lets have a look at the actual architecture of the system
algorithms on the low layers of the stack and batch on the topmost (Figure 6).
level. Batch only when new data are arriving faster than they can be The user interacts with MQ using so-called sockets. They are
processed. pretty similar to TCP sockets, the main difference being that each
www.drdobbs.com February 2014 8
Register
Previous Next [ZEROMQ]

socket can handle communication with multi- The former have to do mainly with connec- queue optimized for fast passing of messages
Previous Next
ple peers, a bit like unbound UDP sockets do. tion management. For example, a TCP listener between threads.
The socket object lives in the users thread object listens for incoming TCP connections Finally, theres a context object (discussed in
Previous Next
(see the discussion of threading models in and creates an engine/session object for each the previous sections but not shown on the
the next section). Aside from that, MQ is new connection. Similarly, a TCP connector diagram) that holds the global state and is
running multiple worker threads that handle object tries to connect to the TCP peer and accessible by all the sockets and all the asyn-
Previous Next
the asynchronous part of the communication: when it succeeds it creates an engine/session chronous objects.
reading data from the network, queuing mes- object to manage the connection. When such
sages, accepting incoming connections, etc. connection fails, the connector object tries to Concurrency Model
Download There are various objects living in the re-establish it. One of the requirements for MQ is to take
worker threads. Each of these objects is The latter are objects that are handling advantage of multi-core boxes; in other
owned by exactly one parent object (owner- data transfer itself. These objects are com- words, to scale the throughput linearly with
ship is denoted by a simple full line in the posed of two parts: the session object is the number of available CPU cores.
diagram). The parent can live in a different responsible for interacting with the MQ Our previous experience with messaging
Subscribe thread than the child. Most objects are owned socket, and the engine object is respon- systems showed that using multiple threads
directly by sockets; however, there are couple sible for communication with the network. in a classic way (critical sections, semaphores,
of cases where an object is owned by an ob- Theres only one kind of the session object, etc.) doesnt yield much performance improve-
ject which is owned by the socket. What we but theres a different engine type for each ment. In fact, a multi-threaded version of a
get is a tree of objects, with one such tree per underlying protocol MQ supports. Thus, we messaging system can be slower than a sin-
socket. The tree is used during shut down; no have TCP engines, IPC (inter-process commu- gle-threaded version, even if measured on
object can shut itself down until it closes all nication) engines, PGM engines (a reliable a multi-core box. Individual threads are sim-
its children. This way we can ensure that the multicast protocol, see RFC 3208), etc. The ply spending too much time waiting for each
shutdown process works as expected; for ex- set of engines is extensible in the future other while, at the same time, eliciting a lot of
ample, that pending outbound messages are we may choose to implement, say, a Web- context switching that slows the system down.
pushed to the network prior to terminating Socket engine or an SCTP engine. Given these problems, we decided to go for a
the sending process. The sessions are exchanging messages with different model. The goal was to avoid locking
Roughly speaking, there are two kinds of the sockets. There are two directions to pass entirely and let each thread run at full speed.
asynchronous objects: objects that are not in- messages in and each direction is handled by The communication between threads was
volved in message passing and those that are. a pipe object. Each pipe is basically a lock-free to be provided via asynchronous messages
www.drdobbs.com February 2014 9
Register
Previous Next [ZEROMQ]

(events) passed between the threads. This, as insiders know, is the classic kind of cooperative multitasking. This means we needed a scheduler;
Previous Next
actor model. objects need to be event-driven rather than being in control of the
The idea was to launch one worker thread per CPU core having entire event loop. That is, we have to take care of arbitrary sequences
Previous Next
two threads sharing the same core would only mean a lot of context of events, even very rare ones and we have to make sure that no object
switching for no particular advantage. Each internal MQ object, such holds the CPU for too long; etc.
as say, a TCP engine, would be tightly bound to a particular worker In short, the whole system had to become fully asynchronous. No
Previous Next
thread. That, in turn, means that theres no need for critical sections, object can afford to do a blocking operation, because it would not
mutexes, semaphores and the like. Additionally, these MQ objects only block itself but also all the other objects sharing the same worker
wont be migrated between CPU cores so would thus avoid the nega- thread. All objects must become, whether explicitly or implicitly, state
Download tive performance impact of cache pollution (Figure 7). machines. With hundreds or thousands of state machines running in
parallel you have to take care of all the possible interactions between
worker: thread 1 worker: thread 2 them and most importantly of the shutdown process.
It turns out that shutting down a fully asynchronous system in a
clean way is a dauntingly complex task. Trying to shut down a thou-
Subscribe connection connection sand moving parts, some of them working, some idle, some in the
process of being initiated, some of them already shutting down by
connection themselves, is prone to all kinds of race conditions, resource leaks and
connection similar. The shutdown subsystem is definitely the most complex part of
listener MQ. A quick check of the bug tracker indicates that some 30--50% of
reported bugs are related to shutdown in one way or another.
connection connection
Lesson learned: When striving for extreme performance and scal-
ability, consider the actor model; its almost the only game in town in
such cases. However, if you are not using a specialized system like Er-
lang or MQ itself, youll have to write and debug a lot of infrastruc-
Figure 7: Multiple worker threads.
ture by hand. Additionally, think, from the very beginning, about
the procedure to shut down the system. Its going to be the most
This design makes a lot of traditional multi-threading problems complex part of the codebase and if you have no clear idea how to
disappear. Nevertheless, theres a need to share the worker thread implement it, you should probably reconsider using the actor model
among many objects, which in turn means there needs to be some in the first place.
www.drdobbs.com February 2014 10
Register
Previous Next [ZEROMQ]

Lock-Free Algorithms
Previous Next queue reader: thread 1
Lock-free algorithms have been in vogue lately. They are simple mech-
anisms for inter-thread communication that dont rely on the kernel-
Previous Next
provided synchronization primitives, such as mutexes or semaphores; writer thread queue reader: thread 2
rather, they do the synchronization using atomic CPU operations, such
as atomic compare-and-swap (CAS). It should be understood that they
Previous Next
are not literally lock-free instead, locking is done behind the scenes queue reader: thread 3
at the hardware level.
MQ uses a lock-free queue in pipe objects to pass messages be-
Figure 8: Queues.
Download tween the users threads and MQs worker threads. There are two
interesting aspects to how MQ uses the lock-free queue.
owned by writer thread owned by reader thread
First, each queue has exactly one writer thread and exactly one
reader thread. If theres a need for 1-to-N communication, multiple
queues are created (Figure 8). Given that this way the queue doesnt
Subscribe have to take care of synchronizing the writers (theres only one writer) pre-write pre-read
or readers (theres only one reader) it can be implemented in an extra-
efficient way. writer: flush reader: flush
Second, we realized that while lock-free algorithms were more effi-
cient than classic mutex-based algorithms, atomic CPU operations are
Figure 9: Lock-free queue.
still rather expensive (especially when theres contention between CPU
cores) and doing an atomic operation for each message written and/or
each message read was slower than we were willing to accept. portion of the queue thats accessed solely by the writer thread, and
The way to speed it up was batching once again. Imagine you had then flush it using a single atomic operation.
10 messages to be written to the queue. It can happen, for example, The same applies to reading from the queue. Imagine the 10 mes-
when you receive a network packet containing 10 small messages. sages above were already flushed to the queue. The reader thread
Receiving a packet is an atomic event; you cannot get half of it. This can extract each message from the queue using an atomic operation.
atomic event results in the need to write 10 messages to the lock-free However, its overkill; instead, it can move all the pending messages
queue. Theres not much point in doing an atomic operation for each to a pre-read portion of the queue using a single atomic operation.
message. Instead, you can accumulate the messages in a pre-write Afterwards, it can retrieve the messages from the pre-read buffer one
www.drdobbs.com February 2014 11
Register
Previous Next [ZEROMQ]

by one. Pre-read is owned and accessed solely by the reader thread increased tenfold, some 20 bindings to different languages were
Previous Next
and thus no synchronization whatsoever is needed in that phase. implemented, etc.
The arrow on the left of Figure 9 shows how the pre-write buffer can The user interface defines the perception of a product. With basically
Previous Next
be flushed to the queue simply by modifying a single pointer. The arrow no change to the functionality just by changing the API MQ
on the right shows how the whole content of the queue can be shifted changed from an enterprise messaging product to a networking
to the pre-read by doing nothing but modifying another pointer. product. In other words, the perception changed from a complex
Previous Next
Lesson learned: Lock-free algorithms are hard to invent, trouble- piece of infrastructure for big banks to hey, this helps me to send my
some to implement and almost impossible to debug. If at all possible, 10-byte-long message from application A to application B.
use an existing proven algorithm rather than inventing your own. Lesson learned: Understand what you want your project to be and de-
Download When extreme performance is required, dont rely solely on lock-free sign the user interface accordingly. Having a user interface that doesnt
algorithms. While they are fast, the performance can be significantly align with the vision of the project is a 100% guaranteed way to fail.
improved by doing smart batching on top of them. One of the important aspects of the move to the BSD Sockets API
was that it wasnt a revolutionary freshly invented API, but an existing
API and well-known one. Actually, the BSD Sockets API is one of the oldest
Subscribe The user interface is the most important part of any product. Its the APIs still in active use today; it dates back to 1983 and 4.2BSD Unix. Its
only part of your program visible to the outside world and if you get it been widely used and stable for literally decades.
wrong, the world will hate you. In end-user products its either the GUI The above fact brings a lot of advantages. Firstly, its an API that
or the command line interface. In libraries its the API. everybody knows, so the learning curve is very short. Even if youve
In early versions of MQ the API was based on AMQPs model of never heard of MQ, you can build your first application in couple of
exchanges and queues. (See the AMQP specification at http://www. minutes thanks to the fact that you are able to reuse your BSD Sockets
amqp.org/specification/1.0/amqp-org-download.) From a historical knowledge.
perspective its interesting to have a look at the white paper from Additionally, using a widely implemented API enables integration of
2007 (http://zeromq.org/whitepapers:messaging-enabled-network) MQ with existing technologies. For example, exposing MQ objects
that tries to reconcile AMQP with a brokerless model of messaging. as sockets or file descriptors allows for processing TCP, UDP, pipe,
I spent the end of 2009 rewriting it almost from scratch to use the file and MQ events in the same event loop. Another example: the ex-
BSD Socket API instead. That was the turning point; MQ adoption perimental project to bring MQ-like functionality to the Linux kernel
soared from that point on. While before it was a niche product used (https://github.com/250bpm/linux-2.6 ) turned out to be pretty simple
by a bunch of messaging experts, afterwards it became a handy com- to implement. By sharing the same conceptual framework it can re-use
monplace tool for anybody. In a year or so, the size of the community a lot of infrastructure already in place.
www.drdobbs.com February 2014 12
Register
Previous Next [ZEROMQ]

Most importantly, the fact that the BSD Sockets API has survived One approach is to adopt the UNIX philosophy of do one thing and
Previous Next
almost three decades despite numerous attempts to replace it means do it well. What this means is that the problem domain should be ar-
that there is something inherently right in the design. BSD Sockets tificially restricted to a small and well-understood area. The program
Previous Next
API designers have whether deliberately or by chance made the should then solve this restricted problem in a correct and exhaustive
right design decisions. By adopting the API we can automatically share way. An example of such approach in the messaging area is MQTT
those design decisions without even knowing what they were and (http://mqtt.org). Its a protocol for distributing messages to a set of
Previous Next
what problem they were solving. consumers. It cant be used for anything else (say for RPC) but it is easy
to use and does message distribution well.
The other approach is to focus on generality and provide a powerful
Dont succumb to Not Invented Here
Download syndrome.
and highly configurable system. AMQP is an example of such a system.
Its model of queues and exchanges provides the user with the means
to define programmatically almost any routing algorithm they can
Lesson learned: While code reuse has been promoted from time im- think of. The trade-off, of course, is a lot of options to take care of.
memorial and pattern reuse joined in later on, its important to think MQ opts for the former model because it allows the resulting prod-
Subscribe of reuse in an even more generic way. When designing a product, have uct to be used by basically anyone, while the generic model requires
a look at similar products. Check which have failed and which have messaging experts to use it. To demonstrate the point, lets have a look
succeeded; learn from the successful projects. Dont succumb to Not how the model affects the complexity of the API. What follows is an
Invented Here syndrome. Reuse the ideas, the APIs, the conceptual implementation of an RPC client on top of a generic system (AMQP):
frameworks, whatever you find appropriate. By doing so you are al-
lowing users to reuse their existing knowledge. At the same time you connect (192.168.0.111)
may be avoiding technical pitfalls you are not even aware of at the exchange.declare (exchange=requests, type=direct,
moment. passive=false,
durable=true, no-wait=true, arguments={})
Messaging Patterns exchange.declare (exchange=replies, type=direct,
In any messaging system, the most important design problem is that passive=false,
of how to provide a way for the user to specify which messages are durable=true, no-wait=true, arguments={})
routed to which destinations. There are two main approaches, and I reply-queue = queue.declare (queue=, passive=false,
believe this dichotomy is quite generic and applicable to basically any durable=false, exclusive=true, auto-delete=true,
problem encountered in the domain of software. no-wait=false, arguments={})

www.drdobbs.com February 2014 13


Register
Previous Next [ZEROMQ]

queue.bind (queue=reply-queue, exchange=replies, s = socket (REQ)


Previous Next
routing-key=reply-queue) s.connect (tcp://192.168.0.111:5555)
queue.consume (queue=reply-queue, consumer-tag=, s.send (Hello World!)
Previous Next
no-local=false, reply = s.recv ()
no-ack=false, exclusive=true, no-wait=true, arguments={})
request = new-message (Hello World!)
Previous Next
request.reply-to = reply-queue Up to this point, weve argued that specific solutions are better than
request.correlation-id = generate-unique-id () generic solutions. We want our solution to be as specific as possible.
basic.publish (exchange=requests, routing-key=my-service, However, at the same time, we want to provide our customers with as
Download mandatory=true, immediate=false) wide a range of functionality as possible. How can we solve this appar-
reply = get-message () ent contradiction?
The answer consists of two steps:
On the other hand, MQ splits the messaging landscape into so-
called messaging patterns. Examples of the patterns are publish/sub- 1. Define a layer of the stack to deal with a particular problem area
Subscribe scribe, request/reply or parallelized pipeline. Each messaging pat- (transport, routing, presentation, etc.).
tern is completely orthogonal to other patterns and can be thought of 2. Provide multiple implementations of the layer. There should be a
as a separate tool. separate non-intersecting implementation for each use case.
What follows is the re-implementation of the above application using
MQs request/reply pattern. Note how all the option tweaking is reduced Consider the example of the transport layer in the Internet stack. It
to the single step of choosing the right messaging pattern (REQ): is meant to provide services such as transferring data streams, apply-

www.drdobbs.com February 2014 14


Register
Previous Next [ZEROMQ]

ing flow control, providing reliability, etc., on ments with new patterns wont hurt the exist- This article summarizes our experience with
Previous Next
the top of the network layer (IP). It does so by ing patterns. building a large-scale distributed system in
defining multiple non-intersecting solutions: Lesson learned: When solving a complex a systematic manner. It focuses on problems
Previous Next
TCP for connection-oriented reliable stream and multi-faceted problem, it may turn out that are interesting from a software architec-
transfer, UDP for connectionless unreliable that a monolithic general-purpose solution ture point of view, and I hope that designers
packet transfer, SCTP for transfer of multiple may not be the best way to go. Instead, we and programmers in the open source com-
Previous Next
streams, DCCP for unreliable connections and can think of the problem area as an abstract munity will find it useful.
so on. Note that each implementation is com- layer and provide multiple implementations
pletely orthogonal: A UDP endpoint cannot of this layer, each focused on a specific well- Martin Sstrik is an expert in the field of
Download speak to a TCP endpoint. Neither can a SCTP defined use case. When doing so, delineate messaging middleware. He participated in the
endpoint speak to a DCCP endpoint. the use case carefully. creation and reference implementation of the
This means that new implementations can Be sure about what is in the scope and what AMQP standard and has been involved in vari-
be added to the stack at any moment without is not. By restricting the use case too aggres- ous messaging projects in the financial indus-
affecting the existing portions of the stack. sively, the application of your software may try. He is a founder of the MQ project, and
Subscribe Conversely, failed implementations can be be limited. If you define the problem too currently is working on integration of messag-
forgotten and discarded without compromis- broadly, however, the product may become ing technology with operating systems and
ing the viability of the transport layer as a too complex, blurry, and confusing for the the Internet stack. This article is excerpted
whole. users. and adapted from The Architecture of Open
The same principle applies to messaging pat- Source Applications: Volume II (http://is.gd/
terns as defined by MQ. Messaging patterns Conclusion pv0IqW)made available under the Creative
form a layer (the so-called scalability layer) As our world becomes populated with lots of Commons Attribution 3.0 Unported license.
on top of the transport layer (TCP and friends). small computers connected via the Internet Please see the full description of the license
Individual messaging patterns are implemen- mobile phones, RFID readers, tablets and for details at http://aosabook.org/en/intro1.
tations of this layer. laptops, GPS devices, etc. the problem html#license
They are strictly orthogonal the publish/ of distributed computing ceases to be the
subscribe endpoint cant speak to the request/ domain of academic science and becomes a
reply endpoint, etc. Strict separation between common everyday problem for every devel-
the patterns, in turn, means that new patterns oper to tackle. The solutions, unfortunately,
can be added as needed and that failed experi- are mostly domain-specific hacks.
www.drdobbs.com Month 2013 15
Register
Previous Next [DOCUMENTATION TIPS]

DOWNLOAD PDF
Previous Next 10 Things Tech Writers Want Developers
Previous Next to Know
Good documentation is often what makes good software great. Creating quality docs is the
Previous Next result of good habits and paying attention to the right details.

By Edward J. Joyce

I
Download

know a developer Ill call Bob (his real name customers rely on that information. Without communication that the public can under-
is Robert). Bob does great work, producing decent documentation, the best software stand and use.
Subscribe quality software in short order, but he has a and hardware may well be destined to be The Center for Plain Language has taken
common weak point: documentation. This shelf-ware. the law a step further by citing violations
begs the question: Should Bob even be To help Bob provide premium raw materi- in communication clarity from all organiza-
concerned with writing documentation? Bob is als to the documentation machinists on the tions. It bestows a WonderMark after the
paid to write code, not technical prose. assembly line, here are ten tried and true tips Centers judges read a document, shake
But Bob the developer does have docu- culled from seasoned technical writers. their heads, and say We wonder what they
mentation responsibilities as the supplier of meant.
raw materials. He must deliver specifications, Crisp, Clear, Concise Communication The following two examples are parts of
screen shots, diagrams, demonstrations, re- Sometimes it takes more than cajoling, coax- text that have earned the Centers Wonder-
corded sessions, and other relevant infor- ing, and cursing to convince a technologist Mark awards:
mation to a technical writer who will sculpt to write crisp, clear, concise communication.
it all into easily assimilated knowledge for Sometimes it takes a federal edict. In 2010, Our goal is to keep pace with all new techni-
consumption by end users. Project manag- President Barack Obama and Im not mak- cal and security-related requirements. For this
ers, product marketers, support staff, execu- ing this up signed the Plain Writing Act, reason, we inform you today about the transi-
tives, sales representatives, and ultimately a law that requires federal agencies to use tion to a new PHP version. Please note that after
www.drdobbs.com February 2014 16
Register
Previous Next [DOCUMENTATION TIPS]

the transition, the option Register Globals will Instead of leaving unmarked forks on the You can save wear and tear on the optic
Previous Next
not be supported anymore. highways of the roadmap, nip any budding nerve and cut the word count of the previous
confusion by using explicit terminology such paragraph in half with this abridgment:
Previous Next
and as Use the dialog under Edit/Preferences/
Cursors to change the cursor format. The WS-1035 product includes a touch-
Welcome to the online Policies and Proce- screen, remote temperature sensor, connecting
Previous Next
dures Manual. Use the buttons on the left to Avoid OON cables, power adapter, and the software pack-
view the table of the contents, or to search for (Overheating the Optic Nerve) age on disk.
documents by title or document ID number. To Overheating the Optic Nerve (OON) syn-
Download download any of the software packages please drome has yet to be chronicled in the Jour- There is a place for technical details. Its called
click the appropriate button on this page. nal of the American Medical Association; an appendix. Put the minutia at the back of
but if you read manuals, user guides, and the book where it wont obscure the concepts
Avoid WonderMark-class prose, and instead technical specifications, you have no doubt and principles that need to be conveyed to the
strive for ClearMark awards, which celebrate suffered its symptoms. It starts as gentle reader as the first order of business.
Subscribe the best in clear communication and plain pressure in the forehead while you peruse
language. technical literature searching for an elusive Purge Needless Politeness
piece of information. Needless politeness means using words such
Precise Instructions How does a developer cause OON? By satu- as please, thank you, and we appreciate your
If your life revolves around a niche software rating text with excessive, excruciating, exas- devotion to our insanely great products.
product such as Pixels with Personality, you perating details such as this example from the Consider the statement: If you observe the
can probably navigate the menus blindfolded. introduction of a product manual: 0x5422DA error 17 times in a minute, please
The statement Use the Preferences dialog to call our corporate technical support hotline
change the cursor is a clear roadmap for you. The shipping contents of the WS-1035 in- for advice. Thank you. That statement con-
If youre a technical writer charged with clude a touch-screen LCD monitor station, a tributes to deforestation by requiring un-
writing tomes for several software products, 433 MHz wireless temperature sensor, the re- necessary paper stock to express a simple
that statement may not get you far because spective connecting cables, a 110 VAC adapter, instruction. No one will be offended by the
launching the Preferences dialog could very and the software package on a 4.7 GB DVD politeness-free form: If you observe the
well require different navigation in the vari- disk. 0x5422DA error 17 times in a minute, call
ous products. technical support.
www.drdobbs.com February 2014 17
Register
Previous Next [DOCUMENTATION TIPS]

Save those courtesies for making amends Introduction, System Requirements, and In- Use the dialog under Edit/Preferences/Cur-
Previous Next
with technical writers whom you offended stallation sections of the document? Do they sors to change the cursor format.
in the past with incomplete, inaccurate, or include statements such as This product is not
Previous Next
incomprehensible drafts of documentation. compatible with Windows 95, or This product to
requires a minimum of 2 GB RAM? The earnest
Accurate, Thorough, Timely Reviews reader, when coming upon these statements, Choose the dialog under Edit/Preferences/
Previous Next
If you think of technical documentation as is forced to wonder if the documentation has Cursors to change the cursor format.
a publication, the developer starts out as a not been updated since a bygone era when
research assistant collecting facts, describing Windows 95 and hardware with 2 GB or less of and then to
Download processes, and assembling other raw informa- RAM was in vogue.
tion for authors (the technical communica- Other red flags are entries in the Known Navigate to the dialog under Edit/Prefer-
tors). At the end of the process, the research Issues section that list problems which were ences/Cursors to change the cursor format.
assistant dons the hat of editor and reviews corrected umpteen releases ago.
and critiques the authors draft document. The final key to a good review is timeliness. Establish a threshold for revisions. If a pas-
Subscribe A review has three main properties: accu- Dont wait until the 11th hour to deliver to sage is factually correct and accurately con-
racy, thoroughness, and timeliness. Accuracy the technical writer a 200-page manual with veys the information, let it stand and trust
is self-explanatory. The entire review rates a enough edits to drain several red pens. Give the writers instincts. Avoid the temptation
failing grade if the end result is wrong. the writer sufficient time to incorporate your to micro-edit something just so it sounds
Thoroughness, an often overlooked goal updates without creating panic during the better in your opinion.
of a technical review, distinguishes key ref- waning minutes of the release cycle.
erence documents from permanent fixtures Revere Visuals
on dusty bookshelves. It means reviewing Reasonable Edits In the evolution of computing, documenta-
the overall content rather than narrowly Related to timely reviews are reasonable tion has marched lockstep with software
focusing on your particular area of respon- edits. In other words, trust the writers in- evolving from text to text-with-graphics to
sibility. stincts and dont pepper the draft with re- hyperlinked prose to audio and video. As a
For example, if you provided input for a Con- wordings of sentences due to capricious developer, you need to recognize that the
figuration section describing a Cursor Selec- preferences. raw materials you put in the documentation
tion dialog, you would feel obligated to review You are guilty of capricious preferences if pipeline should also reflect a similar transfor-
it to ensure that it is accurate. What about the you asked a writer to change text such as: mation.
www.drdobbs.com February 2014 18
Register
Previous Next [DOCUMENTATION TIPS]

A video presentation from a developer Be Monolingual ing team painstakingly crafted in English now
Previous Next
might be overkill, but you can at least share You might be tempted to show off your becomes raw material for translators.
your desktop for live demonstrations of the worldly nature by interspersing a document As with the original draft documentation,
Previous Next
software in action. A combination of a text- with Latin phrases and abbreviations such as if the assembly line is filled with good qual-
based description and a good visual is source those in this example: ity materials that is, materials that were
input gold to a technical writer, said Brian Ev- The wireless communication between the prepared with translation as an option the
Previous Next
erett, Senior Technical Information Architect base station and the remote module, i.e., the translators will have a better chance of quickly
at CA Technologies. client sensor, reaches distances of 100 meters in producing foreign language versions.
So, if you have a choice between feeding unimpeded areas, e.g., direct line of sight, ergo The first factor to consider when preparing
Download gold or ordinary ore into the documentation place the base and client in fields, beaches, ur- documentation for translation is communica-
forge, opt for the gold. ban lots, et al. tion with the translator. The translator may
well be someone living on the other side of
Spell Out Acronyms Consider the challenge of a translator who the globe who has learned English as a sec-
Consider the following statement: is faced with translating this verbiage to ond language. Given cultural and time-zone
Subscribe another language so the product can be differences, most communication may be by
MP performance can be impeded by noise in marketed in other countries. Those phrases e-mail, as opposed to direct conversations
an ISDN BRI which can also impact LCP negotia- and abbreviations from the ancient Romans and meetings. So make an extra effort to
tion. constitute added effort to the jobs of the clarify any questions or concerns from the
translators. Not only must they translate the translator in your email responses.
The acronyms in this passage can have a vari- English words, but they must also recognize To help ensure that the documentation will
ety of meanings to the uninitiated. MP could be the Latin terms and translate them correctly. be translation-ready once it is delivered to the
interpreted as Multilink Protocol or MP3 (mis- Save foreign phrases for cocktail parties. In translation team, here are some pointers:
typed as MP) or if its part of an instruction technical documentation, be monolingual
manual for an army command post Military and stick to plain language. Use the same term or expression for the
Police. same concept. Consider this example:
To avoid ambiguity, take a few seconds to Expect Translation
spell out acronyms and save yourself a round If a product is successful in one country, its To activate this software product, ensure
of emails and phone calls from a befuddled apt to be successful in others. In that case, the that you are logged in with administrator privi-
writer. documentation that you and a technical writ- leges on the system. Download the kit to your
www.drdobbs.com February 2014 19
Register
Previous Next [DOCUMENTATION TIPS]

computer. Run the installation utility to acti- Instead of: Use: Vertical lists, as shown in the second ital-
Previous Next
vate the program on your hardware. icized passage, are key to breaking down
ascertain find out technical information into easily assimilated
Previous Next
In this case the words system, computer, chunks. Use them liberally.
and hardware could all be replaced by com- expedite hasten, speed up
puter; and software product, kit, and program Use active voice wherever possible. Com-
Previous Next
could all be replaced by software. Consistency necessitate need, have to, require pare the active and passive forms of the fol-
in terminology will help dispel any ambiguity lowing instruction:
and potential misinterpretation. prior to before Passive: Any change to the configuration
Download must be updated in the master file.
Avoid expressions that are culture specific. subsequent to after Active: When you change the configuration,
Your development colleagues may understand update the master file.
this admonishment in a troubleshooting guide: Use vertical lists. Read the following pas- The passive voice conceals who or what
sage a few times while noting the tempera- does the action, and it requires more words
Subscribe The attribute not found error may be a red ture of your optic nerve: than the active voice. In short, passive is more
herring. To level the playing field, run the data- difficult to translate than active, so choose ac-
base table verification utility. In the Home screen, select one of the catego- tive wherever possible.
ries or select the Customize Selection to custom-
Good luck trying to translate that advice into ize updates that you want to apply to the system If you get into the habit of following these
Mandarin. Instead, use the more universal: depending on the requirement. recommendations, your customers and your
colleagues will be thankful. Your customers
The attribute not found error may be related Now read: will especially reward you by using your prod-
to another problem. Check the database by run- ucts and using them correctly.
ning the table verification utility. In the Home screen, do one of the following
depending on the requirement: Edward J. Joyce, a software engineering
Use plain words. To ease the translation manager, strives to provide source input gold
process and avoid violating the Plain Writing Select one of the categories. to his writer colleagues at CA Technologies.
Act, use simple words instead of pompous Select the Customize Selection to
synonyms. Here are a few examples: customize updates.
www.drdobbs.com February 2014 20

You might also like