Twelve-Factor App
12 факторов
A set of 12 practices for building applications that are easy to deploy, update, and scale. The practices separate the application from environment-specific settings and keep its data independent of any one running instance.
Also known as: 12-factor
What it means
Imagine deploying the same web app to a laptop, a test environment, and production. You should be able to point it at a different database without editing its source, and replacing a running instance shouldn't delete anyone's data. The Twelve-Factor App is a set of practices for achieving that. It describes how an app is built and operated, not how to organize its business logic. It works for monoliths as well as microservices.
The twelve factors
- Codebase. Keep one version-controlled codebase per app. Development, staging, and production are deployments of that codebase, not separately maintained copies of it.
- Dependencies. Declare and isolate dependencies so the app doesn't accidentally rely on a package or tool installed on a particular machine.
- Config. Supply values that vary by deployment through environment variables. Database connection settings belong there; an internal routing table that doesn't vary by deployment can stay in code.
- Backing services. Treat databases, queues, and similar services as attached resources. Switching to another compatible instance should be a configuration change, not a code change.
- Build, release, run. Build an artifact from the code, combine it with deployment configuration to create a release, and run that release without rebuilding the artifact.
- Processes. Run stateless processes that don't depend on one another's local memory or disk. Store data that has to survive a restart or be available to another instance in a backing service.
- Port binding. Have the web process listen on a port itself. A reverse proxy can still route traffic to it.
- Concurrency. Scale out by adding processes of the type that needs capacity. For example, add queue workers without adding web instances.
- Disposability. Start quickly and shut down gracefully. A worker that can't finish a job before it exits should leave the job eligible for redelivery.
- Dev/prod parity. Keep development and production close in tooling and backing services, and keep the time between writing and deploying changes short.
- Logs. Emit logs as a stream to standard output; let the execution environment handle collection and storage.
- Admin processes. Run migrations and other one-off tasks separately, using the same release and environment as the regular app processes.
Example: deploying a web app and a worker
An order service uses PostgreSQL and a message queue. CI builds one image, and the deployment platform runs that image with different connection settings in staging and production. The web process accepts orders; separate worker processes drain the queue. If the queue starts to back up, the team can add workers without rebuilding the image. A database migration runs as a separate job instead of having every new instance attempt it at startup.
A common interview question
What breaks when you put two instances behind a load balancer? If login sessions live in process memory, a request routed to the other instance may not recognize the user. Moving sessions to a shared store solves the routing problem and lets sessions survive instance replacement. Stateless doesn't mean an app has no data or can't use memory: it means handling a later request doesn't depend on the previous instance still being there. Containers alone don't give you that property.