StatefulSet vs Deployment — When to Use Each
Use Deployments for stateless applications where Pods are interchangeable. Use StatefulSets when each Pod needs a stable identity, persistent storage, or ordered lifecycle — such as databases, caches, and distributed systems like Kafka or ZooKeeper.
Detailed Answer
Choosing between a Deployment and a StatefulSet is one of the most common architectural decisions in Kubernetes. The choice depends on whether your application is stateless or stateful.
Use a Deployment When
Your application is stateless — any Pod can handle any request, and Pods are interchangeable:
- Web servers (Nginx, Apache)
- API services
- Workers that pull from a shared queue
- Microservices that store state in external databases
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
spec:
replicas: 5
selector:
matchLabels:
app: api-server
template:
metadata:
labels:
app: api-server
spec:
containers:
- name: api
image: myapp/api:v2.1
ports:
- containerPort: 8080
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"
Deployments create Pods with random suffixes (e.g., api-server-7f8d5b6c4-x9k2p). If a Pod dies, a new one is created with a different name. This is perfectly fine for stateless apps.
Use a StatefulSet When
Your application is stateful — each Pod needs to be distinguishable from the others:
- Databases (MySQL, PostgreSQL, MongoDB)
- Distributed systems (Kafka, ZooKeeper, etcd)
- Caches (Redis Cluster, Memcached)
- Search engines (Elasticsearch)
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: kafka
spec:
serviceName: "kafka-headless"
replicas: 3
selector:
matchLabels:
app: kafka
template:
metadata:
labels:
app: kafka
spec:
containers:
- name: kafka
image: confluentinc/cp-kafka:7.5.0
ports:
- containerPort: 9092
env:
- name: KAFKA_BROKER_ID
valueFrom:
fieldRef:
fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
volumeMounts:
- name: data
mountPath: /var/lib/kafka/data
resources:
requests:
cpu: "500m"
memory: "1Gi"
limits:
cpu: "2"
memory: "4Gi"
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 50Gi
Decision Matrix
| Requirement | Deployment | StatefulSet | |---|---|---| | Interchangeable Pods | Yes | No | | Stable network identity | No | Yes | | Per-Pod persistent storage | No | Yes | | Ordered startup/shutdown | No | Yes | | Fast scaling | Yes | Slower (ordered) | | Rolling updates | Forward order | Reverse order | | PVC cleanup on scale-down | N/A | Manual |
The Gray Areas
Some situations are less clear:
Single-replica databases: You can technically use a Deployment with a PVC for a single database instance. However, a StatefulSet is still preferred because it reattaches the same PVC by name after rescheduling, whereas a Deployment creates a new Pod name each time.
Stateless apps that need stable hostnames: If your app is stateless but needs a predictable DNS name for external registration, a StatefulSet works — but consider whether a regular Service with a fixed ClusterIP might suffice.
Operator-managed workloads: In practice, most teams use Kubernetes operators for databases. These operators create and manage StatefulSets internally, abstracting away much of the complexity.
Operational Considerations
StatefulSets require more care in production:
- PVC lifecycle: Deleting a StatefulSet does not delete its PVCs. You must clean them up manually.
- Scaling speed: Default ordered scaling means each Pod must be Ready before the next starts.
- Update risks: A bad update to a StatefulSet Pod can block the entire rollout since updates proceed one Pod at a time.
- Backup and restore: Stateful data needs its own backup strategy — Kubernetes does not back up PVC contents.
Why Interviewers Ask This
This question tests whether you can make informed architectural decisions about workload types, which is critical for designing production Kubernetes clusters.
Common Follow-Up Questions
Key Takeaways
- Deployments are for stateless apps; StatefulSets are for stateful apps that need stable identity and storage.
- StatefulSets add operational overhead — only use them when your application truly requires the guarantees they provide.
- Many teams use operators (like the MySQL or PostgreSQL operator) that build on StatefulSets to manage database lifecycle.