One case for queues
Ted Dzubia recently posted The Case Against Queues. I have to grant him that, like anything else, queues can be over/misused, so probably they are being in some parts. Film at 11. (I would say that re useful metrics to alarm on, the time between a message arriving on a queue and it being acked as processed is typically easy to measure and to set sensible thresholds for.)
In an Google+ discussion of the post the argument has been advanced that queues should be avoided wherever possible; that they cause more problems than they solve. My limited experience is the other way; I’m working on systems where I’m finding them to be a highly valuable abstraction and where I’m not seeing significant cost. I thought the discussion would benefit from more examples, so I present here one case where I’ve found them very useful.
Suppose there are two systems, which I’ll call Truths and Consequences. They maintain state in related domains, but are decoupled - probably owned by different teams - and they model their domains in sufficiently different ways that working out the consequences of truths requires some code to crunch. It could take quite a long time to recalculate all consequences of all truths, but we can usually comfortably keep up with recalculating consequences as truths change, and we don’t want to ignore the consequences of any truths.
Truths and Consequences communicate through a queue with typical robustness properties such as At-Least-Once delivery. When the truth about some named thing (say “cats”, or “dogs”) changes, Truths publishes a message containing that name onto a queue. Note that the message doesn’t say “the truth about cats is that they are all gray”, but rather “the truth about cats has changed”. When Consequences consumes the message, it asks for the current Truth about the named thing, updates relevant consequences, and acks the message. For any given set of truths, calculating their consequences (and hence, processing a message) is of course idempotent.
Of course the queue decouples the two systems so that you can change truths without waiting to hear about the consequences - hell, Consequences can drop off the network for a while as far as Truths cares, and in a halfway decent framework the queue works as a resilient store out-of-the-box. These are the kind of reasons why we introduced the abstraction in the first place. But there are other nice properties of this system too.
For example, suppose occasionally a new truth appears that we can’t currently calculate the consequences of. Maybe there’s a disastrous edge-case that crashes the calculation (“dog.getNose().smell() throws NullPointerException”), or maybe the truth breaks some assumptions that the calculation has used (“uh, what? dog.getHead() returned an array??”) The “dogs” message is poisonous and Consequences can’t currently handle it. So it nacks the message, prompting it to be moved from the original queue to an associated dead-letter queue (DLQ). After the Consequences system has been updated to deal with the new situation, it can have another go at consuming such messages, which were sitting on the DLQ in the mean time. Another possibility is that Truths realizes they were talking nonsense, and changes their mind back to something sane. In that case the Consequences system doesn’t need any update, it just reconsumes the message; when it asks for the truth about “dogs” it finds that they have just the one head again and all is well.
Another example: for some set of things, the truths are unchanged, but Consequences is updated to fix a mistake in the existing calculation, or to extend it with a useful new consequence to calculate. Publish messages naming all the relevant things, and the new calculation will be applied. Is it going to take a while and we don’t want to interrupt business as usual? Put these messages on a lower priority “backfill” queue that is only consumed when the regular one is empty. Are some of the things more important than others? Order the messages on the backfill queue to reflect that. Similar approaches apply when truths about a large new set of things become available and need backfilled.
A last example: during an era of uncertainty, truth about some set of things becomes very volatile, swinging back and forth faster than the consequences can be calculated and recalculated, filling up the queue with a backlog of messages naming the same things over and over. Just introduce some plumbling that dedupes the pending messages by name - because we always calculate consequences based on the current truth, not the historic one.
Note that either system can take responsibility for the queue and work to redrive DLQs and reprioritize messages - typically it depends on which role cares most about the relationship. If Truths wants to be heard, it can own the queue; if Consequences needs to know the truth, vice versa. In the system I work on right now, we play both these roles in relationships with various other teams.



Under the banner of
The exact content varies with the theme but this Sunday afternoon’s
Whilst deserting, I also got to enjoy the practicalities of a cloaking device: a long hooded covering formally called a
I really quite liked it. Warm and light as previously noted, it is also great for my regular extra thirty minutes of sleep in the back of the 43 bus during my commute out to South Queensferry, and it didn’t take me too long to learn how to not trip on it. But it gives off very loud pretentious goth vibes, whilst I’m a quiet and dispassionate goth who does not seek to provoke the staring or the cries of “hey, Aragorn”.