Skip to main content
App basics: HTTP, auth, dataLesson 10 of 11

Distributed Systems Basics

On one machine, a function call either returns or throws. Across a network there is a third answer: silence. Follow one order through client → order service → database and replica → outbox → queue → shipping, and meet the problems every cloud system has, with the plain patterns that handle them.

What we knowWaiting
Consistency—
Ordering by—
Delivery—
DatabaseClientPlaces an orderOrder service2 replicasDB primaryTakes writesDB replicaAsync copyOutbox relayPolls or reads the logMessage queueAt-least-onceShippingConsumerPOST /ordersINSERT
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Why a timeout does not tell you whether something happened
  • Idempotency keys make retries safe
  • Replication lag, consistency models, CAP and PACELC
  • Why clocks cannot order events, and what to use instead
  • The outbox pattern for a database write plus a message

Problem → pattern​

You meetWhy it happensPattern
Timeout, unsure if the write happenedLost reply, slow node, partial failureIdempotency key, safe retries with backoff
Duplicate messagesAt-least-once delivery, retries after crashesEvent ids, idempotent consumers
Just-written data missing on readAsync replication lagRead-your-writes: read from primary, or wait for the replica to catch up
Two sides of a split both accept writesNo majority ruleQuorum-based consensus (Raft in etcd), odd member count
Newer update lostOrdering by wall-clock timestampsVersion numbers, optimistic concurrency (409 Conflict)
Order saved but event never sentDual write to DB and brokerTransactional outbox plus relay
ModelA read may returnTypical use
Strong (linearizable)Only the latest completed writeLeader election, locks, balances
Read-your-writesYour own writes for sure; others' maybe laterUser profile, "my orders"
EventualOld data for a while; copies convergeCaches, search indexes, feeds

Try it yourself​

# Optimistic concurrency in Kubernetes: an update based on an old resourceVersion fails
kubectl create configmap demo --from-literal=count=1
kubectl get configmap demo -o yaml > old.yaml # remembers the current resourceVersion
kubectl patch configmap demo -p '{"data":{"count":"2"}}' # someone else changes it
kubectl replace -f old.yaml # Conflict: the object has been modified
kubectl delete configmap demo

# Replication lag in PostgreSQL
psql -h primary -c "SELECT client_addr, state, replay_lag FROM pg_stat_replication;"
psql -h replica -c "SELECT now() - pg_last_xact_replay_timestamp() AS lag;"

# How far is this machine's clock off? (chrony or systemd-timesyncd)
chronyc tracking # "System time" shows the offset from NTP
timedatectl # "System clock synchronized: yes/no"

# A retry with an idempotency key (header name depends on the API)
curl -X POST https://api.example.com/orders \
-H 'Idempotency-Key: 6f1c2a9e-order-42' -d '{"item":"book"}'

Check yourself

  1. A call to the payment service times out after 10 seconds. What do you actually know?
  2. A user changes their name, reloads the page and sees the old name. Reads go to an async replica. Which guarantee is missing?
  3. A service commits an order to the database, then publishes an event to Kafka. Sometimes shipping never sees the order. What is the robust fix?

Further reading: Transactional outbox (microservices.io) · Consistency models (Jepsen) · Raft consensus · Kubernetes resource versions · Control Plane HA and etcd Quorum · How a Message Queue Decouples Services · What Happens When an App Saves to a Database