12 Factor App
Twelve Factor App methodology
Codebase
In this article, we will explore the 12-Factor App methodology while demonstrating a simple example built with Python and the Flask web framework. These principles are designed to guide the development of scalable, resilient, and maintainable applications.
The 12 factors are as follows:
- Have one codebase.
- Explicitly declare and isolate dependencies.
- Store configuration in the environment.
- Treat backing services as attached resources.
- Strictly separate build and run stages.
- Execute the app as one or more stateless processes.
- Export services via port binding.
- Scale out via the process model.
- Maximize robustness with fast startup and graceful shutdown.
- Keep development, staging, and production as similar as possible.
- Treat logs as event streams.
- Run admin or management tasks as one-off processes.
A Simple Flask Example
We begin with a basic Flask application that displays a simple message when accessed via a browser. The entry point for our Flask app is the app.py
file, which is responsible for handling all incoming and outgoing requests.
Below is the code for our simple Flask app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def welcomeToKodeKloud():
return "Welcome to KODEKLOUD!"
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
When you run this application and navigate to its URL in your browser, the homepage will display the message "Welcome to KODEKLOUD!".
Note
At this stage, the code is stored only on your local machine.
The First Factor: One Codebase
The first principle of the 12-Factor App methodology insists on maintaining a single codebase per application. This approach is essential as your user base expands and new features are added, especially when multiple developers are involved. Each developer works on their own local environment while committing changes to a central repository.
Without a proper version control system, concurrent modifications by different developers could lead to conflicts. This is where Git becomes invaluable. Git enables concurrent contributions to the same codebase by facilitating operations like pulling the latest changes with the git pull
command, making local updates, and pushing new commits using git push
.
The central repository is typically hosted on a cloud platform. For example, GitHub is a popular platform for hosting Git repositories, while GitLab and Bitbucket offer similar solutions.
Managing Multiple Applications
Consider starting with an initial web application. Over time, you might expand your system by adding services such as order processing or delivery functionalities. In the past, it was common to maintain a single codebase for all related applications. However, once multiple services are deployed, the architecture becomes distributed, and sharing one codebase across multiple applications violates the 12-Factor App principles. Each application should reside in its own codebase.
Within a single codebase, you can still deploy multiple instances of your application across various environments (such as development, staging, and production). This strategy ensures that while each application maintains its isolated codebase, you can manage multiple deployments seamlessly.
This structure not only keeps your development process organized but also promotes consistency across different environments, allowing for smoother transitions between development, testing, and production stages.
Summary
By following the principles outlined in the 12-Factor App methodology, you ensure that every aspect of your application—from a single codebase to distinct deployments across environments—is optimized for scalability and maintainability. Leveraging tools like Git further enhances collaborative development, helping you manage changes effectively as your application grows.
For more insights on scalable application design and best practices, explore our additional resources and documentation linked below.
Watch Video
Watch video content