Contents

Backend Development › Backend Basics

The Twelve-Factor App

Twelve practices for building deployable, scalable web services.

Also known as: 12-factor app, twelve factor, 12 factor methodology, 12factor

The Twelve-Factor App is a methodology for building web services that are easy to deploy, scale and maintain, written by developers at Heroku (published around 2011) from what they’d seen across many apps. It’s still a common reference for cloud and container-based services, and a popular interview topic. The twelve factors:

#FactorIn practice
1CodebaseOne codebase in version control, many deploys (dev, staging, production)
2DependenciesDeclare them explicitly (a lockfile) and isolate them. Don’t rely on system-wide packages
3ConfigKeep config in the environment, not in code (configuration)
4Backing servicesTreat databases, queues and caches as attached resources, swappable by changing a URL
5Build, release, runStrictly separate building the artifact, combining it with config, and running it
6ProcessesRun as one or more stateless processes. Persistent data lives in a backing service (stateless services)
7Port bindingThe app is self-contained and exports HTTP by listening on a port
8ConcurrencyScale out by running more processes, with different process types for different work (worker process model)
9DisposabilityFast startup, graceful shutdown. Processes can be started and stopped at any moment
10Dev/prod parityKeep environments as similar as possible (environments)
11LogsTreat logs as an event stream: write to stdout, and let the platform collect them (logging)
12Admin processesRun one-off tasks (migrations, scripts) in the same environment and codebase as the app

Why it holds up

Together, they describe a service that can be built once, configured per environment, started anywhere and scaled by adding copies, which is exactly what containers and cloud platforms assume.

# the spirit of the factors, in one place
export DATABASE_URL=postgres://...      # 3 and 4: config and backing service via environment
npm ci && npm run build                 # 2 and 5: explicit dependencies, a separate build step
node server.js                          # 6 and 7: a stateless process that listens on $PORT

Use it as guidance, not scripture

  • Some parts show their age or need nuance: environment variables alone are a limited way to manage many secrets, and some apps legitimately hold state.
  • It targets web services. Batch jobs, data pipelines and mobile apps need adaptation.
  • The value is the mindset: separate code, config and state, and make processes disposable. Most operational headaches come from violating one of them (a config file baked into the image, state on local disk, a “pet” server nobody can recreate).