What is the twelve-factor app?

In short
The twelve-factor app is a set of twelve practices for building software-as-a-service apps that are easy to deploy, scale and maintain. It was written by developers who worked on the Heroku cloud platform. The method works with any programming language and any mix of backing services.
What the method is for
Most modern software runs as a service. Users open a web app in a browser, and the code runs on servers you never see. Building such software well is harder than it looks. Apps grow over time, new developers join the team, and the code slowly drifts away from the environment where it runs. The twelve-factor app is a methodology, meaning a set of guiding principles rather than a framework or a library. It describes how to structure an app so that it stays portable, easy to deploy and simple to scale.
The method came from the people behind the Heroku platform. They had helped build and deploy hundreds of apps and seen many more run in the wild. The twelve factors are their summary of what tended to work and what tended to cause trouble.
The twelve factors
The factors cover several areas of an app's life:
- Code: one codebase in version control, deployed many times.
- Setup: dependencies declared explicitly, config kept in the environment, and backing services treated as attached resources.
- Running: separate build, release and run stages, stateless processes, port binding, scaling by adding processes, fast startup and graceful shutdown.
- Growth: keep development, staging and production similar, treat logs as event streams, and run admin tasks as one-off processes.
You do not need to adopt all twelve at once. Each one is a reasonable habit on its own. Together they reduce surprises when an app moves from a laptop to a server.
How it works in practice
Take config as an example. A common mistake is to hard-code a database password in the source file. The twelve-factor approach stores config in the environment instead, so the same code can run in test and production with different values. A simple pattern looks like this:
export DATABASE_URL=postgres://user:[email protected]:5432/shop
node server.js
The app reads DATABASE_URL at startup. Nothing in the code changes when the database moves. The same idea applies to backing services such as a queue or a memory cache. The app treats them as attached resources that can be swapped by changing a setting.
Another factor concerns processes. An app should run as one or more stateless processes. Any data that must last belongs in a backing service, such as a database, not in the memory or local disk of one process. Because processes hold no unique state, you can start more of them to handle more load, which is the idea behind scaling out.
When to use it
The method suits apps that run as a service and are expected to change often. Teams that deploy several times a day benefit most from the dev and production parity and the strict separation of build and run stages. It is also useful when you plan to move an app between hosting providers, because the clean contract with the operating system keeps the code portable.
It is less relevant for a small script that runs once on one machine, or for desktop software that users install locally. The principles still hold in spirit, but following every factor would add effort without much gain.
Common mistakes
A frequent mistake is treating the twelve factors as a checklist to pass rather than as trade-offs to understand. Another is storing user uploads on the local disk of a server, then wondering why they vanish when a process is replaced. Keeping state in a proper backing service avoids that.
Teams also sometimes write logs to files and rotate them by hand. The method suggests writing logs as a stream of events to standard output and letting the platform collect them. This keeps the app simple and makes logs easier to read in one place.
Alternatives and related ideas
The twelve-factor app is not the only way to think about service design. Some teams prefer a microservices approach, which splits an app into many small services, each with its own data. Others focus on container tools that package code with its dependencies. These ideas overlap with the twelve factors. The methodology is best seen as a common vocabulary and a sensible baseline, not as a replacement for judgment about your own system.
The full text at the official site explains each factor in more detail, with the reasoning behind it. Reading the original is the best next step after this overview.
FAQ
Do I need to follow all twelve factors?
No. The factors are guidelines rather than strict rules. Many teams adopt the most useful ones first, such as storing config in the environment and keeping processes stateless.
Does the twelve-factor app only work with one programming language?
No. The methodology applies to apps written in any language. It also works with any combination of backing services, such as databases, queues and memory caches.
Why does the method say to keep config in the environment?
Environment settings change between deploys, while code stays the same. Keeping config outside the code lets one codebase run in several environments without edits.
Is the twelve-factor app the same as a framework?
No. It is a methodology, a set of principles for structuring an app. You can apply it with any framework or with none.
Sources
This page is based on the official documentation below. Read it for full detail.
Twelve-factor app — official documentation ↗