12-Factor App: Statelessness, Scaling, Disposability, and Deployment Strategies

Factor VI: Stateless and “Share Nothing”

  • Problem solved: If an application instance stores user state (such as login sessions or shopping carts) inside its own local memory or local disk, restarting that instance or routing the user’s next request to another replica breaks the user’s session.
  • Mechanism: Processes must be stateless and share nothing directly with peer instances. Any state that must outlive a single request is persisted externally in backing services such as databases or distributed memory caches (Factor IV).
  • No sticky sessions: Applications must not rely on “sticky” sessions (assuming all requests from a given user return to the same instance). Every replica must be able to serve any incoming request.

Factor VII: Export Application as Port Binding

  • Problem solved: Traditional web applications often required installation inside a heavy host-level application server (such as Apache or Tomcat), coupling the app to external runtime infrastructure.
  • Mechanism: A 12-factor application is self-contained: it embeds its own server library and exports itself directly by binding to a port and listening for requests.
  • Kubernetes realization: As illustrated in the lecture’s diagram (slide 15), a caller (Pod X) sends a request to a ClusterIP Service (see Kubernetes Services Ingress and Reverse Proxy), which receives the request and load-balances across ready backend replicas (Pod 1–Pod 4).

Factor VIII: Horizontal Scaling Better Than Vertical Scaling

  • Vertical scaling (scale up): Adding more CPU or RAM to a single server. Hard limit: Scaling cannot exceed the physical boundaries of one machine.
  • Horizontal scaling (scale out): Running additional identical copies (replicas) of the application across multiple servers.
  • Architectural consequences:
    1. One-thread-per-process model: Applications should not try to scale by dynamically spawning many internal threads inside one process.
    2. Externalized state: Instances must remain stateless (Factor VI), or any required shared state must be reachable from anywhere in the cluster.
  • Kubernetes scaling primitives (see Kubernetes Multi-Tenancy Quotas and Autoscaling):
    • Pods declare Requests (guaranteed baseline resources) and Limits (maximum ceiling), plus scale-out/scale-in conditions via HPA (Horizontal Pod Autoscaler).
    • When HPA is not possible or sufficient, Vertical Pod Autoscaler (VPA) adapts a Pod’s CPU/memory allocation, and Cluster Autoscaler adds or removes worker nodes in cloud hyperscalers to match cluster demand and optimize costs.
  • Key exception (when horizontal scaling is NOT appropriate): HPC (High-Performance Computing) applications that rely heavily on multiple tightly coupled threads exchanging data rapidly through shared memory on the same machine.

Factor IX: Disposability and Deployment Strategies

The “Cattle” paradigm and graceful housekeeping

  • Mechanism: Every application instance must be disposable—capable of starting fast and terminating at any moment without affecting users, because other replicas handle traffic or a replacement starts immediately (see “Pets vs. Cattle” in Kubernetes Pods Sidecars and Workload Controllers).
  • Kubernetes lifecycle support:
    • Pods can be deleted and recreated on demand, scaled to variable replica counts, and automatically restarted by LivenessProbe checks when unresponsive.
    • Graceful housekeeping: Before shutting down, instances must close database connections and network resources cleanly and log shutdown activity; any persistent state must reside externally.
  • Load-balanced upgrade trace (slide 18): Behind a Load Balancer (LB), old v1.1 Pods are taken out of rotation one by one while healthy v1.2 Pods join (a -> b -> c -> d), completing the upgrade with no service interruption.

Deployment strategies compared (slide 19)

StrategyHow traffic & versions shiftZero downtime?Real traffic test?Targeted users?Cloud costRollback durationNegative impact on userSetup complexityK8s support
RecreateTerminate all A, then roll out BNoNoNoLow (1/3)Slow (3/3)High (3/3)Minimal (0/3)Vanilla K8s
Ramped (Rolling)Slowly roll out B replacing AYesNoNoLow (1/3)Slow (3/3)Low (1/3)Low (1/3)Vanilla K8s
Blue/GreenRun full B alongside A, then switch trafficYesNoNoHigh (3/3)Instant (0/3)Medium (2/3)Medium (2/3)Vanilla K8s
CanaryRelease B to a small subset of users, then full rolloutYesYesNoLow (1/3)Medium (2/3)Low (1/3)Medium (2/3)Vanilla K8s
A/B TestingRoute a specific user segment (condition) to BYesYesYesLow (1/3)Medium (2/3)Low (1/3)High (3/3)Service Mesh
ShadowMirror real traffic to B alongside A (B does not affect user response)YesYesNoHigh (3/3)Instant (0/3)None (0/3)High (3/3)Service Mesh

Common confusion:

  • Canary vs. A/B Testing: Both expose Version B to a subset of live users, but Canary splits general traffic indiscriminately to verify stability, while A/B Testing targets specific user conditions (headers, regions, cohorts).
  • Why A/B Testing and Shadow require a Service Mesh: Vanilla Kubernetes Services balance coarsely across Pod endpoints; A/B routing and live traffic mirroring require fine-grained L7 traffic control provided by a Service Mesh (see Lecture 13 - Service Mesh and Istio).

Retrieval

  1. Why do “sticky sessions” (storing user session state in a Pod’s RAM) violate both Factor VIII and Factor IX?
    They violate Factor VIII (Horizontal Scaling) because traffic cannot be freely distributed across newly added replicas without breaking user state, and Factor IX (Disposability) because killing or rolling a Pod during an update destroys active user sessions.
  2. You must validate a risky new payment service against real production traffic with zero negative impact on users if it fails, and extra temporary cloud cost is acceptable. Which strategy fits best, and what extra infrastructure is required?
    Shadow deployment (zero downtime, tests real traffic, zero negative user impact, instant rollback, higher cloud cost). It requires a Service Mesh for fine-grained traffic mirroring.
  3. When does the 12-Factor horizontal scaling rule (one-thread-per-process, scale out) fail to fit?
    In HPC (High-Performance Computing) workloads where multiple threads interact closely and pass data through shared memory—moving work across separate nodes/processes introduces prohibitive communication overhead.

Source: Lecture 12 - Cloud Native Applications and the 12-Factor App, PDF pages 13–18 (printed slides 14–19).