Topic
Distributed Systems
What happens when work is spread across processes that crash, stall and retry: queues, idempotency, sharding, caches and the failures between them.
ARTICLES
Choosing Between Optimistic, Pessimistic, Advisory, and Distributed Locks
When to use SELECT FOR UPDATE, version checks, Postgres advisory locks or Redis locks, and which failures each one survives. Includes fencing tokens for expired leases.
When RabbitMQ Isn't Enough: Building Durable, Retryable Jobs
Database-backed jobs with atomic claiming, backoff, circuit breakers and an outbox, and what each piece does and does not guarantee.
Circuit Breakers in Production: Timeouts, Retry Storms, and Failure Recovery
How circuit breakers limit calls to a failing API, how half-open probes work, and how timeouts and retries can prevent a retry storm.
Why Exactly-Once Processing Is Hard: RabbitMQ, Retries, and Idempotency
Where RabbitMQ duplicates come from and how an outbox, deduplication keys and idempotent consumers protect database effects and external calls.
CHALLENGES TO RUN
Fencing a zombie worker
A worker stalls, its lease runs out, someone else takes the job, and the first worker wakes up and finishes anyway. Make its late work count for nothing.
One message per action through a flaky provider
Send exactly one SMS per order through a provider that times out and sends duplicate, out-of-order webhooks, while workers get killed. Then share the workers fairly across 100 stores.
Where does each comment live?
Split comments across databases four ways on real SQLite shards, move data without losing writes, then meet the viral post no hash can spread and the stale server writing to the wrong place.
A popular post takes down the database
A cached comment list expires under 20,000 reads a second and the database goes down for good. Fix it with stale serving, one refresh per key, a refresh budget and a local cache, all measured.
CONCEPTS
What is at-least-once delivery?
At-least-once delivery means the broker keeps redelivering a message until the consumer acknowledges it. The message is never lost, but it can arrive more than once. So the consumer must tolerate duplicates, and exactly-once effects come from an idempotent consumer on top.
What is a cache stampede?
A cache stampede happens when many requests miss the same expired or missing cache entry at the same moment and all recompute it at once. The sudden burst of identical queries hits the database together, can overload it, and turns a brief expiry into a long slowdown or outage.
What is consistent hashing?
Consistent hashing places both keys and servers on the same circle of hash values. A key belongs to the first server clockwise from it. When a server joins or leaves, only the keys in the arcs next to it change owner, instead of almost every key as with hash % n.
What is a fencing token?
A fencing token is a number that increases every time a lock or lease is granted. The protected resource remembers the highest one it has seen and rejects writes carrying a lower one, so a paused holder whose lease has expired cannot overwrite the new holder's work.
What is a hot key?
A hot key is a single key that receives a large share of all traffic. Hashing spreads different keys across servers, never one key, so every request for it lands on one server, and adding servers does not help. Fixes are splitting the key, caching reads, or giving it dedicated capacity.
What is an idempotency key?
An idempotency key is a unique id that the client chooses and sends with a request, so a retry of the same operation can be recognized and not applied twice. The server stores the key together with the result, and answers a repeated key with the stored result instead of acting again.
What is a metastable failure?
A metastable failure is a state where a system stays broken after the trigger that caused it is gone, because a feedback loop inside the system keeps the overload alive. Removing the original cause does not help. The system only recovers when load is cut or the loop is broken.
What is request coalescing?
Request coalescing, also called singleflight, makes concurrent callers asking for the same key share one in-flight call instead of each making their own. The first caller runs the work and the rest wait for its result. It turns a burst of identical requests into a single backend query.
What is stale-while-revalidate?
Stale-while-revalidate is a caching strategy that serves a cached response after its freshness time has passed, while a refresh runs in the background. Readers get an instant answer and the next reader gets a fresh one. It stops at a hard limit, after which the old copy is no longer served.
What is the transactional outbox pattern?
The transactional outbox pattern writes an event to an outbox table in the same database transaction as the state change it describes. A separate relay process later reads the table and publishes the event. The event cannot be lost if the change committed, but it may be published more than once.
What are virtual nodes?
Virtual nodes (vnodes) give each physical server many positions on a hash ring instead of one. Its share becomes the sum of many small arcs, so load evens out, and when a server leaves, its data spreads over several neighbors instead of landing on one.