Twelve Factor App Methodology
Hello Everyone,
while I was working on Microservices Architecture with one of EdX Course, I have come across with Twelve Factor App Mehodology and I wanted to write an article about it. Let’s deev in together and learn what is the Twelve Factor App Methodology.
The Twelve Factor App Methodology has written by Heroku developers and presented by adam wiggins. It’s a methodology to create Software as a Service(SaaS) Applications. Twelve factor app describes the charecteristic of modern software development, which is scalabe, flexiable and maintainable web applications.
1-Codebase: One codebase tracked in revision control, many deploys
Our first factor emphasizes the importance of having a single codebase stored in a version control system(such as Git), which simplifies the process of deploying and maintaing the application. And also It will be much easier for new joining developers to adapt to the project and the team.
2-Dependencies: Explicitly declare and isolate dependencies
When we are developing our project we are adding libraries, packages, frameworks and accessing from one service to the others as we need it. And when we are doing that we are hoping or thinking the system will have the package itself. We are adding dependencies. And every programing language has a packaging system(Bundler for ruby, NuGet for c#) Thats creating and downloading dependencies to the project. Dependencies factory says that: With Dependencies we are saying to the project that “Add this packages, libraries, frameworks and be able to access this services…” Thats how when other team members or anyone else download and open the project they will be able to work the project because the package system will be already adding dependencies to the project. because with our dependecy file everything we need will be already ready for being able to work the project correctly.
3-Config: Store config in the environment
While we are working on our project we are using some kind of accessing information(Such as passwords, hosts URLs and others) that’s why we can not and dont want to use them in the code. And we are putting them in an environment and we are accessing them from our code without know them. And also in case of making any changes with the information, instead of going and changing in the project’s code in a lot of places(we may use them more than one time) we are only changing it in the environment and it effecting all places which is accessing there and using it. These configs are generally Database urls, Credentials, API keys and some other resources.
4-Backend Services: Treat backing services as attached resources
Backing services are any service that the app service consumes. Like SMTP services for email, Messaging/queueing systems(RabbirMQ, Kafka), caching systems and other services. Every service that our application communicates over network is considering as a backing service. The purpose is that without making any changes to the app’s code we should be able to swap it with another changes that’s why we are using via URL or other locator/credentials. That’s how when we want to change a local SMTP with a third-party service we could do that without change the code.
5- Build, Release, Run: Strictly separate build and run stages
Build, Release and Run stages are strictly seperates. Build stages converts the source code into an executable binary bundle. Release stages takes the output of build stage and combines it with necessary configuration to the release, with a unique release identity to create uniqe release. And Run stages takes the release stages and run the app. Why it is important? it’s too important because while you are adding some new features or changing something you dont want that to effect your all project while the last release version is running you are making your changes then fallowing the steps again which are build, release and run. If everything is okay you can add the latest version in production and this process did not effect your project’s working version.
6-Processes: Execute the app as one or more stateless processes
Processes are about running your application on one or more stateless processes. Stateless applications are the key to achieving scalability, flexibility and maintainable. Stateless processes are about making states backing services stateless. With using services like Load balancer, and caching services. Processes are share nothing. every process are self-contained and does notshare any data with other services they are independent from other services. That’s the thing which make them stateless.
An example of processes scenario could be using a Load balancer to accessing more than one servers under control of Load balancer.
7-Port Binding: Export services via port binding
We are exporting our services by port binding and it allow the application to be self-contained. when we are developing an applciatin on our local device we are using local service such as http://localhost:5050 with port binding we are listening the request coming to this service. And there are internal and external services with port binding we are making them self-contained services. by using port binding one app can start to be a service too. And These ports are generally writing on the config file to be easy to change and also we can update them without make any change on the code.
8-Concurrency: Scale out via the process model
Services are self-contained, stateless processes. That’s how we can scale up and down at the same time multiple stateless process by using service controllers like Load Balancer. It’s out of order we can call more that one service at the same time and making them stateless will be an advantage in case of one of our services down other services will be still working and responsing the client’s request. And that’s how we can handle multiple tasks at the same time which is the purpose of Concurrency
9-Disposability: Maximize robustness with fast startup and graceful shutdown
Robustness means that the system should be able to maintain it’s functionality even when there is an unexpected conditions like traffic spikes, data center failures or any other problem. Our system should be able to stop and run the project quickly. Because while the project stop or run again there may be a huge number of request from client side and we always want to make our clients happy and get what they need from our platform. Designing stateless processes, Using Load Balancing, Containerization, Orchestration are some operation to make the Service Disposable.
10-Dev/Prod parity: Keep development, staging, and production as similar as possible
This factor is abut making the gap between development and production as similar as possible. While we are developing our project we want to be sure if our app will be working on production successfull too. And that’s why we are taking some steps like CI/CD pipelines, Testing Environmets, Configruation Management(environment variables seperates from the code) we want to minimize the difference between development and production. Our goal is to avoid unexpected issues and buggs when making deployment.
11-Logs: Treat logs as event streams
Logs are helps us to see the behavior of our events on service. Let’s say we had a problem while our service is working, that’s the time why we need Logs we will read and try to understand what could be the problem what was wrong or did not work while the service has failed. It provides a record of events, actions and errors to make easier to analyze, diagnosing issues and monitoring the performance on the service.
Logs from a MongoDB server:
2023-08-26 20:43:05 about to fork child process, waiting until server is ready for connections.
2023-08-26 20:43:05 forked process: 27
2023-08-26 20:43:05
2023-08-26 20:43:05 {"t":{"$date":"2023-08-26T17:43:05.153+00:00"},"s":"I", "c":"CONTROL", "id":20698, "ctx":"main","msg":"***** SERVER RESTARTED *****"}
2023-08-26 20:43:05 {"t":{"$date":"2023-08-26T17:43:05.171+00:00"},"s":"I", "c":"NETWORK", "id":4915701, "ctx":"main","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":21},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":21},"outgoing":{"minWireVersion":6,"maxWireVersion":21},"isInternalClient":true}}}
12-Admin Processes: Run admin/management tasks as one-off processes
This factor refers seperation admin or management task seperately as standalone one-off processes. This separation helps maintain the stability and performance of the main application while allowing for controlled execution of administrative actions. This processes are such as Database migrations, Data Seeding and Environment Setup.
That was all I want to write about 12 factor app methodology concept. I am learning a lot while I am writing I hope you learned a lot too while you are reading and enjoyed it. Don’t forget to follow on Medium to keep going to learn together. See you soon :)
References: