One order, many machines A client places an order. The order service saves it in a database, and shipping has to hear about it. Each box is a different process, often on a different machine, talking over a network.
In one process, a call either works or fails. Between processes there is a third outcome: no answer at all. Almost every pattern in this lesson exists to deal with that.
A timeout means "I do not know" The client waits, then times out. Maybe the request never arrived. Maybe the order was saved and the reply was lost. Maybe the service is just slow and will finish later. The client cannot tell these apart, and a slow machine looks exactly like a dead one.
Networks lose, delay, reorder and duplicate messages. Parts of the system fail while others keep running. This is partial failure, and it is normal, not rare.
Watch out Never treat a timeout as "it did not happen". Design every write so that asking again is safe.
An idempotency key makes the retry safe The client retries, and sends the same Idempotency-Key header both times. The service stores the key with the result of the first attempt. When the key comes again, it returns that stored result instead of creating a second order.
An operation is idempotent when doing it twice has the same effect as once. Setting a value is naturally idempotent; adding to it is not. Messages from a queue need the same treatment, since a consumer can see one message twice.
Key idea Store the key in the same transaction as the write, with a unique constraint. Then two retries racing each other still create only one order.
An async replica is a little behind The primary copies each change to a replica in the background. Reads go to the replica to take load off the primary. The copy usually lands within milliseconds, but under load or after a network hiccup it can trail by seconds.
The client opens "My orders" right after ordering. The read hits the replica, which has not received order 42 yet, and the order seems to be missing. This gap is replication lag.
Key idea Measure lag on every replica and alert on it. Lag is the window in which readers can see old data.
Consistency models say what a reader may see Strong consistency (linearizability): every read sees the latest completed write, as if there were one copy. Read-your-writes: you always see your own writes, though others may see them later. Eventual consistency: if writes stop, all copies converge, but a read can return old data meanwhile.
Our bug breaks read-your-writes. Common fixes: send a user's reads to the primary for a short time after they write, or remember the write's log position and read from a replica only once it has caught up to it.
Key idea Ask "which reads must be fresh?" Payments and stock checks usually need the primary. Product lists and dashboards are fine with eventual consistency.
CAP and PACELC in plain words CAP: when a network partition splits the copies, a system must choose. Either it refuses some requests to stay consistent, or it keeps answering and some answers may be stale. You cannot have both during a partition, and partitions do happen.
PACELC adds the everyday case: Else, when there is no partition, you still trade Latency against Consistency. Waiting for a replica to confirm every write is safer and slower. Many databases let you choose per query or per table.
Key idea CAP is about what happens during a failure, not a label for a product. Ask a database what it does on partition and what it trades for latency in normal times.
Consensus uses a majority, a quorum Some decisions need every node to agree: who is the leader, which write came first. Consensus algorithms such as Raft (used by etcd) and Paxos commit a value once a majority of nodes has it. With 3 nodes, 2 is a majority; with 5, it is 3.
Two separate majorities always share a node, so two sides of a partition cannot both decide. The side without a majority stops taking writes. This is the consistent choice from CAP.
Key idea Do not build your own consensus. Use etcd, ZooKeeper, or a database with built-in leader election, and run an odd number of members.
Clocks drift, so they cannot order events Both order service replicas update order 42 a few milliseconds apart. Each stamps the write with its own clock and the database keeps the later timestamp. But the clocks differ: one runs a little ahead, and NTP can even step a clock backwards. The real last write loses.
Wall-clock time is fine for logs and rough durations. To order changes to the same thing, use a counter that one place hands out: a version column, a sequence number, or a log offset. A write carries the version it read, and fails if it changed meanwhile.
Watch out Kubernetes does this with resourceVersion. An update based on an old version gets 409 Conflict, and the client reads again and retries.
Writing to the DB and the queue is not atomic The order service saves order 42, then publishes OrderCreated to the queue. If the Pod crashes between the two, the order exists but shipping never hears about it. If it publishes first and the database transaction then fails, shipping gets an order that does not exist.
A database and a message broker do not share one transaction. Two separate writes from one service can always end up half done.
Watch out Look for code that commits to a database and then calls a broker or another API. Each such place can lose or invent events on a crash.
The outbox pattern: one transaction, then relay Instead, the service writes the order row and an outbox row (the OrderCreated event) in the same local transaction. Both commit or neither does. A separate relay reads new outbox rows, by polling or by reading the database change log (for example with Debezium), and publishes them.
The relay can crash after publishing but before marking the row sent, so it may publish twice. Delivery is at-least-once, and the shipping consumer dedupes by event id. End-to-end exactly-once is an illusion; what you get is at-least-once delivery plus idempotent handling.
Key idea Give every event a unique id, and have consumers record processed ids in the same transaction as their own change.
Assume the network lies, and design for it Timeouts mean "unknown", so make writes idempotent. Replicas lag, so decide which reads need fresh data. During a partition you pick consistency or availability; in normal times, latency or consistency.
Let a quorum system (such as etcd) decide leaders. Order changes with versions, not clocks. And when a write and a message must both happen, use an outbox and idempotent consumers.