The Code Codex
Theme
← Notes / Architecture

note · list17 Oct 2024 · 2 min

The Twelve-Factor App

A methodology published by the Heroku folks in 2011 for applications that run as a service in the cloud. More than a decade later, it is still the baseline for what we expect from a containerized application.

#FactorWhat it saysIn practice
ICodebaseOne versioned codebase, many deploysThe same repository produces dev, staging and production
IIDependenciesExplicitly declare and isolate dependenciesgo.mod, pom.xml, package.json; nothing installed “by hand” on the server
IIIConfigStore config in the environmentDatabase URL and keys in environment variables, not in code
IVBacking servicesTreat backing services as attached resourcesSwapping the local database for a managed one only changes config
VBuild, release, runStrictly separate the stagesAn image built once and promoted across environments
VIProcessesRun as stateless processesSession in Redis, not in the instance’s memory
VIIPort bindingExport services via a portThe app starts its own HTTP server on the configured port
VIIIConcurrencyScale out with more processesMore replicas instead of a bigger machine
IXDisposabilityStart fast and shut down gracefullyHandle SIGTERM, finish in-flight work and exit
XDev/prod parityKeep environments as similar as possibleThe same Postgres in dev (via a container) and in production
XILogsTreat logs as event streamsWrite to stdout and let the platform collect them
XIIAdmin processesRun admin tasks as one-off processesMigrations running as a job, with the same code as the app

Many of these factors are what make an application easy to run in containers.