The 12 factor app principle is a methodology or set of principles for building the scalable and performant, independent, and most resilient enterprise applications. It is very popular as it aligns with the Microservice principle.
1. Codebase:
One codebase tracked in version control, many deploys.
2. Dependencies:
Explicitly declare and isolate the dependencies.
From a java project perspective, think of Pom.xml, where you can declare all your dependencies.
3. Config:
Store config in the environment.
4. Backing Services:
Treat backing services as attached resources.
Backing services like – Database, Message Brokers, or any other services the app uses should be treated as an attached resources.
5. Build, Release, Run:
Strictly separate build and run stages.
6. Processes:
Execute the app as one or more stateless processes.
Any session data should not be stored in the app itself, rather it should be saved in a database.
7. Port Binding:
Export services via port binding.
Web apps generally need a container to get executed, for eg., a java app need Tomcat to run the application. As per the twelve-factor app principle your app should be self reliant and does not rely on runtime injection of a webserver into the execution environment to create a web-facing service. The web app exports HTTP as a service by binding to a port, and listening to requests coming in on that port.
In a local development environment, the developer visits a service URL like http://localhost:5000/
to access the service exported by their app. In deployment, a routing layer handles routing requests from a public-facing hostname to the port-bound web processes.
In short, this is all about having your application as a standalone instead of deploying them into any of the external web servers. Spring boot is one example of this one. Spring boot by default comes with embedded tomcat, jetty, or undertow.
8. Concurrency:
Scale out via the process model.
We should opt for horizontal scaling instead of vertical scaling. By adopting the containerization, applications can be scaled horizontally as per the demands.
Vertical scaling : Add additional hardware to the system.
Horizontal scaling : Add additional instances of the application.
9. Disposability:.
Fast startup and graceful shutdown
Using containerization the applications can be started or stopped quickly,
10. Dev/Prod Parity:
Keep development, staging and production environment as similar as possible.
11. Logs:
Treat logs as event streams.
12. Admin Processes:
Run admin/management tasks as one-off processes.
While deploying an app to an environment, there are some admin/management processes that has to be executed prior to that. These can be data migration scripts, One-Off DB scripts, etc. Twelve-factor principle advocates for keeping those admin/management processes as part of the codebase, so that it will also follow the same process that is defined for your app codebase.