Singular Update Queue

Use a single thread to process requests asynchronously to maintain order without blocking the caller.

Problem

When the state needs to be updated by multiple concurrent clients, we need it to be safely updated with one-at-a-time changes. Consider the example of the Write-Ahead Log pattern. We need entries to be processed one at a time, even if several concurrent clients are trying to write. Generally, locks are used to protect against concurrent modifications. But if the tasks being performed are time-consuming, such as writing to a file, blocking all the other calling threads until the task is completed can have severe impact on the overall system throughput and latency. It is important to make effective use of compute resources, while still maintaining the guarantee of one-at-a-time execution.

Solution

Implement a work queue and a single thread working off the queue. Multiple concurrent clients can submit state changes to the queue—but a single thread works on state changes. This can be naturally implemented with goroutines and channels in languages like Golang.

for more details go to Chapter 13 of the online ebook at oreilly.com

This pattern is part of Patterns of Distributed Systems

23 November 2023