etcd

Definition

etcd is a distributed, reliable key-value store used as the shared configuration and service discovery backbone for distributed systems. Developed by CoreOS (now part of Red Hat) and hosted by the CNCF, it is the de facto storage backend for Kubernetes.

etcd stores critical data about a distributed system’s state, ensuring consistency across all nodes using the Raft consensus algorithm.

Key Concepts

  • Key-Value Store: Simple map of string keys to byte-array values
  • Raft Consensus: Leader-based consensus for strong consistency
  • Watch/Monitor: Clients can watch for key changes in real-time
  • TTL (Time To Live): Keys can expire automatically
  • Revision: Every write increments a global revision number
  • Lease: Keys can be bound to a lease; key is deleted when lease expires
  • Linearizable Reads: Guaranteed to see the latest write

etcd Architecture

Client → etcd Node (Leader) → Raft Protocol → etcd Nodes (Followers)
                                              → etcd Nodes (Followers)
                                              → etcd Nodes (Followers)
  • Leader handles all read/write requests
  • Followers replicate the leader’s log
  • Raft ensures all nodes agree on the state
  • Quorum: Majority of nodes must agree for a write to succeed

etcd Use Cases

Use Case Description
Kubernetes API storage Stores all cluster state (Pods, Services, ConfigMaps)
Service discovery Services register themselves; clients discover by key lookup
Configuration management Centralized config store for distributed systems
Leader election Distributed leader election via leases/locks
Distributed locks Mutual exclusion across nodes
Metadata store Store application metadata and state

etcd vs Consul vs ZooKeeper

Feature etcd Consul ZooKeeper
Data model Key-value Key-value + DNS Tree
Consensus Raft Raft ZAB
Service discovery Built-in Built-in (primary) Limited
Health checks Watch-based Built-in Watch-based
Multi-datacenter Native Native Complex
Kubernetes Default storage Alternative Alternative
Language Go Go Java

etcd Commands

# Write a key
etcdctl put /mykey "myvalue"

# Read a key
etcdctl get /mykey

# Watch for changes
etcdctl watch /mykey

# Delete a key
etcdctl del /mykey

# List keys with prefix
etcdctl get / --prefix --keys-only

# Compact and defragment
etcdctl compact <revision>
etcdctl defrag
  • Kubernetes — consensus algorithm used by etcd
  • Consul — alternative service mesh and config store
  • ZooKeeper — older distributed coordination system
  • Distributed Systems — etcd is a foundational distributed system component

References