Skip to content

Twelve-factor app

Introduction

The twelve-factor app is a checklist of lessons for writing cloud services; the core ideas are config outside the code, stateless services, logs sent to the output stream, and the ability to start and stop at any time.

Why use it

Following these practices lets the app move across development, test, and production without code changes, scale out by running more instances, and fit cleanly into containers.

The most important factors

  1. Config outside the code: database URLs, secrets, and other environment-specific values live in environment variables; they are not hardcoded or committed (see secrets management).
  2. Stateless processes: the service itself does not keep user sessions, uploads, or other long-lived state. Put those in the database, Redis, or object storage. Then any instance can die or be added without affecting users.
  3. Logs to standard output: the program only prints logs; the platform collects and stores them (see logging).
  4. One codebase, many deploys: the same build artifact goes to every environment; differences come only from config.
  5. Start fast, stop gracefully: start quickly; on a stop signal, finish in-flight requests before exiting.
  6. Dev/prod parity: avoid “one database locally, another in production”.

Component comparison

Configuration via environment variables

Built into the frameworkOpsLow

Things that vary by environment, such as database addresses and secrets, are not written into code but read from environment variables at runtime.

Best forThe starting point of almost every project, with few settings and deployment on few servers.

Avoid whenThere are many settings and you need auditing and dynamic changes; add a configuration center.

Pros

The same code can run directly in development, test, and production

Containers and common platforms support environment variables natively

Passwords live in the environment, so they are not committed to the repository

Cons

Environment variables are plain text, and someone who can see the process may read them

Too many variables become hard to manage, and there is no history or approval

A changed variable usually takes effect only after the application restarts

Containerization + stateless services

Self-hosted open sourceOpen sourceOpsMedium

Package the app as a container image; the service itself stores no user data, so you can run more copies or shut one down at any time.

Best forServices that need multi-instance scaling and frequent releases.

Avoid whenSmall projects on a single server with very low traffic, where deploying directly may be easier.

Pros

The same image runs in every environment: one build, deployed in many places

Adding an instance, or replacing a failed one, is fairly direct

Logs go to standard output, and the platform collects them in one place

Cons

Sessions and uploaded files have to move out, which needs extra work

You have to manage image builds and image size, which has a learning cost

Once you move up to Kubernetes, orchestration and operations are a higher bar

Configuration center

Managed serviceFree tier + paidOpsMedium

Put configuration in one dedicated service that apps fetch from at startup or at runtime, allowing centralized changes and tracking.

Best forTeams with many microservices, frequently changing configuration, and auditing needs.

Avoid whenProjects with only one or two services, where environment variables are enough.

Pros

Configuration of many services is managed centrally, with change history and access control

Some products support dynamic updates without restart

Suits teams with many settings and many services

Cons

One more critical service must stay available, and if it goes down apps may fail to start

Adds integration and learning cost

Clearly over-engineering for small projects

Checklist

  • The repo has no passwords, secrets, or environment-specific addresses
  • Service instances can be scaled out or replaced at any time
  • Sessions and uploads live outside the service
  • Logs go to standard output
  • The same build artifact can be deployed to every environment