Deployments vs. StatefulSets

Difficulty: Intermediate

Question

When would you use a Deployment versus a StatefulSet in Kubernetes, and what guarantees does a StatefulSet give that a Deployment does not?

Answer

Use a Deployment for stateless workloads where every replica is interchangeable, like REST APIs and web frontends. Use a StatefulSet when each replica needs a stable identity: a fixed name (db-0, db-1), a stable DNS hostname, and its own persistent volume that follows it across restarts, which is what databases, Kafka and ZooKeeper need.

What a Deployment gives you

A Deployment manages ReplicaSets, which manage Pods. Pods get random suffixes (api-7d9f8c-x2kqp), are created and deleted in any order, and if they use a PersistentVolumeClaim it is usually shared (or there is none at all). That is ideal for a payment API at a company like Razorpay: any Pod can serve any request, so you can scale from 3 to 30 replicas, roll out with RollingUpdate, and let a Service load-balance across them.

What a StatefulSet adds

  • Stable, ordinal names: Pods are <name>-0 through <name>-(N-1). A replacement for mysql-1 is again called mysql-1.
  • Stable DNS: with a headless Service, each Pod gets mysql-1.mysql.default.svc.cluster.local, so a replica can always find the primary at mysql-0.
  • Per-Pod storage: volumeClaimTemplates creates one PVC per Pod (data-mysql-0, data-mysql-1). When mysql-1 is rescheduled to another node, it reattaches the same PVC.
  • Ordering: with the default podManagementPolicy: OrderedReady, Pods start one by one (0, then 1, then 2) and are removed in reverse. Rolling updates also go from the highest ordinal down. You can switch to Parallel when ordering is not needed.

Side-by-side comparison

AspectDeploymentStatefulSet
Pod namesRandom hash suffixOrdinal: app-0, app-1
StorageShared PVC or noneOne PVC per Pod via volumeClaimTemplates
Network identityThrough Service onlyStable per-Pod DNS via headless Service
Scaling orderAny order, in parallelOrdered by default, reverse on scale down
Typical useAPIs, web apps, workersDatabases, Kafka, Elasticsearch, etcd

Gotchas and real-world advice

  • Deleting or scaling down a StatefulSet does not delete its PVCs by default, to protect data. Since v1.32 (GA) you can control this with persistentVolumeClaimRetentionPolicy.
  • A StatefulSet does not make your app replicate data. Kubernetes gives identity and storage; the database (MySQL group replication, Postgres streaming replication) still handles replication. This is why most teams use an operator such as CloudNativePG or Strimzi for Kafka rather than a raw StatefulSet.
  • If a node becomes unreachable, StatefulSet Pods are not instantly replaced, because Kubernetes must avoid two Pods with the same identity writing to one volume.
  • Many teams still prefer a managed database (RDS, Cloud SQL) over running stateful systems in the cluster. Saying this shows judgment.

Code examples

StatefulSet with per-Pod storage and a headless Service

apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  clusterIP: None          # headless: gives each Pod its own DNS record
  selector:
    app: postgres
  ports:
    - port: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres    # must match the headless Service
  replicas: 3
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:16
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:    # one PVC per Pod: data-postgres-0, -1, -2
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 20Gi

Each replica gets its own 20Gi volume and a DNS name like postgres-0.postgres. Replication between them is still the job of Postgres or an operator.

Key points

  • Deployment = interchangeable replicas; StatefulSet = replicas with a stable name, DNS and volume.
  • StatefulSets require a headless Service referenced by spec.serviceName for per-Pod DNS.
  • volumeClaimTemplates creates one PVC per Pod, and PVCs survive scale-down by default.
  • Default OrderedReady policy starts Pods in order and terminates them in reverse.
  • StatefulSets do not replicate data; the application or an operator does.

Common follow-up questions

  • Why does a StatefulSet need a headless Service?
  • What happens to the PVCs when you scale a StatefulSet from 3 to 1?
  • Would you run your production database on Kubernetes or use a managed service?
  • What is a Kubernetes operator and why is it often used for stateful systems?

Interview tips

  • Frame the answer around identity: name, network and storage. That single word ties the whole answer together.
  • Mention that StatefulSets do not solve replication; it separates candidates who have actually run one.
  • Give one concrete example for each: an order API as a Deployment, Kafka or Postgres as a StatefulSet.

Concepts covered

Deployment, StatefulSet, Stable Network Identity, volumeClaimTemplates, Ordered Rollout

More Kubernetes & Orchestration questions