Highlights

Contents

Part IV
04

Concurrency across instances

We leave the single process and find that its locks no longer help. Wren runs four to twenty identical instances, and the data they race over lives in shared stores. We will cover why in-process locks fail across a fleet and the lock-free strategies that work, and compare-and-swap on shared storage.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Processes, threads andevent loops2Async code and pools3Race conditions in memory4Concurrency acrossinstances5Distributed locks6Backpressure and overload7Memory and resource leaks8The Node.js runtime9Go and other runtimes10One instance end to end

#Section 10Why local locks stop working

Wren’s withdrawal code had a mutex around the balance check, added after a race in testing. In production the race came back. The reason is structural. A mutex lives in one process’s memory. App instance 1 holds its mutex, app instance 2 holds a different mutex in a different process on a different machine, and both read the balance of 100 from PostgreSQL at the same moment. Each lock was respected perfectly, and each guarded nothing the other could see.

A MUTEX GUARDS ONE PROCESS; FOUR INSTANCES MEANS FOUR UNRELATED MUTEXESA MUTEX GUARDS ONE PROCESS; FOUR INSTANCES MEANS FOUR UNRELATED MUTEXESapp 1app 2app 3app 4balance 100app 1 and app 2 each holdtheir own lock and bothwithdraw 80
Figure 9.19Four instances, four separate mutexes. Two of them withdraw 80 each from the same balance.

Any state that more than one instance reads and writes needs coordination that all instances can see. Distributed concurrency is that coordination, and the most important design skill here is choosing the cheapest correct form of it. A distributed lock, Part V, is the most expensive and fragile, and good designs try everything else first.

The first option is to let the shared store do the work. Unit VI showed the forms. An atomic statement, UPDATE accounts SET balance = balance - 80 WHERE id = 7 AND balance >= 80, checks and writes in one step under the row lock, and 0 rows updated means insufficient funds. A row lock with SELECT ... FOR UPDATE serializes the read-check-write in a short transaction. A unique constraint makes duplicates impossible. A version column gives optimistic concurrency. All of these hold across any number of instances, because the database is the single place where the data lives.

The second option is a single writer. If all changes to one entity are made by one process, there is no race on that entity. Unit VIII’s Kafka partitioning gives this for free. Key commands by account id, and every command for account 7 goes to one partition, processed in order by one consumer. No lock is needed, because there is never more than one writer for account 7 at a time. Actor systems such as Akka and Orleans make the same idea explicit, with each entity owned by one actor that processes its messages one at a time.

The third option is idempotency. If the race is between duplicate attempts at the same operation, such as two retries of one payment, an idempotency key with a unique constraint makes the second attempt return the first result. The race stops mattering.

Only when none of these fit, when the work spans several stores or external systems and must not run twice concurrently, is a distributed lock the right tool. Wren’s nightly report job is the example the next part uses. It reads from many tables, writes files to object storage and sends emails, and two copies running at once would send every restaurant two reports.

#Section 11Compare-and-swap on shared storage

Optimistic concurrency, compare-and-swap at the scale of a whole system, is available in almost every shared store, and it is often the best way to coordinate instances without a lock. The pattern is always the same. Read the item with its version, compute the change, write with a condition that the version is unchanged, and on failure read again and retry or report a conflict.

In SQL it is the version column of Unit VI, UPDATE ... WHERE id = 123 AND version = 7. In HTTP it is the ETag and If-Match header of Unit I, where a PUT with a stale ETag gets 412 Precondition Failed. Amazon S3 added conditional writes with If-None-Match in 2024 and If-Match soon after, so two instances can safely update the same configuration object. DynamoDB has condition expressions on every write. etcd and ZooKeeper offer compare-and-set on a key’s revision. Redis has WATCH and Lua scripts, Unit VII.

COMPARE-AND-SWAP ON SHARED STORAGE: A CONDITIONAL WRITECOMPARE-AND-SWAP ON SHARED STORAGE: A CONDITIONAL WRITEapp 1app 2settings.jsonETag "v7"PUT If-Match: "v7" → 200, now "v8"PUT If-Match: "v7" → 412SQL WHERE version = 7, DynamoDB condition expressions, S3 If-Match, etcd transactions
Figure 9.20Two instances write the same object. The one carrying the old ETag gets 412 and must reread.

Wren uses it for its feature flag configuration, a JSON document in object storage that several admin instances edit. Each admin reads the document with its ETag, applies a change, and writes with If-Match. If two admins edit at once, the second gets 412, rereads the document with the first admin’s change included, reapplies its own change and writes again. Neither change is lost, and no lock was held at any point.

Conditional writes beat locks in three ways. Nothing is held while the client thinks, so a crashed or paused client blocks nobody. The check happens in the store, at the moment of the write, which is the only moment that matters, so a paused client that wakes up with stale data simply fails its write instead of corrupting anything. And there is no lock service to run.

Their limit is contention. As in Unit VI, the expected attempts per success is 1 ÷ (1 − p) for conflict probability p. For a hot item updated by many writers, retries pile up, and a single writer or a queue is better. They also apply to one item at a time. Keeping several items consistent needs a store with multi-item transactions, such as DynamoDB’s TransactWriteItems, or one of the patterns from Unit VIII.

This conditional-write idea comes back in Part V in a sharper form. The fencing token, the fix for unsafe distributed locks, is a conditional write that checks a token instead of a version.