1. The 12 Factor App¶
In the modern era, software is created as web apps
or software-as-a-service
. The 12 factor app is a methodology for building software-as-a-service
apps that:
Key Principles
- Use declarative formats for setup automation
- Have a clean contract with the underlying OS
- Are suitable for deployment on modern cloud platforms
- Minimize divergence between development and production
- Can scale up without significant changes
1.1. I. Codebase¶
One codebase tracked in revision control, many deploys
- Always tracked with version control system (e.g., Git, Mercurial, Subversion)
- One-to-one correlation between codebase and app
- Multiple codebases = distributed system
- Shared code should be factored into libraries
1.2. II. Dependencies¶
Explicitly declare and isolate dependencies
- Never rely on implicit system-wide packages
- Declare all dependencies via a dependency declaration manifest
- Use a dependency isolation tool during execution
Example in Python: - Pip for
declaration
- Virtualenv forisolation
1.3. III. Config¶
Store config in the environment
- Config includes: database handles, credentials, per-deploy values
- Strict separation of config from code
- Store config in environment variables
Avoid: - Constants in code - Config files not in revision control - Grouped "environments" (e.g., development, test, production)
1.4. IV. Backing Services¶
Treat backing services as attached resources
- Examples: datastores, messaging systems, SMTP services, caching systems
- No distinction between local and third-party services
- Access via URL or credentials stored in config
1.5. V. Build, release, run¶
Strictly separate build and run stages
- Build stage: Fetch dependencies, compile binaries and assets
- Release stage: Combine build with config
- Run stage: Execute app in the environment
1.6. VI. Processes¶
Execute the app as one or more stateless processes
- Processes are stateless and share-nothing
- Persist data in stateful backing services (e.g., databases)
1.7. VII. Port binding¶
Export services via port binding
- App is completely self-contained
- Exports HTTP as a service by binding to a port
1.8. VIII. Concurrency¶
Scale out via the process model
- Processes are a first-class citizen
- Enable horizontal scaling
- Rely on the OS's process manager
1.9. IX. Disposability¶
Maximize robustness with fast startup and graceful shutdown