Back to articles

etcd: what it stores and why you should care

etcd holds the only copy of everything your cluster knows, and most engineers never look at it until it stops accepting writes. This article explains how Kubernetes lays out and versions its data there, why quorum dictates three or five members, what the storage quota and disk latency do to a control plane, and how backup and restore work in practice.

Every object in a Kubernetes cluster exists in exactly one place: etcd. The API server is the only component that talks to it, and everything else, from the scheduler to kubectl, sees the cluster through the API server. So when etcd is slow, the whole control plane is slow, and when etcd refuses writes, nothing in the cluster can change.

Who this is for: engineers who run their own control plane, and engineers on managed Kubernetes who want to know what their provider is doing for them and where the limits still reach them.

What you walk away with: a model of how Kubernetes data is laid out and versioned in etcd, why quorum dictates three or five members, the storage quota and the alarm that stops all writes, why disk latency is the number to watch, and a backup and restore procedure with its caveats.

Versions: Kubernetes v1.37 and etcd v3.7, the version kubeadm v1.37 deploys by default (3.7.0). Check which etcd version your distribution ships; the Kubernetes documentation gives 3.4.29+ and 3.5.11+ as the minimum recommended versions for production.

What is in there

etcd is a key-value store with a flat, sorted keyspace. The API server writes every object under a prefix, /registry by default (the --etcd-prefix flag), in the form:

/registry/<resource>/<namespace>/<name>
/registry/secrets/default/db-credentials
/registry/pods/kube-system/coredns-7c65d6cfc9-abcde
/registry/namespaces/default
/registry/<group>/<plural>/<namespace>/<name>      (custom resources)

Cluster-scoped resources have no namespace segment. Built-in resources use the lower-cased resource name without the API group, with a few historical exceptions compiled into the API server (nodes are stored under /registry/minions, Services under /registry/services/specs). Custom resources include their API group, as do APIServices and CustomResourceDefinitions. You can list the keys from a control-plane node:

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \
  --cacert=<trusted-ca-file> --cert=<cert-file> --key=<key-file> \
  get /registry --prefix --keys-only | head -50

On a kubeadm cluster the three files are in the etcd static pod's manifest. etcdctl and etcdutl are only in that pod's shell-less image: use kubectl -n kube-system exec etcd-<node> -- etcdctl …, pipes on your side. The values are the serialised objects, protobuf for built-in resources and JSON for custom resources; anyone who can read etcd can read every Secret in the cluster, which is why access to etcd and to its backups is equivalent to cluster-admin.

Because the keyspace is sorted, "list all pods in namespace X" is one range read over a prefix. There are no secondary indexes: a list filtered by label reads the whole prefix and the API server filters afterwards. The API server's watch cache serves many of those reads without touching etcd.

Size per object is bounded twice. The API server's validation caps the data of a Secret or a ConfigMap at 1 MiB. For everything else, a large custom resource for example, the ceiling is etcd's maximum request size, 1.5 MiB by default (--max-request-bytes).

Revisions: where resourceVersion comes from

etcd does not overwrite values. Its data model is multi-version: the store has a single counter, the revision, which starts at 1 and increases with every write transaction anywhere in the keyspace. Each write stores a new version of the key tagged with that revision, and the old versions stay readable until they are compacted.

This is the origin of metadata.resourceVersion. It is the etcd revision at which the object was last modified, exposed as an opaque string. Three Kubernetes behaviours rest on it:

History is also what makes etcd grow: an object updated every few seconds, such as a Lease, leaves a trail of old versions.

Compaction and defragmentation

Compaction discards history older than a given revision. In a Kubernetes cluster the API server drives it: --etcd-compaction-interval defaults to 5 minutes, and 0 disables the API server's compaction requests. etcd has its own --auto-compaction-retention as well, off by default.

Compaction is the reason a client sometimes receives 410 Gone with the message that a resource version is too old: it asked for a revision that has been compacted away. The API documentation puts the retained history at five minutes by default. Clients are expected to re-list and start a new watch.

Compaction frees space inside the database file without shrinking it. The freed pages are reusable by etcd but still occupied on disk, and the file's size is what counts against the quota described below. Defragmentation rewrites the file to release that space:

etcdctl defrag --cluster          # every member, online
etcdutl defrag --data-dir <dir>   # one member, while etcd is stopped

Defragmenting a live member blocks reads and writes on that member while it rebuilds, and the request is not replicated: each member must be done separately, so do one at a time. The Kubernetes documentation calls it an expensive operation to run as rarely as possible and recommends a tool such as etcd-defrag, whose README lists defragmenting the leader last among its rules.

The quota and the NOSPACE alarm

etcd enforces a limit on the size of its backend database, set with --quota-backend-bytes. The default is 2 GiB, and 8 GiB is the suggested maximum for normal environments; etcd warns at startup above that.

When any member's database exceeds the quota, etcd raises a cluster-wide NOSPACE alarm and the cluster goes into a maintenance mode that accepts only reads and deletes. Writes fail with mvcc: database space exceeded. For Kubernetes that means no new pods, no status updates, no Lease renewals. The alarm does not clear by itself, even after space is freed.

The documented recovery is three steps, in this order:

# 1. Compact history up to the current revision
rev=$(etcdctl endpoint status --write-out=json | egrep -o '"revision":[0-9]*' | egrep -o '[0-9].*')
etcdctl compact "$rev"

# 2. Defragment every member
etcdctl defrag --cluster

# 3. Disarm the alarm
etcdctl alarm disarm

etcdctl alarm list shows the alarm and etcdctl endpoint status --write-out=table the database size per member. To see it coming, watch two metrics together: etcd_mvcc_db_total_size_in_bytes is the file size including free space awaiting defragmentation, and etcd_mvcc_db_total_size_in_use_in_bytes is what is in use after compaction. A large gap means a defragmentation will help. When both approach the quota, the etcd documentation says a history compaction is required; where the API server already compacts every five minutes, that leaves shrinking the data or raising the quota.

What fills it

The usual causes: a controller that creates objects and never deletes them, large custom resources updated at high frequency, many big ConfigMaps or Secrets (Helm 3 stores every release revision as a Secret by default), and events.

Events are objects in etcd too. The API server gives them a time to live, --event-ttl, 1 hour by default, so they expire on their own; a cluster in a crash loop can still write them faster than they expire. The API server's --etcd-servers-overrides flag can send a resource such as /events to a separate etcd cluster, so that event churn cannot fill or slow the database that holds everything else. Reading events well is covered in the events guide.

Raft and quorum: why three or five

etcd replicates with the Raft consensus protocol. One member is the leader; every write goes through it, and a write is committed once a majority of members, the quorum, has persisted it. For n members, quorum is (n/2)+1, rounded down.

Members Quorum Failures tolerated
1 1 0
2 2 0
3 2 1
4 3 1
5 3 2
6 4 2
7 4 3

A fourth member raises the quorum without raising the number of failures you survive. Two members are worse than one: either failing stops the cluster.

More members are not faster: the etcd FAQ says write performance suffers in larger clusters and suggests no more than seven. The Kubernetes documentation recommends five for production: with five you can take one member down for maintenance and still survive an unplanned failure. With three, every planned restart leaves you one fault from losing quorum.

When quorum is lost, etcd stops accepting writes. It does not split into two clusters that both accept changes. If a majority of members is gone for good, the only way back is disaster recovery from a snapshot.

The leader sends heartbeats every 100 ms by default, and a follower that hears nothing for the election timeout, 1000 ms by default, starts an election. That is why stretching one etcd cluster across distant sites is delicate: the documentation wants the election timeout at least ten times the round-trip time between members.

Why disk latency is the number to watch

A member may not acknowledge a write until it has appended it to its write-ahead log and called fsync. Every committed write therefore waits for the fsync of a majority of members. The etcd FAQ goes further: disk latency is part of leader liveness. A leader whose disk stalls cannot commit anything, and a slow enough disk leads to late heartbeats and elections.

Two histograms tell you whether the disk is good enough, with thresholds from the etcd documentation:

Metric What it measures Guidance
etcd_disk_wal_fsync_duration_seconds Time to fsync the write-ahead log p99 below 10 ms
etcd_disk_backend_commit_duration_seconds Time to commit a batch to the database file p99 below 25 ms

Alongside them, watch whether each member has a leader and how often the leader changes (etcd_server_has_leader, etcd_server_leader_changes_seen_total). The etcd documentation reads rapid leader changes as a sign of network trouble or excessive load. In the etcd log the same problem appears as two warnings (wording from the v3.7.0 source; the FAQ still quotes older text): apply request took too long, for a request slower than 100 ms, and leader failed to send out heartbeat on time.

The API server reports how long its own etcd calls take in etcd_request_duration_seconds, the view to use when you cannot reach etcd's metrics.

So: give etcd SSD-backed storage and do not share its disk with workloads that write heavily.

Backup and restore

A snapshot is a consistent copy of the keyspace from one member:

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \
  --cacert=<trusted-ca-file> --cert=<cert-file> --key=<key-file> \
  snapshot save /backup/etcd-$(date +%F).db

etcdutl snapshot status /backup/etcd-2026-11-15.db --write-out=table

etcdctl talks to a running cluster over the network; etcdutl works on files. From etcd v3.6 the split is enforced: snapshots are taken with etcdctl and can only be inspected and restored with etcdutl. Scripts that call etcdctl snapshot restore stop working there, and in v3.7.0 etcdctl snapshot has only the save subcommand.

A snapshot contains every Secret in whatever form it is stored. If you use encryption at rest (next section), the snapshot holds ciphertext and is useless without the keys, so the encryption configuration has to be backed up too, and stored somewhere else.

Restore creates a new cluster:

etcdutl snapshot restore snapshot.db \
  --bump-revision 1000000000 --mark-compacted \
  --data-dir /var/lib/etcd-restored

In kubeadm's etcd container only /var/lib/etcd and the certificates are host paths: a save to /backup fails, and a restore to /var/lib/etcd-restored succeeds inside the container and is lost.

Three things to know before you need it:

A restore returns the cluster's desired state to the moment of the snapshot. Objects created since are gone from the API; objects deleted since come back.

Encryption at rest

By default the API server writes resources to etcd unencrypted, Secrets included; base64 in a Secret manifest is an encoding, not protection. Encryption at rest is configured in the API server, not in etcd, with a file passed through --encryption-provider-config:

apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
  - resources:
      - secrets
    providers:
      - secretbox:
          keys:
            - name: key1
              secret: <base64-encoded 32-byte key>
      - identity: {}

The first provider in the list encrypts new writes; the listed providers are tried in order for reads, and the trailing identity lets the API server still read what is not yet encrypted. Existing Secrets stay unencrypted until they are rewritten, so follow up with kubectl get secrets -A -o json | kubectl replace -f -. The old plaintext stays in the database file, and in snapshots, until compaction and defragmentation. With a local provider such as secretbox, the key sits in a file on the control-plane node, next to the data it protects: it defends against a stolen backup or disk, not against someone on the node. The kms v2 provider, stable since v1.29, keeps the key-encryption key in an external key management service.

To check that it works, read a Secret straight from etcd and look for the k8s:enc:secretbox:v1: prefix in place of readable content:

etcdctl get /registry/secrets/default/<name> | hexdump -C | head

What a managed service hides from you

On a managed control plane you do not operate etcd; the provider does.

That does not remove the limits. The database still has a size ceiling, object size limits still apply, and a controller that floods the API with writes still slows the control plane. What changes is visibility and control: expect API server metrics such as etcd_request_duration_seconds and apiserver_storage_size_bytes in place of etcd's own, and no etcdctl. What the provider exposes, and whether its etcd backups can be restored at your request, differs per service: read its documentation and do not assume. A backup you control has to work at the API level, and keeping the desired state in Git covers a large part of the same need.

Summary

etcd holds the cluster's only copy of the truth as versioned keys under /registry. The revision counter makes watches and optimistic concurrency possible, and the history behind it is what compaction trims and defragmentation reclaims. A full database stops all writes until someone compacts, defragments and disarms the alarm. Quorum makes three members the minimum and five the comfortable number, and disk fsync latency decides how stable everything is. A backup is only as good as the restore you have practised.