Theory
12-Factor App Principles
📋 Overview
The 12-Factor App is a methodology for building software-as-a-service (SaaS) applications that are portable, resilient, and scalable. It solves the core problems of "works on my machine" syndrome and scaling bottlenecks by enforcing strict separation between code, configuration, and state.
🏗️ Core Principles & Characteristics
- Codebase: One codebase tracked in version control, many deploys.
- Dependencies: Explicitly declare and isolate dependencies (no reliance on system-wide packages).
- Config: Store configuration in the environment (variables), never in the code.
- Backing Services: Treat databases, caches, and queues as attached resources.
- Build, Release, Run: Strictly separate these stages; releases are immutable.
- Processes: Execute the app as one or more stateless, share-nothing processes.
- Port Binding: Export services via port binding (self-contained).
- Concurrency: Scale out via the process model (horizontal scaling).
- Disposability: Maximize robustness with fast startup and graceful shutdown.
- Dev/Prod Parity: Keep development, staging, and production as similar as possible.
- Logs: Treat logs as event streams, not managed files.
- Admin Processes: Run management/admin tasks as one-off processes.
⚖️ Trade-offs: Pros & Cons
| Feature / Approach | Advantages (Pros) | Disadvantages (Cons) |
|---|---|---|
| Statelessness | Seamless horizontal scaling and high fault tolerance. | Requires external state management (e.g., Redis), increasing latency for state access. |
| Environment Config | High security (no secrets in repo) and easy environment switching. | Can lead to "config sprawl" if not managed by a central orchestrator. |
| Port Binding | Removes dependency on external web servers like Apache/Nginx. | Requires the application to handle its own networking stack complexities. |
🌍 Real-World Implementation
- Tech Stack: Heroku (where it originated), Kubernetes, Docker, AWS Lambda.
- Use Case: Modern microservices architecture where services must be deployed and scaled independently across cloud providers.
💡 Interview "Gotchas" & Tips
- The "Stateless" Myth: Remember that while the app is stateless, the system is not. State must be pushed to backing services (DBs/Caches).
- Parity Gap: Be prepared to discuss how Docker helps achieve Dev/Prod parity but doesn't solve differences in backing service scale (e.g., local SQLite vs. prod RDS).
📐 Suggested Architecture Primitives
- Client, Gateway, Server, Service, SQL, NoSQL, Cache, MQ, Cloud, Auth
Canvas