What guarantees does a StatefulSet provide that a Deployment does not?
Quick Answer
Stable, ordinal pod identity (web-0, web-1) with stable DNS names, ordered and graceful rollout/scaling, and stable per-pod persistent storage that survives rescheduling. That's what stateful, clustered systems need.
Detailed Answer
Deployment pods are interchangeable, get random names, and share nothing per-pod. A StatefulSet gives each pod a stable identity: a fixed ordinal name (db-0, db-1) and, paired with a headless Service, a stable DNS name (db-0.db) that persists across restarts and reschedules. Pods are created/updated/deleted in order (0,1,2 up; reverse down), which matters for quorum systems and primary/replica bootstrapping. Each pod gets its own PersistentVolumeClaim via volumeClaimTemplates, so pod db-0 always reattaches to the same volume — data survives rescheduling. These properties are exactly what databases (PostgreSQL, MySQL), message brokers (Kafka), and distributed stores (Cassandra, Elasticsearch) require. The trade-offs: scaling and updates are slower (ordered), and deleting a StatefulSet doesn't delete its PVCs by default (to protect data). Use Deployments for stateless tiers, StatefulSets when identity and storage must be stable.
Code Example
apiVersion: apps/v1
kind: StatefulSet
metadata: { name: db }
spec:
serviceName: db # headless Service -> db-0.db, db-1.db ...
replicas: 3
volumeClaimTemplates:
- metadata: { name: data }
spec: { accessModes: [ReadWriteOnce], resources: { requests: { storage: 20Gi } } }Interview Tip
Three guarantees: stable identity/DNS, ordered operations, stable per-pod storage. Add the operational gotcha that deleting a StatefulSet keeps the PVCs — a common surprise that shows hands-on experience.