In Kubernetes, StatefulSets are a special type of workload API object used to manage stateful applications. Unlike a Deployment (which is used for stateless apps), StatefulSets are designed for applications that require stable network identities, persistent storage, and ordered scaling or updates.
There are two main ways to deploy applications in Kubernetes:
- Deployment – for stateless applications
- StatefulSet – for stateful applications
Stateless Applications
A stateless application is an application that does not store any data or state about previous interactions. Each request is processed independently, and no information is saved once the request is completed. This means if the application crashes or restarts, it can continue working without needing to recover any past data.
In Kubernetes, stateless applications are usually deployed using Deployments because:
- Pods can be freely created, destroyed, or replaced without affecting application behavior.
- Load balancers can route requests to any Pod since they all behave the same way.
- Scaling up or down is simple because Pods do not depend on local state.
Stateful Applications
A stateful application is an application that maintains data or state across sessions and requests. Unlike stateless applications, they cannot treat every request as independent because they rely on previously stored information. If such an application crashes or restarts, it must be able to recover its state to continue functioning correctly.
In Kubernetes, stateful applications are deployed using StatefulSets because:
- Each Pod needs a stable, unique identity (name, network ID, and hostname).
- Persistent storage must be attached to Pods so data survives restarts.
- Scaling requires careful handling to ensure data consistency and ordering

Benefits of Using StatefulSets (Active Voice Version)
- Stable and Unique Pod Identity
- Each StatefulSet pod receives a unique, predictable name (e.g.,
mysql-0,mysql-1) and maintains a stable network identity. - This feature helps stateful applications like databases or clustered apps communicate reliably using pod identity.
- Each StatefulSet pod receives a unique, predictable name (e.g.,
- Persistent Storage with Stable Volume
- StatefulSets automatically attach PersistentVolumeClaims (PVCs) to each pod.
- Pods retain their data even after deletion or rescheduling, ensuring persistent storage.
- Ordered Deployment and Scaling
- Kubernetes creates, updates, or deletes pods in sequential order (0 → N).
- This process allows applications to bootstrap predictably and start or stop gracefully.
- Ordered Rolling Updates
- StatefulSets update one pod at a time in a defined sequence, reducing the risk of service disruption.
- This approach benefits applications that cannot tolerate simultaneous changes.
- Consistency for Stateful Applications
- Stateful applications like databases, message queues, and caches depend on stable network identity and storage for replication and quorum.
- StatefulSets maintain a consistent environment for these workloads.
- Easy Recovery
- When a pod fails, StatefulSets recreate it with the same identity and storage, minimizing application disruption.
- This process reduces the need for manual intervention during recovery.
- DNS-Based Service Discovery
- StatefulSet pods use stable DNS names to simplify intra-cluster communication.
- Example:
pod-0.service-name.namespace.svc.cluster.local.
- Ideal for Clustering
- StatefulSets support applications that require replication, leader election, or quorum mechanisms, such as Cassandra, ZooKeeper, or Elasticsearch.
When to Use StatefulSets
StatefulSets in Kubernetes are ideal for deploying stateful applications that require stable, unique network identifiers, persistent storage, and ordered, graceful deployment and scaling. They are suitable for applications like databases, key-value stores, and messaging queues that require consistent identity and storage.
Here is an example of a StatefulSet named web:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.k8s.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
How to Create a StatefulSet in Kubernetes
Here is a step by step tutorial on how to use StatefulSets and some basic operations on StatefulSets.
Create a Nginx StatefulSet Application
Step 1. Create a StatefulSet file. you can do that by entering the following command:
touch example-statefulset.yaml
Step 2. Open this file in a code-editor and write the following code into it:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: codezaza-example-statefulset
annotations:
description: "This is an example statefulset"
spec:
selector:
matchLabels:
app: nginx
serviceName: "codezaza-example-service"
replicas: 3 # remember this, we will have 3 identical pods running
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: myclaim

Step3. Now we have to create a service file and a PersistentVolumeClaim file.
touch example-service.yaml
touch example-persistentVolumeClaim.yaml

Create a Service for the StatefulSet Application
Step 4. Enter the following code into the service file:
apiVersion: v1
kind: Service
metadata:
name: codezaza-example-service
annotations:
description: "this is an example service"
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx

Step 5. Enter the following code into the PersistentVolumeClaim file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 8Gi # This means we are requesting for 8 GB of storage

Now lets apply these changes.
Step 6. Enter the following command in your terminal to create the codezaza-example-statefulset:
kubectl create -f example-statefulset.yaml
This will create our codezaza-example-statefulset, you will get a similar result:

Searching our StatefulSets in our terminal by the command
kubectl get statefulsets
we will find our codezaza-example-statefulset in the list.

Step 7. Enter the following command in your terminal to create the codezaza-example-service.
kubectl apply -f example-service.yaml
this will create a service with the name “codezaza-example-service”
Step 8. Let’s check our pods and services, for getting the list of pods enter the following command in your terminal:
kubectl get pods
for checking the list of services, enter the following command in your terminal:
kubectl get services
This will give you similar output:

Step 9. Now let’s scale up our pods and check if it works! for scaling up the pods to 6 pods, enter the following command:
kubectl scale statefulset codezaza-example-statefulset --replicas=6
This will create 3 more pods and number of pods are now 6, to get list of pods enter the following command:
kubectl get pods
Step 10. Now let’s scale down pods to 3, for that enter the same command, just change the number of replicas back to 3:
kubectl scale statefulset codezaza-example-statefulset --replicas=3
Operations On StatefulSets
Adding a StatefulSet: To add a StatefulSet to your Kubernetes cluster, use the command kubectl create -f [StatefulSet file name], replacing [StatefulSet file name] with the name of your StatefulSet manifest file.
kubectl create -f [StatefulSet file name]
Scale Up / Down
Adjust the number of replicas in the StatefulSet to meet workload requirements. Pods are created or removed in order.
kubectl scale statefulset <name> --replicas=5
Edit
Update the StatefulSet configuration (e.g., image version, environment variables).
kubectl edit statefulset <name>
Delete
Remove the StatefulSet without deleting the associated PersistentVolumeClaims (PVCs), ensuring data remains intact.
kubectl delete statefulset <name>
Rolling Updates
Update Pods in sequence (one at a time) to minimize downtime while ensuring application consistency.
kubectl rollout status statefulset <name>
kubectl rollout undo statefulset <name>
How Stateful Applications Work?
- In stateful applications like MySQL, multiple pods cannot simultaneously read and write data to avoid data inconsistency.
- One pod is designated as the master pod, responsible for writing and changing data, while others are designated as slave pods, only allowed to read data.
- Each pod has its own replica of the data storage, ensuring data isolation and independence.
- Synchronization mechanisms are employed to ensure that all pods have the same data state, with slave pods updating their data storage when the master pod changes data.
- Continuous synchronization is necessary to maintain data consistency among all pods in the stateful application
StatefulSets vs Deployments
| Feature / Aspect | Deployment | StatefulSet |
|---|---|---|
| Purpose | Manages stateless applications where pods are interchangeable. | Manages stateful applications where each pod has a unique identity and stable storage. |
| Pod Identity | Pods are identical and replaceable; names are autogenerated. | Pods have stable, unique identities (name, network identity) across rescheduling. |
| Storage | Typically ephemeral storage; data is lost if pod restarts. | Supports persistent storage via PersistentVolumeClaims; storage is retained even if pods are deleted. |
| Scaling | Simple scaling; new pods are identical and interchangeable. | Pods are scaled in order, respecting ordinal index (0, 1, 2…); useful for databases like MySQL, Cassandra. |
| Pod Management | Pods can be created or deleted in any order. | Pods are created, updated, and deleted in order (0 → N); ensures ordered, predictable deployment. |
| Use Case | Stateless apps like web servers, API services, frontend apps. | Stateful apps like databases, message queues, or any app requiring stable identity or persistent data. |
| Rolling Updates | Rolling updates are straightforward; new ReplicaSet replaces old pods gradually. | Rolling updates are more controlled; StatefulSet updates pods in sequential order to preserve state. |
| Network Identity | Pods get random names and IPs; service is used for discovery. | Each pod gets a stable hostname, which allows direct access and DNS-based discovery. |
| Versioning / Rollback | Deployment maintains revision history and supports easy rollback. | StatefulSet supports limited rolling updates; rollback is more complex because of state persistence. |
| Headless Service | Optional; mostly not needed for stateless apps. | Required to manage network identity and stable DNS entries for each pod. |
| Self-Healing | Recreates pods automatically if they fail; replacement pods are identical. | Recreates pods automatically while preserving identity and storage; ensures consistency of stateful apps. |
| Example Command | kubectl create deployment nginx-deploy --image=nginx | kubectl apply -f mysql-statefulset.yaml |
| Practical Usage | Common in microservices, CI/CD pipelines, stateless backend/frontend apps. | Common for databases, caches (Redis, Cassandra), or apps needing persistent storage. |
Best Practices
- Use StatefulSets only when state is essential – For most workloads, Deployments are simpler and more efficient. Choose StatefulSets only for applications that need stable identities or persistent data.
- Test scaling behavior before production – Stateful applications often require special handling when scaling up or down (e.g., leader election, replication). Always validate this in a staging environment first.
- Combine with appropriate StorageClasses – Ensure PersistentVolumes are provisioned using the right StorageClass so that data is reliable, consistent, and available across Pod restarts or rescheduling.
Common Interview Questions
1. What is a StatefulSet in Kubernetes?
Answer:
“A StatefulSet is a Kubernetes resource that manages stateful applications. Unlike a Deployment, it ensures that each pod has a unique identity and stable, persistent storage. This makes it suitable for applications like databases, message queues, and clustered services where pod identity matters.”
2. When would you use a StatefulSet instead of a Deployment?
Answer:
“I would use a StatefulSet for applications that require stable network identities or persistent data. For example, databases like MySQL, Cassandra, or Redis clusters need each pod to keep its identity and storage even after rescheduling. For stateless apps, I would use a Deployment instead.”
3. How does StatefulSet ensure stable pod identity?
Answer:
“StatefulSet assigns each pod a unique ordinal index, like app-0, app-1, app-2, and creates a stable network identity using a headless service. This allows pods to maintain predictable DNS names and makes communication between pods reliable.”
4. How does StatefulSet handle storage?
Answer:
“StatefulSet uses PersistentVolumeClaims for each pod, which ensures that the data remains intact even if the pod is deleted or rescheduled. Each pod gets its own PVC, so storage is tied to the pod identity and persists across restarts.”
5. Can you perform rolling updates with StatefulSets? How?
Answer:
“Yes, StatefulSets support rolling updates, but they update pods in sequential order based on their ordinal index. Kubernetes updates one pod at a time, waiting for it to become ready before moving to the next. This ensures minimal disruption and consistency for stateful workloads.”
6. How do you scale a StatefulSet?
Answer:
“I can scale a StatefulSet using the command kubectl scale statefulset <name> --replicas=<number>. Pods are added or removed sequentially, maintaining their identities and persistent volumes. This allows predictable scaling of stateful applications.”
7. What is a headless service in StatefulSets?
Answer:
“A headless service doesn’t provide a cluster IP but allows pods to have stable DNS names. StatefulSet uses headless services to give each pod a predictable network identity, which is essential for direct pod-to-pod communication in clustered or stateful applications.”
8. What are the common use cases of StatefulSets?
Answer:
“StatefulSets are commonly used for databases like MySQL, MongoDB, or Cassandra, message queues like RabbitMQ, and applications that require leader election or clustering. Any workload that needs persistent storage and stable pod identity benefits from StatefulSets.”
9. How is StatefulSet different from Deployment?
Answer:
“The main difference is that Deployments are for stateless apps where pods are interchangeable, while StatefulSets are for stateful apps where each pod has a unique identity and persistent storage. StatefulSets also update pods sequentially, whereas Deployments update them in any order.”
10. How do you handle failures in StatefulSets?
Answer:
“When a pod in a StatefulSet fails, Kubernetes recreates it with the same identity and persistent volume. This ensures that the application state is preserved and reduces manual intervention for recovery. I also monitor pod health and use readiness and liveness probes to detect issues early.”

