What You Trade When You Distribute a System
CAP in practice, consistency as a business decision, sagas, idempotency and circuit breakers, with examples from Loom and Crystal.
Partial failure
A monolith tends to fail in one piece. The process dies and everything is down, which is bad but easy to see. A distributed system fails in pieces. One service is slow, another returns old data, a third is up but can’t reach its database. Those failures are harder to notice and much harder to reproduce.
That isn’t an argument against distributing. It’s an argument for doing it on purpose, because you hit a limit a single machine can’t handle, and not because the architecture diagram looks better with more boxes.
CAP, the useful version
The CAP theorem says that during a network partition a system has to choose between consistency and availability. Partitions aren’t optional (links fail, zones lose contact), so what matters is what each operation does while one is happening.
A CP operation refuses to answer rather than answer wrong. A bank balance is the standard example, and so is the commit path in Crystal. A minority that lost contact with the majority can’t commit anything, by design.
An AP operation keeps answering and accepts that the answer might be old. A social feed that is thirty seconds behind is fine.
Most systems mix both, per operation.
profile reads AP, a few seconds of staleness is fine
account balance CP, must be right
notification status AP
payment CP, a double charge is not acceptable
Crystal even lets the caller choose per read. A linearizable read goes through the leader’s confirmation that it still leads. A bounded-staleness read can be served by any node that heard from the leader recently enough, and it fails instead of serving data older than the bound.
Consistency is a business decision
“Eventual consistency” sounds like a technical compromise, but what you are deciding is how stale the data can be before a user notices or cares. Say a price change takes thirty seconds to reach every replica, and a customer adds the item at the old price in that window. Do you honor the old price, show an error, or quote again? That’s a business rule, and it decides how much consistency you need. The architecture follows from it, not the other way around.
Leader election, usually borrowed
When one node has to be in charge (one consumer of a queue, one holder of a lock), you need leader election. The hard cases are a leader that crashes, a partition where both sides think they lead, and an old leader that comes back.
I wrote a whole series about how Raft handles those cases, because I implemented it myself. For production I’d still borrow a proven one. etcd runs Raft and is what Kubernetes uses, and ZooKeeper has been doing this for years. A lease-based election over etcd takes a few lines of Go.
session, _ := concurrency.NewSession(client, concurrency.WithTTL(10))
election := concurrency.NewElection(session, "/my-service/leader")
election.Campaign(ctx, "node-1") // blocks until this node leads
The lease length is the trade-off. If the leader dies without resigning, the lease runs out and someone else takes over. Too short and a slow network causes false failovers, too long and an actual crash means a long gap without a leader.
Patterns I rely on
Idempotency
Messages arrive more than once. A client times out and retries, but the first request may have succeeded. If the handler isn’t idempotent, the same payment is charged twice.
The fix is a key per request, and the handler records which keys it already applied. Crystal does this for every write. Each client has a session with a sequence number, the replicated state machine remembers the last number per session, and a retried write is answered from that record instead of being applied twice. The integration suite checks exactly that over HTTP.
Circuit breakers
If a downstream service is failing, more requests make it worse. Your own connection pool fills up and the failing service gets even more load. A circuit breaker counts failures and, past a threshold, fails fast without calling the service. After a cooldown it lets a few requests through to test whether the service is back.
CLOSED ── too many errors ──▶ OPEN ── cooldown ──▶ HALF-OPEN
▲ │
└──────────── test requests succeed ─────────────────┘
Sagas
A monolith wraps a multi-step change in one database transaction. Across services there’s no single database to do that. A saga splits the change into local transactions, one per service, and every step that can fail later gets a compensating step that undoes it in business terms.
Buying a ticket in Loom works this way, and no service runs the flow. The orders service creates the order and publishes an event on Kafka. The tickets service reserves the ticket, the expiration service schedules a timeout, and the payments service waits for the payment. If no payment arrives in time, the expiration event triggers the compensation. The order is cancelled and the ticket is released.
That’s more complicated than BEGIN and COMMIT, and that complexity is the price of distribution. If an operation fits in one database, I keep it there.
When to distribute
I’d distribute when one of three things is true. One machine can’t carry the load. One machine is a single point of failure you can’t accept. Or several teams need to deploy independently. If none of them holds, a well-structured monolith is simpler to build, debug and run.
Kubernetes and Kafka come after that decision. The work is knowing which failures you accepted and designing for them, so that the system fails gracefully, recovers by itself, and never loses data it acknowledged.