12 Factor Principle for Cloud Apps
Code is Version Controlled
- Always tracked in version control system
- 1:1 relationship between codebase and app
- Many deploys of a given app
- Codebase same across deploys, versions may differ
Dependencies are Declared and Isolated
- Never assume system-wide packages
- Dependency declaration manifest
- Isolated so no dependency “leak” from system
- Helps new developers
Configuration is Stored in the Environment
- Should store in environment variables
- Should not be “constants” in code
- Ideally not in configuration files
- Avoid grouping as “environments”
Backing Services as Attached Resources
- Services consumed over the network
- No distinction between local or 3rd party services
- Keep dependencies de-coupled
- Attach and detach at will
Build and Run Stages are Separated
- Impossible to change code at runtime
- Releases should have IDs
- Build may be complex, started by devs
- Run is simple and completely unattended
Application Executed as Stateless Processes
- Share nothing
- Persisted data in statefulbacking store
- Memory and file system is cache only
- Avoid sticky sessions
Services are Exported via Port Binding
- Self contained
- Embedded servers
- Listen on a specific port
- Very specific and idealistic
Application Scaled Out via Process Model
- Processes are first-class citizens
- Work assigned to a process type
- Applications have processes that span servers
- Use OS process managers, not daemons
Processes are Disposable
- Can be started or stopped at any time
- Minimal startup time, graceful shutdown
- Worker processes return work to the queue
- Robust against sudden death
Parity Between Application Environments
- Avoid time/personnel/tool gaps
- Design for continuous deployment
- Very important for backing services
- Containers and configmgmt. makes this easier!
Logs Treated as Event Streams
- Logs are a stream of time-ordered events
- App is never concerned with storing own logfiles
- Execution environment captures and stores logs
- May be routed to file, watched, sent to external service
Management Tasks Run as One-Off Processes
- Run in identical environment
- Separate out as scripts that are source controlled
- Don’t run from local terminal
- Don’t run directly against the database