12-Factor Engineering Best Practices You Can Learn From
Learn the 12 principles introduced by Heroku engineers for developing high-quality web applications with scale, robustness, and portability in mind.
During my Computer Science days at the University of Lagos, I used to wonder why software engineering was called engineering. Before then, I had always thought engineering had to be hardware.
Luckily, Ian Sommerville was thinking about confused people like me when he wrote Software Engineering. He described software engineering as an engineering discipline concerned with all aspects of software production.
That framing helped. Engineering is still engineering because we make things work by applying theories, methods, and tools where appropriate. We also inherit best practices from other engineers so we can avoid already solved problems.
Similarly, the 12-factor methodology introduced by engineers at Heroku offers practical principles for building high-quality web applications with scale, robustness, and portability in mind.
The 12-factor application methodology
The 12th Factor application methodology contains 12 principles for building a strong web application or SaaS product. It came from Heroku engineers after developing, deploying, and scaling thousands of applications.
Factor 1: Codebase
- Track your source code in version control.
- Keep one codebase per app and use branching strategies for different deployment paths.
Factor 2: Dependencies
- Declare dependencies explicitly with package managers such as npm, Yarn, pip, or Composer.
- Avoid copying dependencies into source code directly.
Factor 3: Config
- Store configuration in the environment.
- Separate secrets and environment-specific values from application code.
Factor 4: Backing Services
- Treat databases, queues, and similar services as attached resources.
- Be able to switch between local and managed services without rewriting the application.
Factor 5: Build, Release, Run
- Separate build, release, and run stages strictly.
- Use CI/CD pipelines with test and deploy stages.
- Give each release a unique identifier and preserve rollback options.
Factor 6: Stateless Processes
- Run the app as one or more stateless processes.
- Persist state in backing services rather than application memory.
Factor 7: Port Binding
- Export services via port binding.
- Make it possible for one application to become another application's backing service.
Factor 8: Concurrency
- Scale through the process model.
- Separate HTTP work, workers, and other workloads into explicit process types.
Factor 9: Disposability
- Optimize for fast startup and graceful shutdown.
- Disposable processes make releases and scaling safer.
Factor 10: Dev-Prod Parity
- Keep development, staging, and production environments as similar as possible.
- Small gaps reduce surprises and improve continuous delivery.
Factor 11: Logs
- Treat logs as event streams.
- Emit logs to standard output and let external systems handle routing and storage.
Factor 12: Admin Processes
- Run management tasks as one-off processes in the same environment as the main app.
- Ship admin code with the application to avoid synchronization issues.
comments