What is the difference between a Deployment, a ReplicaSet, and a StatefulSet?
Quick Answer
A ReplicaSet keeps N identical, interchangeable pods running. A Deployment manages ReplicaSets to give you declarative rollouts and rollbacks. A StatefulSet gives pods stable identities, ordered rollout, and stable storage — for stateful apps.
Detailed Answer
A ReplicaSet is the low-level controller that ensures a set number of identical pod replicas exist; you rarely create one directly. A Deployment sits on top and manages ReplicaSets for you: when you change the pod template, it creates a new ReplicaSet and scales it up while scaling the old one down (rolling update), and keeps history so you can roll back. Deployments are for stateless, interchangeable pods behind a Service. A StatefulSet is for workloads where identity matters: each pod gets a stable ordinal name (db-0, db-1) and DNS identity, is created/updated/deleted in order, and gets its own persistent volume that survives rescheduling. Use StatefulSets for databases, message brokers, and quorum systems; use Deployments for web/API tiers. DaemonSets (one pod per node) and Jobs/CronJobs round out the workload controllers.
Code Example
# Deployment -> ReplicaSet (stateless, interchangeable) # StatefulSet -> stable identity + per-pod PVC (stateful) kubectl get rs # ReplicaSets a Deployment created kubectl rollout history deploy/web # revisions you can roll back to
Interview Tip
Map each to a use case: ReplicaSet=raw replicas (rarely direct), Deployment=stateless rollouts, StatefulSet=stable identity+storage. Interviewers probe whether you'd (wrongly) run a database on a Deployment.