Bookmarks

Twelve-Factor App methodology

The Twelve-Factor App

A set of twelve principles for building SaaS applications: portable between environments and scalable without code changes.

Also: twelve factors, twelve-factor app, 12-factor app, twelve-factor application

checked 8 Nov

All terms

The methodology was drawn up by Adam Wiggins and colleagues at Heroku, distilling the experience of developing and deploying hundreds of applications. The idea is simple: an application should not depend on where it is run — the same code, unaltered, works on a developer’s laptop, in a staging environment and in the cloud. The document is addressed to developers of SaaS applications and operations engineers.

The twelve principles

PrincipleEssence
I. Codebaseone codebase in version control — many deployments from it
II. Dependenciesdependencies are declared explicitly and isolated from the system
III. Configsettings live in the environment, not in the code
IV. Backing servicesdatabase, queue, cache — attachable resources addressed through configuration
V. Build, release, runthree strictly separated stages; a finished release cannot be changed — only a new one issued
VI. Processesthe application is one or more stateless processes sharing nothing
VII. Port bindingthe application is self-contained and listens on its own port
VIII. Concurrencyscaling by adding processes, not by growing a single process
IX. Disposabilityfast startup and graceful shutdown — a process can be replaced at any moment
X. Dev/prod parityenvironments kept as similar as possible — the foundation of continuous delivery
XI. Logsthe log is a stream of events in stdout; the platform collects and stores it
XII. Admin processesmigrations and one-off scripts — the same codebase, in a one-off process

What this changes in practice

The main consequence is the separation of code and configuration. The application reads its settings from the environment: the database address from DATABASE_URL, keys for external services from variables such as API_KEY. A single build suits every environment; only the environment changes. A backing service can be swapped without touching the code: a local database for a cloud one, an own mail server for a third-party service; only the address in the configuration changes.

bashConfiguration lives in the environment: between environments the values change, not the code
DATABASE_URL=postgres://app@db.internal:5432/shopREDIS_URL=redis://cache.internal:6379S3_BUCKET=uploads

Processes hold no state: files live in external storage, sessions in a shared database or cache. A process must start in seconds and shut down gracefully on the supervisor’s signal: stop taking new requests and finish the current ones. Then the process manager can move a process to another machine or replace a crashed one at any moment, and scaling comes down to launching additional instances.

Origin and boundaries

The authors borrowed the document’s format from Martin Fowler’s books “Patterns of Enterprise Application Architecture” and “Refactoring”. The methodology does not prescribe an architecture: the principles describe deployment and configuration, not how the code is structured, so a monolith and a set of microservices alike can follow them. The full Russian translation is available on the methodology’s website.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • microservices

    The twelve factors are not about splitting into services: a monolith without microservices can follow them just as well.

  • PaaSplatform as a service

    PaaS is the platform on which an application runs; the factors describe what the application itself must be like for the platform to manage it.

In depth in the references

where the topic is covered in detail