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 aClusterIPService (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:
- One-thread-per-process model: Applications should not try to scale by dynamically spawning many internal threads inside one process.
- 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) andLimits(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.
- Pods declare
- 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
LivenessProbechecks 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.
- Pods can be deleted and recreated on demand, scaled to variable replica counts, and automatically restarted by
- Load-balanced upgrade trace (slide 18): Behind a Load Balancer (
LB), oldv1.1Pods are taken out of rotation one by one while healthyv1.2Pods join (a->b->c->d), completing the upgrade with no service interruption.
Deployment strategies compared (slide 19)
| Strategy | How traffic & versions shift | Zero downtime? | Real traffic test? | Targeted users? | Cloud cost | Rollback duration | Negative impact on user | Setup complexity | K8s support |
|---|---|---|---|---|---|---|---|---|---|
| Recreate | Terminate all A, then roll out B | No | No | No | Low (1/3) | Slow (3/3) | High (3/3) | Minimal (0/3) | Vanilla K8s |
| Ramped (Rolling) | Slowly roll out B replacing A | Yes | No | No | Low (1/3) | Slow (3/3) | Low (1/3) | Low (1/3) | Vanilla K8s |
| Blue/Green | Run full B alongside A, then switch traffic | Yes | No | No | High (3/3) | Instant (0/3) | Medium (2/3) | Medium (2/3) | Vanilla K8s |
| Canary | Release B to a small subset of users, then full rollout | Yes | Yes | No | Low (1/3) | Medium (2/3) | Low (1/3) | Medium (2/3) | Vanilla K8s |
| A/B Testing | Route a specific user segment (condition) to B | Yes | Yes | Yes | Low (1/3) | Medium (2/3) | Low (1/3) | High (3/3) | Service Mesh |
| Shadow | Mirror real traffic to B alongside A (B does not affect user response) | Yes | Yes | No | High (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
- 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. - 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. - 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).