Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Return Address

The request message carries the name of the channel its reply must be sent to, so the replier answers whoever asked without knowing any caller, and each caller, or each instance of one, can get its replies on a channel of its own.

One reply queue, eight readers

A print shop's proof renderer sends every finished proof to one queue, and the storefront that asks for proofs runs eight instances that all read from it. Most proofs reach an instance that never asked for them. When a customer uploads artwork, the instance holding her session sends a render request on the proofs.requests queue, through the shop's Message Broker, RabbitMQ, and waits, with her browser connected, for the PDF. The renderer, written when the storefront was a single process, sends the proof to storefront.proofs, and whichever instance takes the next message gets it. With eight readers, a proof reaches the instance waiting for it roughly one time in eight. In the logs it looks like this:

  1. storefront-6 render-proof requested, job J-5521
  2. storefront-3 proof for unknown job J-5521, ignored
  3. storefront-6 job J-5521 timed out after 30 s

Meanwhile, the sales desk app needed proofs too, for quotes its staff prepare on the phone. The renderer team was asked to add a branch: when a request comes from the sales desk, send the proof to salesdesk.proofs. Each new caller would mean a change and a deploy of the renderer, and the eight storefront instances would still be sharing one queue.

The request says where the answer goes

The fix moves that decision into the request. Each request carries the name of the channel its reply must be sent to, in a header usually called something like replyTo, and the replier sends the reply there. Storefront instance 6 now reads from a queue of its own, storefront-6.proofs, and puts that name on every request it sends; the sales desk puts salesdesk.proofs on its own. The renderer's reply code loses its branches: read the address off the request, send the proof to it. An address carried by the request this way is a Return Address.

Each proof goes where its request said

Both callers share one request queue. The renderer reads the reply channel off each request and knows neither caller.

The renderer knows only the queue it reads from and one rule for answering. A ninth storefront instance, a second sales tool, or a nightly job that re-renders proofs for repeat orders each name their own reply channel and work from their first request, with no change to the renderer's code. On the storefront side, the code that handles an upload calls requestProof(job) on a Messaging Gateway, which stamps the reply address on the request and completes when the proof comes back.

The address says where a reply goes, not which request it answers. Storefront 6 can have forty proofs in flight on its own queue, so each request also carries the job id, which the renderer copies onto the reply: that is a Correlation Identifier, and its article covers the matching, including a reply that arrives late or twice.

A per-instance queue is not the only valid address. Naming the shared storefront.proofs on every request is still a Return Address, since the renderer still knows nothing about the storefront. It works when any instance can act on any reply, for example by saving the proof where the waiting one can find it, with the Correlation Identifier telling the taker which job it holds. Here the storefront wants the proof pushed to her browser by the instance holding her connection, so each instance gets an address of its own.

When the address stops existing

Per-instance queues get each proof to the right instance. That moves the question rather than removing it: what happens to a queue when its instance goes away, as storefront instances do with every deploy? Brokers offer short-lived reply queues for this, such as a RabbitMQ exclusive queue, which the broker deletes when the connection that declared it closes and whose name goes in the message's reply_to property, or a JMS temporary queue, carried in JMSReplyTo.

That convenience has a sharp edge. At 14:02 storefront 6 restarts during a deploy, while the renderer is still working on three of its proofs. Its exclusive queue disappears with the old connection. A proof sent before the new process declares its queue finds no queue by that name, and by default RabbitMQ drops a message that matches no queue without telling the publisher (one that sets the mandatory flag gets it returned instead). A proof sent after that reaches a process that has never heard of its job, and is ignored like J-5521. Had the broker generated the queue's name, as RabbitMQ can, every one of them would miss, since the new queue's name is one no in-flight request carries. Three proofs rendered for nothing.

So work out in advance what an orphaned reply costs. For a proof, very little: the browser connection that was waiting died with the old instance, the page sends the request again when it reconnects, and the storefront keeps its timeout. A reply that must not be lost cannot depend on an address that dies with one process. Either the requester names a durable queue, with persistent messages, that outlives its restarts, and the new process that reads it has to recognise replies to jobs it never started, or the result is stored where any instance can fetch it, which is the shape of Asynchronous Request-Reply, with its status check or callback.

An address the replier did not choose

A Return Address hands the requester a decision the replier used to make: where the replier's output goes. A proof contains a customer's artwork, address and prices, and the renderer publishes it to whatever channel the request names. Anyone who can put a message on proofs.requests can write billing.invoices as the address and have a PDF land on the invoicing queue, or name a queue they read themselves. So the renderer replies only to channels it recognises, here names that match storefront-*.proofs or equal salesdesk.proofs, and rejects and logs a request whose address fails the check instead of answering it. The check is only as good as who can create and read those queues: if anyone can declare storefront-9.proofs and consume from it, the pattern lets that address through, so the broker's permissions should keep each allowed reply queue to the caller it belongs to and, where they can express it, keep the renderer's account from publishing anywhere else. When the callers have different rights, a name that passes is not enough either: the renderer also checks that the authenticated requester may use that reply address, so the sales desk cannot send its proofs to storefront-6.proofs.

The same idea turns up outside messaging. An HTTP API that accepts a callback URL with a long-running request, and later posts the result to it, is using a return address in another transport, and needs the same check on where it is sending.

Drawing it in the Challenge

No Challenge checks a Return Address yet. The Messaging Endpoints & Broker Challenge has the node in its palette without requiring it, so once your solution meets its requirements you can add one: a Return Address beside the Messaging Gateway on the request's way out, labelled with the reply channel's name, and that reply channel drawn from the replying endpoint back to the gateway. No requirement scores it; what to look for is a replier that takes its reply channel from the request, with no rule of its own that names a particular caller.

Practice this