The 12-Factor App is a set of best practices for building modern, scalable, and maintainable software-as-a-service (SaaS) applications. It was introduced by engineers at Heroku to help teams build applications that are:
- Portable between environments
- Resilient
- Easily scalable
- Maintainable over time
Here are the 12 principles:
- Keep a single repository per app.
- Multiple environments (dev, staging, prod) are just deploys of the same codebase.
- Don’t rely on system-wide packages.
- Use dependency managers like
Maven(Java),npm(Node.js),pip(Python). - Make dependencies explicit and reproducible.
- Separate config from code.
- Use environment variables for credentials, URLs, feature toggles.
- Avoid hardcoding values.
- Databases, queues, caches, or APIs are services, not hardcoded.
- Access them via URLs or environment config so they can be swapped easily.
- Build: compile, package assets, create executable.
- Release: combine build + config into a release.
- Run: execute in the runtime environment.
- Never change code in the running environment.
- Don’t store data in memory or local disk that you need later.
- Use external storage (DB, cache, etc.) for persistence.
- Statelessness makes horizontal scaling easier.
- Self-contained apps should expose an HTTP port directly.
- No need for an external web server like Apache or Nginx inside the same app process.
- Run multiple instances for scaling, rather than adding threads inside one big instance.
- Use a process manager or orchestration (Kubernetes, Docker).
- Apps should start quickly and shut down cleanly.
- Helps in scaling, deployment, and recovery.
-
Reduce the gap in:
- Time (deploy often)
- Personnel (same team in all environments)
- Tools (same dependencies and versions everywhere)
- Don’t manage log files inside the app.
- Write logs to stdout/stderr, let the environment aggregate/store them.
- Database migrations, cron jobs, or scripts should run in the same environment/config as the app but as separate one-off processes.
💡 In short: The 12-factor methodology pushes for stateless, portable, and easily deployable applications where configuration is separate, dependencies are explicit, and scaling is easy.