Course contents · Lesson 3 of 16

The Twelve-Factor App Explained for Beginners

DevOps Basics · Lesson 3 of 16 · 8 min Foundations

You move a small web app from your laptop to a cloud server, and it takes a whole weekend: a password is typed into the code, a file path exists only on your laptop, and uploaded photos sit on its disk. The twelve-factor app is a list of twelve habits that prevent this. This lesson covers where it comes from, what each factor means and the four that matter most.

Where the twelve factors come from

Heroku is a platform as a service (PaaS): you upload your code and it runs the app for you. In 2011, Adam Wiggins and other Heroku engineers distilled lessons from building hundreds of apps, and watching hundreds of thousands more run, into The Twelve-Factor App.

It targets software as a service: software used over the internet, such as web email, instead of installed. It is still widely quoted. In November 2024 it became an open-source project, and community maintainers are writing an updated version.

The twelve factors at a glance

Each factor is one rule, numbered I to XII. The table sorts them into three groups; the four in green get their own sections below.

Table of the twelve factors in three columns. Code and setup: I Codebase, one repo per app deployed many times; II Dependencies, declare and isolate every library; III Config, per-deploy settings in environment variables; IV Backing services, databases and queues attached by address. Build and run: V Build, release, run; VI Processes, stateless; VII Port binding; VIII Concurrency, scale out with more copies. Operate safely: IX Disposability, start fast and stop cleanly; X Dev/prod parity; XI Logs to standard output; XII Admin processes. Config, Processes, Disposability and Logs are highlighted
Each factor makes the app depend less on one particular machine.

Code and setup. A deploy is one running installation of the app, such as your laptop or production; one codebase, such as a Git repository, feeds them all. Dependencies, the libraries the app uses, are listed in a file, not assumed to be on the server. Backing services, such as a database or an email service, are attached by an address in config, so you can swap one without changing code.

Build and run. With port binding, the app has its own web server and answers on a port, a numbered network door such as 5000. With concurrency, you handle more work by running more copies, not one bigger copy.

Operate safely. Dev/prod parity is the rule from lesson 2, down to the same database type and version. Admin processes are one-off jobs, such as a script that changes the database's table layout, run with the app's code and config.

Four factors that matter most

III. Config in environment variables

Config is everything likely to vary between deploys: database addresses, passwords and keys, the site's web address. Factor III keeps it in environment variables, named settings the operating system hands a program when it starts, such as DATABASE_URL. They change per deploy without touching code, rarely end up in Git by accident, and any language can read them. Passwords are often filled in from a protected secret store at start (lesson 14).

VI. Stateless processes

State is anything the app must remember between requests, such as logins, carts and uploads. A stateless process keeps none of it, because a restart or a move to another machine usually wipes its memory and disk. Lasting data goes to a backing service, typically a database.

In the diagram, a load balancer, which spreads requests across copies, feeds three of them. Copy 2 is restarting, yet no cart is lost. Twelve-factor also rules out sticky sessions, which send a user back to the copy holding their login in memory.

Users send requests to a load balancer, which spreads them across three app copies. Copies 1 and 3 hold no lasting data and read and write a shared database of orders, carts and sessions. Copy 2 is restarting and nothing is lost. A note says a copy's memory and disk can vanish at any restart, so lasting data belongs in the database
Nothing unique lives in a copy, so losing one costs nothing.

XI. Logs written to standard output

A log is a line an app writes to record an event, such as an error (see What Are Logs?). A twelve-factor app writes no log files. It prints events to standard output (stdout), the text stream every program has, which in a terminal is the screen. The platform collects that stream from every copy and sends it wherever the team chooses; the app never needs to know where.

IX. Fast start and clean shutdown

A disposable process is ideally ready a few seconds after launch and stops cleanly. To stop it, a platform sends SIGTERM, a polite "please stop" signal: a web process stops taking new requests and finishes current ones, and a background worker returns its unfinished job to the queue. Docker and Kubernetes force a stop if it is still running after a grace period (30 seconds by default in Kubernetes).

One build, many deploys

Factor V splits the journey into three stages. Build turns code into a runnable package with its dependencies. Release combines that build with one deploy's config under a unique ID. Run starts processes from one chosen release.

In the flowchart, config joins only at release, and releases never change: a fix starts again from the code, and a rollback runs an earlier release.

Flowchart: Codebase, the source in Git, leads to Build, a runnable package such as an image, which leads to Release, the build plus config, which leads to Run, where processes start from that release. A config card with the database address and keys joins only at Release. A note says no passwords or addresses in code or build. A dashed arrow from Run back to Codebase says need a fix, start again from the code. IDs are illustrative
Config joins at one step, so one build can serve staging and production.

Containers fit closely. A container image, the app packed with everything it needs, is the build. The same image runs in staging and production with different environment variables, and the container runtime collects its standard output as logs. A CI/CD pipeline, the automated build, test and deploy steps from lesson 2, follows the same rule: build once, then move that same artifact through every environment (lesson 4 and lesson 6).

Where to bend the rules

The factors are guidelines for web apps, not laws. Databases cannot be stateless: keeping state is their job, and twelve-factor moves state into them. Give a database you run yourself storage that survives restarts, or use a managed database.

A batch job, such as a nightly report, can run for hours, far beyond a shutdown grace period. Split it into small pieces or save progress at checkpoints, so a restarted job carries on. Older apps that keep sessions in memory or uploads on local disk can move them to a shared store and object storage.

Check yourself

Your app keeps carts in its own memory. What breaks with three copies behind a load balancer, and what fixes it?

A user's next request may reach a copy that never saw their cart, and any restart loses carts. Keeping carts in a shared database lets every copy see the same data.

A developer types the production database password into the code "just for now". Which factor breaks, and why?

Factor III, config. Everyone with the code now has the password, and changing it needs a new build; an environment variable gives each deploy its own value.

Why should your app stop cleanly on SIGTERM when the platform could simply kill it?

Platforms stop processes routinely, during releases and scaling down. A clean stop finishes requests and returns jobs to the queue, so users see no errors.