Command Palette

Search for a command to run...

Unit 0.1 · Foundations of State

Why State Is Hard: Durability, Replication, Consistency

What 'stateful' really means, how data survives crashes, why we copy it, and the trade-offs (CAP, PACELC, quorums) that every distributed data system makes.

Beginner 35 min 2 lab steps 2 failure drills

Start here

The mental model

A stateless web server is like a cashier: if one goes home, another takes over and no customer notices. A database is like the shop's ledger: if it burns, the shop doesn't just lose a worker, it loses its memory. Everything about running stateful systems comes from that difference.

So we do three things. We write the ledger in ink before saying 'done' (DURABILITY). We keep photocopies in other buildings (REPLICATION). And we decide what to do when the copies disagree for a moment, whether to wait for them to agree or to answer from whichever copy is nearest (CONSISTENCY). Every system in this course is a different set of answers to those three questions.

Go deeper

How it works inside

01Durability: when is a write really saved?

When a program writes to a file, the data first lands in the operating system's page cache in RAM, not on disk. If the machine loses power, it's gone. fsync() forces it to the physical device and waits (Linux course, disk and storage). Databases make a write durable by appending it to a WRITE-AHEAD LOG (WAL; Kafka calls it the log, Redis calls it the AOF) and fsyncing that log before acknowledging the client. The real data files are updated later, and after a crash the log is replayed.

fsync is slow (milliseconds on network disks like EBS), so systems let you trade safety for speed: Postgres synchronous_commit, Redis appendfsync everysec, Kafka relying on replication instead of per-message fsync. Knowing where each system sits on that dial is half of operating it.

Durability: when is a write really saved?diagram
Rendering diagram…

02Replication: copies on other machines

One machine can always die, so data is copied to others. LEADER–FOLLOWER (primary–replica) replication is the most common: all writes go to one leader, which streams its log to followers (Postgres, MySQL, Redis, Kafka per partition). MULTI-LEADER and LEADERLESS designs (Cassandra, DynamoDB) accept writes anywhere and reconcile later.

SYNCHRONOUS replication waits for a follower to confirm before acknowledging the client: no data loss if the leader dies, but every write pays a network round-trip and a slow follower slows everyone. ASYNCHRONOUS replication acknowledges immediately: fast, but the last few writes can be lost on failover, and followers LAG (reads from them can be stale). Most production setups are 'semi-synchronous': at least one follower must confirm.

03Consistency, CAP, and PACELC

CAP says that during a network PARTITION (nodes can't talk to each other), a system must choose between CONSISTENCY (every read sees the latest write, so it refuses requests it can't guarantee) and AVAILABILITY (it answers anyway, possibly with stale data). You can't opt out of partitions, so the real choice is CP or AP during failures.

PACELC adds the everyday case: Else (no partition), you still trade LATENCY against CONSISTENCY. Waiting for replicas to agree takes time. Postgres with synchronous replicas is PC/EC; DynamoDB by default is PA/EL (eventually consistent reads) with an option for strongly consistent reads.

04Quorums

With N copies, if a write must reach W of them and a read must ask R of them, then W + R > N guarantees the read overlaps at least one copy with the latest write. With N=3, W=2, R=2 you tolerate one node failure for both reads and writes. Kafka's min.insync.replicas=2 with acks=all and a replication factor of 3 is exactly this idea. Leader election uses a MAJORITY quorum too (Raft in etcd and Kafka KRaft), which is why clusters have odd numbers of nodes: 3 nodes tolerate 1 failure, and 4 still tolerate only 1.

Quorumsdiagram
Rendering diagram…

Do it

Hands-on lab

  1. 1

    See the page cache lie to you

    Write 1 GB with and without forcing it to disk. The first dd 'finishes' at RAM speed; conv=fsync makes it wait for the device, which is the real cost databases pay for durability.

    terminal
    $ dd if=/dev/zero of=/tmp/nofsync bs=1M count=1024
    dd if=/dev/zero of=/tmp/withfsync bs=1M count=1024 conv=fsync
    ── expected output ──
    1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.61 s, 1.8 GB/s
    1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.93 s, 218 MB/s
  2. 2

    Measure fsync latency, the number databases care about

    fio with fdatasync=1 and small writes simulates a WAL: each 4 KB write is synced. The p99 latency here is roughly the floor for a durable commit on this disk. Run the same test on a gp3 EBS volume and on local NVMe to see why disk choice matters.

    terminal
    $ fio --name=wal --rw=write --bs=4k --size=64m --fdatasync=1 --filename=/tmp/fio-wal | grep -E 'IOPS|sync.*99.00th'
    ── expected output ──
    write: IOPS=1843, BW=7373KiB/s
    | 99.00th=[ 1336] usec (sync percentiles)

3am practice

Failure drills

Each drill is a real failure mode. Read the scenario and the output, decide what's wrong, then reveal the diagnosis.

Drill #1

Acknowledged, then gone

A team runs Postgres with synchronous_commit = off for speed. The VM is hard-rebooted by the cloud provider. After restart, the last ~200 ms of orders customers were told succeeded are missing.

terminal
$ grep -i 'redo' /var/log/postgresql/postgresql-17-main.log | tail -2
── what you'll see ──
LOG: database system was interrupted; last known up at 2026-09-27 02:14:05 UTC
LOG: redo done at 0/5A3C1F8 system usage: CPU: user: 0.01 s

Drill #2

Two leaders after a network split

A home-made failover script promotes a replica when it can't reach the primary. A network partition separates them; both now accept writes.

terminal
$ psql -h db-a -c 'select count(*) from orders' ; psql -h db-b -c 'select count(*) from orders'
── what you'll see ──
count
-------
18204
count
-------
18191

Decide

How each system in this course answers the three questions

SystemDurabilityReplicationConsistency during failure
PostgreSQLWAL + fsync on commitLeader → followers, async or syncCP with sync replicas; failover can lose async writes
KafkaReplication across brokers; OS flush in backgroundLeader per partition → ISR followersacks=all + min.insync.replicas = CP-ish; unclean election = AP
RedisOptional RDB snapshots / AOFAsync primary → replicasAP-ish: acknowledged writes can be lost on failover
OpenSearchTranslog + fsync per request (default)Primary shard → replica shardsPrimary-based; red/yellow health signals missing copies

The bigger picture

Connects to

Prove it

Interview questions

01

Explain the CAP theorem and what it means in practice.

02

Synchronous vs asynchronous replication?

03

Why do consensus clusters have 3 or 5 nodes, not 4?

0/3 · 0%