StatefulSets and PersistentVolumes for Stateful Java Services
Fast recall
Quick revision
Core answer
postgres-0 remains postgres-0), stable per-replica storage (each pod gets its own PVC via volumeClaimTemplates), and ordered operations. Databases, Kafka, Elasticsearch need this. Most Spring Boot services are stateless and belong on Deployments — even those that use a database. PersistentVolumes provide durable storage provisioned dynamically via StorageClass and survive Pod deletion.Whether you know when NOT to use StatefulSets (most Spring Boot apps are stateless)
Running a database as a Deployment — random pod names, shared PVC or no PVC, split-brain or data loss; or deleting a StatefulSet and expecting PVCs to go with it
Explain the three StatefulSet guarantees (stable ID, stable storage, ordered operations), when managed DBs (RDS, Cloud SQL) are better, VolumeClaimTemplates, headless Services, and `WaitForFirstConsumer` binding mode
Interview-ready guide
Interview answer
Persistent Volumes and StatefulSets together solve running stateful workloads in Kubernetes — databases or message brokers that need durable storage and stable network identity across Pod restarts.
Persistent Volumes — storage abstraction
- A PersistentVolumeClaim (PVC) requests storage by size and access mode (
ReadWriteOnce,ReadWriteMany); Kubernetes binds it to a PersistentVolume either pre-provisioned by an admin or dynamically created by a StorageClass. ReadWriteOncemeans only one node can mount the volume at a time — the constraint for most block storage (AWS EBS, GCE Persistent Disk); a StatefulSet with 3 replicas andReadWriteOncemeans each replica gets its own PVC, not one shared.- The PVC lifecycle is independent of the Pod — delete the Pod or even the StatefulSet and the PVC (and the data) survive; explicitly delete the PVC to release storage; this is intentional for durability.
StatefulSet — ordered, stable pods
- StatefulSets give each pod a stable identity:
db-0, db-1, db-2; the DNS name fordb-0isdb-0.db-service.namespace.svc.cluster.local— this stability is what PostgreSQL, Kafka, Elasticsearch require for cluster membership and leader election. - StatefulSets scale and update in order:
db-0starts beforedb-1; during a rolling updatedb-2updates first, thendb-1, thendb-0— this ensures the primary (usuallydb-0) is updated last, minimizing failover window. volumeClaimTemplatesautomatically creates a PVC for each pod;db-0getsdata-db-0and restartingdb-0always reattaches to the same volume.
When to use StatefulSets
- Use Deployments for stateless Spring Boot services — simpler, roll back more cleanly, no PVCs needed.
- Use StatefulSets for databases (if running in Kubernetes at all), Kafka brokers, Elasticsearch nodes, or any process requiring stable network identity or per-instance durable storage.
- Most teams run databases outside Kubernetes on managed services (RDS, Cloud Spanner) — the operational complexity of StatefulSets and storage is worth avoiding when a managed service handles it.
Operational gotchas
- Scaling down a StatefulSet removes pods but leaves PVCs — orphaned disks accumulate cloud cost over time; add a cleanup runbook or use an operator.
- Use
volumeBindingMode: WaitForFirstConsumeron the StorageClass so volumes are provisioned in the same availability zone as the scheduled pod. - Replication is not backup — a
DROP TABLEreplicates instantly; schedulepg_dumpor snapshots stored in a different region/account for real durability.
StatefulSets and PersistentVolumes for Stateful Java Services
Direct Answer
StatefulSets are for workloads where each replica is not interchangeable — stable network identity (postgres-0 remains postgres-0), stable per-replica storage (each pod gets its own PVC via volumeClaimTemplates), and ordered operations. Databases, Kafka, Elasticsearch need this. Most Spring Boot services are stateless and belong on Deployments — even those that use a database. PersistentVolumes provide durable storage provisioned dynamically via StorageClass and survive Pod deletion.
Common Mistakes & Pitfalls
Running a database as a Deployment — random pod names, shared PVC or no PVC, split-brain or data loss; or deleting a StatefulSet and expecting PVCs to go with it
Check your understanding
Answer these without looking back at the article.
- What is the difference between a PersistentVolume and a PersistentVolumeClaim?
- How do you back up a PostgreSQL database running in a Kubernetes StatefulSet?
- What is a StorageClass and how does dynamic volume provisioning work?
- Why would you use a Kubernetes operator instead of a plain StatefulSet for a database?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.