Skip to main content

Queue Centric Work Pattern

The Queue Centric Work Pattern (QCWP) is simple. Send a message declaring the intent of the command, acknowledge the message and proceed. All work takes place in a background process so the user is not kept waiting for the request to return. Acknowledgement usually takes the form of persistence to ensure that no messages are lost. Real life examples of the QCWP in action would be the sending of an email or the confirmation of an order being accepted from an online retailer.

The QCWP will introduce the concept of eventual consistency, which surprisingly is not an issue in most cases. The queue itself should be implemented via some form of message queue that handles some of the more complicated technical issues regarding message meta data, routing, persistence and so on. Once a message queue has been chosen the code required to implement QCWP does not differ to far from simple request-response examples in terms of both complexity and lines of code.


Reduced Latency

Transferring the message, confirming acknowledgement and returning to user with some form of confirmation can be very quick. If the process is long running, it can be vastly quicker to use the QCWP. Even for low latency scenarios, the use of the QCWP introduces other benefits.


If something fails you can retry the command in a background process. Nothing is lost when one or more systems are down. If the command fails consistently, then you can simply notify the user or perform some other compensating action.


If one system is offline the message is just stored and the queue builds up. Once back online the queue will be emptied. The temporal coupling between the two systems is now removed. Coupling has been reduced so much that you can switch consumer with another system and the client would be unaware as long as the message formats remain the same. This allows different languages to read and populate the queues.


To increase throughput you can simple introduce a competing consumer until the appropriate amount of messages is handled within a SLA boundary. The inverse is also true. The QCWP allows throttling. Rather than peak load from web server traffic hitting the back end services, these can be scaled independently. As the consumer of the messages will handle each message at its own pace, there is no chance that other dependencies such as databases would become overwhelmed.


These benefits don't come for free however. The main issue with the QCWP is the time it takes to get to grips with this change of conceptual model. Testing asynchronous code is a lot harder, introducing problems such as polling shared resources for changes. The very same issue means simply debugging asynchronous systems can be challenging even with good monitoring and auditing in place.


QCWP was a real change in terms of how I think about two services communicating. This change in pattern is not hard, merely different. Once you adjust to the challenges, the benefits enable some truly resilient systems when communication must occur out of process.


Popular posts from this blog

Constant Object Anti Pattern

Most constants are used to remove magic numbers or variables that lack context. A classic example would be code littered with the number 7. What does this refer to exactly? If this was replaced with DaysInWeek or similar, much clarity is provided. You can determine that code performing offsets would be adding days, rather than a mysterious number seven.Sadly a common pattern which uses constants is the use of a single constant file or object. The beauty of constants is clarity, and the obvious fact such variables are fixed. When a constant container is used, constants are simply lumped together. These can grow in size and often become a dumping ground for all values within the application.A disadvantage of this pattern is the actual value is hidden. While a friendly variable name is great, there will come a time where you will want to know the actual value. This forces you to navigate, if only to peek at the value within the constant object. A solution is to simple perform a refactor …

Three Steps to Code Quality via TDD

Common complaints and problems that I've both encountered and hear other developers raise when it comes to the practice of Test Driven Development are: Impossible to refactor without all the tests breakingMinor changes require hours of changes to test codeTest setup is huge, slow to write and difficult to understandThe use of test doubles (mocks, stubs and fakes is confusing)Over the next three posts I will demonstrate three easy steps that can resolve the problems above. In turn this will allow developers to gain one of the benefits that TDD promises - the ability to refactor your code mercifully in order to improve code quality.StepsStop Making Everything PublicLimit the Amount of Dependencies you Use A Unit is Not Always a Method or ClassCode quality is a tricky subject and highly subjective, however if you follow the three guidelines above you should have the ability to radically change implementation details and therefore improve code quality when needed.

DRY vs DAMP in Tests

In the previous post I mentioned that duplication in tests is not always bad. Sometimes duplication becomes a problem. Tests can become large or virtually identically excluding a few lines. Changes to these tests can take a while and increase the maintenance overhead. At this point, DRY violations need to be resolved.SolutionsTest HelpersA common solution is to extract common functionality into setup methods or other helper utilities. While this will remove and reduce duplication this can make tests a bit harder to read as the test is now split amongst unrelated components. There is a limit to how useful such extractions can help as each test may need to do something slightly differently.DAMP - Descriptive and Meaningful PhrasesDescriptive and Meaningful Phrases is the alter ego of DRY. DAMP tests often use the builder pattern to construct the System Under Test. This allows calls to be chained in a fluent API style, similar to the Page Object Pattern. Internally the implementation wil…