jonnify·redis /redis-patterns/coordination

R2 · Coordination & Consistency

Coordination —
atomicity first.

The foundation every later chapter builds on: a reliable queue is made of atomic moves and a lock lease.

Five patterns for getting concurrent access right — atomic read-modify-write, distributed locking, the Redlock contrast, cross-shard consistency, and hash-tag co-location. The grounding is EchoMQ (echo/apps/echo_mq): every state transition is one inline Lua script run EVALSHA-first, the claim lease holds a job to one worker with attempts as its fencing token, and the braced emq:{q}: keyspace keeps a queue's keys on one slot. The chapter closes by making an codemojex guess submission atomic across runtimes. It comes before the queue chapters because a reliable queue is built from atomic moves.

§1

Why & when

echo/apps/echo_mq

Concurrency breaks data in quiet ways: two writers read the same value and one update vanishes; a job meant for one worker runs on three; a multi-key change lands half-applied across cluster slots. Coordination is the discipline of making concurrent access correct — with an atomic operation that admits no interleaving, a lock that admits one holder, or a key layout that keeps related data together. Each pattern is a different answer to one question: who is allowed to change this, and when.

Lost update

Two writers read-modify-write one key and an update disappears — one inline Lua move closes the race.

One worker at a time

A claimed job must run on exactly one node — the lease admits a single holder until it lapses.

Surviving a failover

A lock that must outlive a node failure — Redlock's majority-of-N, weighed against its cost.

Multi-key on a cluster

Related keys that must change together — the braced hashtag keeps them on one slot.

Contention · many clients, one serialized change through a single atomic Lua EVAL
client A client B client C atomic gate EVALSHA · or SET NX one state one at a time — no lost update

Every pattern in this chapter narrows who may change a key, and when — until concurrent access produces one correct result instead of a race.

§2

The patterns

five modules · one workshop

Five teaching modules, then a workshop. Each module is a hub with three dives.

built ready to read soon specified
§3

How to apply

name the race · the primitive follows

The hard part is matching the coordination primitive to the failure you are guarding against. Name the race you cannot allow, and the pattern follows.

Pick the need · get the primitive
readout · Select a need to see which primitive matches and how EchoMQ applies it.

There is no universal coordination primitive — only the one that matches the race you cannot allow.

§4

The workshop

echo/apps/codemojex · EchoMQ.Jobs

The chapter closes with R2.06: make an codemojex guess submission atomic across runtimes. Codemojex.Guesses.submit/3 turns an untrusted request into a typed place command, minting a branded ORD id at acceptance; the order is then admitted onto the {orders} queue as one inline Lua script — a kind check, a duplicate refusal, the row write, and the pending insertion happening on the server in one atomic step. It is idempotent on retry, never a torn write, and needs no lock and no Redlock, because the script is the serialization.

Notes on Valkey. An inline Lua script runs atomically — no other command interleaves with it — and every key it touches is declared in KEYS, so on a cluster the keys must share one slot; the braced {q} hashtag is what guarantees it. valkey.io/topics/cluster-spec

§5

Up next

R3–R8

With atomicity in hand, the queue chapters can be built: a reliable queue is a set of atomic moves over the braced keyspace.

R3soon

Reliable Queues

Wait, active, done, recover.

R4soon

Time, Delay & Priority

The sorted set as a clock.

R5soon

Streams & Events

The durable log.

R6soon

Flow Control

Staying stable under load.

R7soon

Data Modeling

How data lives in RAM.

R8soon

Production & Ops

Running the tier at scale.

§6

References

sources · related

Sources

Related