Platform Engineering · Lesson 99

etcd in Production: Quorum, Slow Disks, Snapshots, and Restore

Every object in a Kubernetes cluster lives in etcd, and every change to one is a Raft write: the leader appends it to its log, flushes the log to disk, sends it to the followers, waits for a majority to do the same, and only then answers the API server. That makes etcd the one component whose disk latency and member count decide whether the whole control plane can change anything at all. This lesson follows one write through etcd, shows why a slow disk on the leader slows every kubectl command, why losing two of three members makes the cluster read-only, and how to take, check, and restore the snapshot you will one day need.

Difficulty: Advanced · Know this first: Lesson 98, Lesson 43, Lesson 42

Run it as:

When it breaks, I check

One thing to remember

Where the time goes

This bar splits one write, in milliseconds. It is a worked example, not a measurement. Two fsyncs and one round trip are almost all of it on healthy hardware; when the disk is slow, the WAL hop swallows the whole bar.

Try it: members, failures, and the disk

Choose how many members the cluster has and how many are down. Then set the WAL fsync time. The first two decide whether writes can commit at all; the third decides how fast every write is.

quorum = floor(members ÷ 2) + 1 · writes commit while members − down ≥ quorum · p99 fsync should stay under 10 ms

2quorum
1members you can lose
yeswrites commit

Healthy: quorum and a fast disk

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Basic to master

etcd grows in four levels: first what it stores and why a majority matters, then reading its health, then running it well, disks, defragmentation, and backups, and finally planning for the day you restore a control plane.

Basic — The cluster's memory, kept by a majority

At the basic level you know that etcd is where Kubernetes keeps all of its state, and that it is a small cluster of its own, usually three or five members, that agrees on every change by majority vote using Raft.

Terms: member, leader, follower, quorum, Raft log, revision, snapshot.

Rule of thumb: three members survive one failure, five survive two. Even numbers add cost without adding tolerance.

Where it breaks: when a majority is lost, the cluster keeps running what it already runs but cannot change anything: no new pods, no scaling, no rescheduling after a node failure.

Try it: Basic

How many members can a 5-member etcd cluster lose and still accept writes?

Now do it: Run etcd_quorum.py in Go deeper and read the table for 1 to 7 members.

Intermediate — Read etcd's health

At the intermediate level you can talk to etcd with etcdctl and the client certificates, read endpoint status and health, find the leader, and read the metrics that matter.

  • etcd_server_has_leader is 1 on every member; etcd_server_leader_changes_seen_total rises rarely.
  • p99 etcd_disk_wal_fsync_duration_seconds under 10 ms and p99 etcd_disk_backend_commit_duration_seconds under 25 ms.
  • etcd_mvcc_db_total_size_in_bytes well below the quota.

Troubleshooting: on managed Kubernetes (EKS, GKE, AKS) the provider runs etcd and you see it only through API server latency; on kubeadm, OpenShift, and self-managed clusters it is yours.

Try it: Intermediate

Which metric tells you the disk is the problem?

Now do it: Run etcdctl endpoint status --cluster -w table on a test cluster (kind runs etcd as a static pod) and find the leader, the term, and the database size.

Advanced — Disks, space, and backups

At the advanced level you run etcd well. Dedicated local SSD or NVMe, no burst-credit network disks. Members across zones in one region. Compaction running (the API server does it every 5 minutes), defragmentation scheduled member by member, and a quota alert at 70 to 80 percent.

  • Snapshots: etcdctl snapshot save on a schedule, copied off the nodes, encrypted, and checked with etcdutl snapshot status.
  • Secrets at rest: a snapshot holds every Secret; with an API server EncryptionConfiguration they are encrypted inside it, and the snapshot still needs protecting like a credential.
  • Members: replace a failed member with member remove then member add; never add two at once.

Failure scenarios: the NOSPACE alarm stops all writes until you compact, defragment, and run etcdctl alarm disarm; a disk at 100 percent corrupts nothing but stops the member.

Try it: Advanced

Why defragment etcd members one at a time?

Now do it: Write the defragmentation runbook for your cluster: order of members, the command, the check after each, and the alarm disarm step.

Master — Plan the restore you hope never to run

At the master level you can restore a control plane, and you have done it on a test cluster this quarter. A restore is not a rollback of one object; it puts the whole cluster back to the moment of the snapshot. Anything created after it, pods, Secrets, leases, CRD instances, is gone, and controllers will act on the restored state.

  • Procedure: stop every API server, restore the snapshot into a fresh data directory on each member with etcdutl snapshot restore and the cluster's member list, start etcd, then the API servers, then watch controllers reconcile.
  • Recovery objectives: snapshot frequency is your control-plane RPO; the rehearsed procedure time is your RTO.
  • Alternatives: for many teams the better recovery is rebuilding the cluster from Git (GitOps) and restoring only data volumes; etcd restore is the tool for when that is not possible.
  • Capacity: object count and churn (events, leases, Endpoints) drive database size and write rate; watch them as the cluster grows.

Trade-off: five members survive two failures and cost more latency per write; three is right for most clusters with good disks.

Try it: Master

You restore etcd from a snapshot taken at 02:00. It is now 09:00. What should you expect?

Now do it: On a kind cluster: save a snapshot, create a Deployment, restore the snapshot, and confirm the Deployment is gone. Time the whole procedure.

Go deeper

Quorum, fault tolerance, and the space quota are small enough to compute by hand. This script prints the quorum table, which failures a cluster survives, and how close a database is to its quota. Standard library only; python3 etcd_quorum.py.

Quorum and space

# etcd_quorum.py - quorum, fault tolerance, and how close the database is to its quota
def quorum(members):
    return members // 2 + 1

print("members  quorum  members you can lose")
for n in range(1, 8):
    print(f"{n:>7}  {quorum(n):>6}  {n - quorum(n):>20}")

print()
for alive, members in [(3, 3), (2, 3), (1, 3), (3, 5), (2, 5)]:
    ok = alive >= quorum(members)
    print(f"{alive} of {members} alive: {'writes and linearizable reads work' if ok else 'no quorum: writes and linearizable reads fail; only stale serializable reads'}")

# The backend database only shrinks on defragmentation; compaction frees space inside it.
GiB = 1024 ** 3
quota = 2 * GiB                      # --quota-backend-bytes default
size, in_use = 1.9 * GiB, 0.7 * GiB  # etcd_mvcc_db_total_size_in_bytes vs ..._in_use_in_bytes
print()
print(f"db size {size / GiB:.1f} GiB of a {quota / GiB:.0f} GiB quota: {size / quota:.0%} used")
print(f"in use after compaction: {in_use / GiB:.1f} GiB; defragment to give back {(size - in_use) / GiB:.1f} GiB")
print("at 100% of the quota etcd raises the NOSPACE alarm and accepts only reads and deletes")
$ python3 etcd_quorum.py
members  quorum  members you can lose
      1       1                     0
      2       2                     0
      3       2                     1
      4       3                     1
      5       3                     2
      6       4                     2
      7       4                     3

3 of 3 alive: writes and linearizable reads work
2 of 3 alive: writes and linearizable reads work
1 of 3 alive: no quorum: writes and linearizable reads fail; only stale serializable reads
3 of 5 alive: writes and linearizable reads work
2 of 5 alive: no quorum: writes and linearizable reads fail; only stale serializable reads

db size 1.9 GiB of a 2 GiB quota: 95% used
in use after compaction: 0.7 GiB; defragment to give back 1.2 GiB
at 100% of the quota etcd raises the NOSPACE alarm and accepts only reads and deletes

Read it like this: four members tolerate one failure, the same as three; two of five alive is not a quorum; and a 1.9 GiB database with 0.7 GiB in use needs a defragmentation, not a bigger quota.

Health, a snapshot, and a restore (kubeadm layout)

# On a control-plane node of a kubeadm cluster. Paths differ on other distributions.
export ETCDCTL_API=3
E="--endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key"

etcdctl $E endpoint status --cluster -w table     # leader, term, db size per member
etcdctl $E endpoint health --cluster
etcdctl $E alarm list

etcdctl $E snapshot save /var/backups/etcd-$(date +%F-%H%M).db
etcdutl snapshot status /var/backups/etcd-2026-11-22-0200.db -w table

# Defragment one member at a time, then clear a NOSPACE alarm.
etcdctl --endpoints=https://10.0.1.11:2379 --cacert=... --cert=... --key=... defrag
etcdctl $E alarm disarm
# Restore: lab or a real recovery only. This rewinds the whole cluster.
# 1. Stop the API servers (on kubeadm, move kube-apiserver.yaml out of /etc/kubernetes/manifests).
# 2. On each member, restore into a NEW data directory with that member's name and peer URL:
etcdutl snapshot restore /var/backups/etcd-2026-11-22-0200.db \
  --name cp-1 \
  --initial-cluster cp-1=https://10.0.1.11:2380,cp-2=https://10.0.1.12:2380,cp-3=https://10.0.1.13:2380 \
  --initial-advertise-peer-urls https://10.0.1.11:2380 \
  --data-dir /var/lib/etcd-restored
# 3. Point etcd at /var/lib/etcd-restored (the hostPath in etcd.yaml), start etcd on all members,
#    then put the API server manifests back and watch: kubectl get nodes, kubectl get pods -A.

These commands follow the etcd 3.5 and 3.6 tooling, where etcdutl replaces the deprecated etcdctl snapshot status and snapshot restore. They change a control plane: run them first on a disposable cluster, such as kind, where etcd runs as a static pod, and time the restore end to end.

One time it broke

This is a composite of a failure pattern that keeps repeating; the numbers are illustrative. A self-managed cluster ran its three etcd members on network-attached disks with burst credits.

Timeline

  • Mon 14:00A large batch job creates and deletes thousands of pods. etcd write rate triples, and the members spend their disk burst credits.
  • Mon 16:30Credits run out. WAL fsync p99 goes from 3 ms to 400 ms. kubectl takes seconds; leader elections start every few minutes.
  • Mon 16:50Controllers time out renewing their leases and restart. Deployments stop rolling. The batch job is paused and fsync recovers within an hour as credits refill.

What changed after

etcd moved to provisioned-IOPS local disks, alerts were added on p99 WAL fsync above 10 ms and on more than three leader changes an hour, and batch workloads got their own namespace with a pod creation rate limit. The lesson the team wrote down: etcd's disk is the control plane's heartbeat.

If you remember nothing else

Design exercise

Design the etcd layer and its backups for a self-managed production cluster of 400 nodes that must survive the loss of a zone, with a control-plane recovery point of one hour.

Clarifying questions to ask

How many zones are there, and how many members can you place in each? What disks are available? How large is the database today and how fast does it grow? Who can read the snapshots? Has anyone restored one, and how long did it take?

Rough sizing

Say 3 zones, a 1.2 GiB database growing 100 MiB a month, 800 writes a second at peak, and NVMe available on control-plane nodes.

Shape of the answer

Three members, one per zone, on dedicated NVMe, with the default 2 GiB quota raised to 8 GiB and an alert at 70 percent; compaction by the API server every 5 minutes and a weekly defragmentation, member by member. Hourly snapshots copied to encrypted object storage in another region, checked with etcdutl, kept 7 days; a quarterly restore drill on a cloned cluster with the time recorded. Alerts on has_leader, leader changes, fsync p99, and database size.

Trade-offs to name

Five members survive two failures and add a little latency to every write and more to run; three is enough with one per zone. Hourly snapshots bound the loss to an hour and cost storage and care for sensitive copies. A GitOps rebuild may beat an etcd restore for recovery, and it needs every object, including secrets, to be reproducible from outside the cluster.

Saved on this device, in this browser.
Next in this series · Lesson 100
Kubernetes Autoscaling: HPA, VPA, KEDA, and Node Autoscaling

A traffic spike does not meet one autoscaler. It meets a chain of them. Metrics have to be collected, the Horizontal Pod Autoscaler has to do its arithmetic, the scheduler has to find room, a node autoscaler has to buy a machine…

Go to Lesson 100 →

Or browse the full course from the Lessons index.