The twelve factors of a well-written application
The Twelve-Factor App, written at Heroku in 2011: four ideas about code, and three rules missing from the list.
Benjamin · Technical directorThe Twelve-Factor App is a short text published in 2011 by Adam Wiggins, one of the founders of Heroku. It sets out twelve rules for a web application to deploy, scale and stay maintainable without fuss. It takes twenty minutes to read on 12factor.net.
It is a good reference, both for code to be written and for code to be audited, taken over or extended. The twelve rules fall into four ideas.
Code and configuration are kept apart
The code is the same everywhere. What changes from one environment to another, database addresses, passwords, API keys, is read from environment variables (factor 3).
The database, the mail server and file storage are services attached through an address (factor 4). Moving from a local database to a hosted one means changing one variable.
This is the first rule to follow. A credential written in the code stays in the repository history, even once deleted, and every copy of the repository carries it.
An application can be rebuilt identically
An application has a single repository (factor 1). Its dependencies are declared there with their versions (factor 2). A release is built, receives its configuration, then runs, in three separate steps (factor 5). The developer’s machine, staging and production use the same services, in the same versions (factor 10).
This leads to a simple requirement: any developer should be able to run the application on their machine with one command, and deploy it with another. The repository’s README says which.
An application can stop and start again
The application keeps nothing on its disk or in memory from one request to the next (factor 6). It listens on a port and starts on its own (factor 7). Capacity is added by starting more processes (factor 8). It starts in a few seconds and shuts down cleanly, finishing what it has started (factor 9).
These four rules go together. They make it possible to restart an application at any time, update it without downtime, and run a second copy the day one is no longer enough. An application that stores its users’ files on its own disk can do none of this.
What happens in production can be read
The application writes its events to standard output, and the environment collects them (factor 11). Migrations and maintenance scripts are part of the code and run in the same environment as the application (factor 12).
Any action in production should leave a trace: a versioned command that can be read and run again.
What is missing
The 2011 text says nothing about tests or security. Three rules complete the list:
- Tests that run before every deployment. A deployment that breaks something should be stopped by a machine.
- Up-to-date dependencies. A library that is no longer maintained no longer receives security fixes.
- A planned rollback. Every release states how to return to the previous version.
For an audit
These fifteen rules make an audit grid, before a takeover or any work on an application. Each one is rated with the client: met, partly met, not met. The result fits on one page and says what to fix, and in what order.