The 12-Factor App Approach for Building Scalable and Maintainable Applications
The 12-Factor App methodology provides a set of best practices for developing modern, cloud-native applications. These principles were introduced by Heroku co-founder Adam Wiggins in 2011 and have since become widely adopted in the software development community. In this article, we’ll explore each of the 12 factors and provide examples to illustrate their importance.
1. Codebase
One codebase per application: Maintain a single codebase for your application, even if it runs on multiple environments (e.g., development, staging, production). Use version control (e.g., Git) to manage changes.
Example: Imagine a Python web app with a single Git repository containing all the code for different environments (dev, staging, prod).
2. Dependencies
Explicitly declare dependencies: Use a dependency management tool (e.g.,
pip
for Python) to specify the exact versions of libraries and packages your application relies on.Example: Specify library versions in a
requirements.txt
file.
3. Config
Store configuration in the environment: Avoid hardcoding configuration values (e.g., database credentials, API keys) in your code. Instead, use environment variables to configure your application.
Example: Use environment variables for database credentials (
DATABASE_USER
).
4. Backing Services
Treat backing services (databases, caches, queues) as attached resources: Connect to these services via environment variables or configuration files. Avoid hardcoding connection strings.
Example: Connect to a PostgreSQL database using the
DATABASE_URL
environment variable.
5. Build, Release, Run
Separate build, release, and run stages: Build your application, package it into a release, and then run it. This separation ensures consistency across environments.
Example: Separate CI/CD pipelines for building, releasing, and deploying the app on various environments.
6. Processes
Execute the app as one or more stateless processes: Avoid storing application state in-process. Use external services (e.g., databases) for persistence.
Example: Run multiple instances of the app behind a load balancer.
7. Port Binding
Export services via port binding: Your app should listen on a specific port (specified by the environment) to receive incoming requests.
Example: Listen on port 8080 for incoming HTTP requests.
8. Concurrency
Scale out via the process model: Run multiple instances of your app to handle increased load. Use a process manager (e.g.,
systemd
) to manage concurrency.Example: Scale horizontally by adding more app instances.
9. Disposability
Maximise robustness with fast startup and graceful shutdown: Design your app to start quickly and handle graceful shutdowns. Use stateless processes to facilitate easy restarts.
Example: Handle
SIGTERM
gracefully for clean shutdowns and finish processing the existing requests.
10. Dev/Prod Parity
Keep development, staging, and production environments as similar as possible: Use the same tools, services, and configurations across environments to minimise surprises.
Example: use the same Docker image for dev and prod environments.
11. Logs
Treat logs as event streams: Log important events to
stdout
or a centralised logging service. Avoid writing logs to files within the app.Example: Log important events to
stdout
or a centralised logging service for tracing.
12. Admin Processes
Run admin/management tasks as one-off processes: Use separate processes for tasks like database migrations, data imports, and backups.
Example: Run a one-off task to migrate the database schema or generating the signed certificates.
By following the 12-Factor App principles, you can build applications that are easier to maintain, scale, and deploy in modern cloud environments. For further deep dive visit https://12factor.net/
Subscribe to my newsletter
Read articles from Sandeep Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by