Deployments vs. StatefulSets

Difficulty: Intermediate

Question

When would you use a Deployment vs. a StatefulSet?

Answer

- **Deployment**: Used for **stateless** applications (like web servers). Any Pod is interchangeable. Updates are rolling, and Pod names are random (e.g., `web-abcd-123`). Scaling is fast and non-sequential.

- **StatefulSet**: Used for **stateful** applications (like databases or Kafka). Each Pod has a stable, unique network identifier and persistent storage that follows it (even if deleted/recreated). Pods are created and deleted in a strict, sequential order (e.g., `db-0`, `db-1`, `db-2`).

**Rule of thumb**: Use Deployment for almost everything. Use StatefulSet only if you need stable hostnames or dedicated storage per pod.

Code examples

Deployment Template

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:alpine

This creates 3 identical, interchangeable pods.

Key points

Concepts covered

Deployment, StatefulSet, Stateless, Sticky Identity