In the modern era, software is commonly delivered as a service: called web apps, or software-as-a-service. The twelve factor app is a methodology for building software-as-a-service apps that:
•Use declarative formats for setup automation, to minimize time and cost for new developers joining the project;
•Have a clean contract with the underlying operating system, offering maximum portability between execution environments;
•Are suitable for deployment on modern cloud platforms, obviating the need for servers and systems administration;
•Minimize divergence between development and production, enabling continuous deployment for maximum agility;
•And can scale up without significant changes to tooling, architecture, or development practices.
The twelve-factor methodology can be applied to apps written in any programming language, and which use any combination of backing services (database, queue, memory cache, etc).
Who should read this document?
Any developer building applications which run as a service. Ops engineers who deploy or manage such applications.
High Level Snapshot of 12 Factors
The Twelve Factors Applied to Microservices
One codebase per service, tracked in revision control; many deploys
The Twelve Factor App recommends one codebase per app. In a microservices architecture, the correct approach is actually one codebase per service. Additionally, we strongly recommend the use of Git as a repository, because of its rich feature set and enormous ecosystem. GitHub has become the default Git hosting platform in the open source community, but there are many other excellent Git hosting options, depending on the needs of your organization.
Explicitly declare and isolate dependencies
As suggested in The Twelve Factor App, regardless of what platform your application is running on, use the dependency manager included with your language or framework. How you install operating system or platform dependencies depends on the platform:
•In noncontainerized environments, use a configuration management tool (Chef, Puppet, Ansible) to install system dependencies.
•In a containerized environment, do this in the Docker file.
Store configuration in the environment
Anything that varies between deployments can be considered configuration. The Twelve Factor App guidelines recommend storing all configuration in the environment, rather than committing it to the repository. We recommend the following specific practices:
•Use non‑version controlled .env files for local development. Docker supports the loading of these files at runtime.
•Keep all .env files in a secure storage system, such as Vault, to keep the files available to the development teams, but not committed to Git.
•Use an environment variable for anything that can change at runtime, and for any secrets that should not be committed to the shared repository.
•Once you have deployed your application to a delivery platform, use the delivery platform’s mechanism for managing environment variables.
Treat backing services as attached resources
The Twelve Factor App guidelines define a backing service as “any service the app consumes over the network as part of its normal operation.” The implication for microservices is that anything external to a service is treated as an attached resource, including other services. This ensures that every service is completely portable and loosely coupled to the other resources in the system. Additionally, the strict separation increases flexibility during development – developers only need to run the service(s) they are modifying, not others.
Build, Release, Run
Strictly separate build and run stages
To support strict separation of build, release, and run stages, as recommended by The Twelve Factor App, we recommend the use of a continuous integration/continuous delivery (CI/CD) tool to automate builds. Docker images make it easy to separate the build and run stages. Ideally, images are created from every commit and treated as deployment artifacts.
Execute the app in one or more stateless processes
For microservices, the important point in the Processes factor is that your application needs to be stateless. This makes it easy to scale a service horizontally by simply adding more instances of that service. Store any state-ful data, or data that needs to be shared between instances, in a backing service.
Each service manages its own data
As a modification to make the Port binding factor more useful for microservices, we recommend that you allow access to the persistent data owned by a service only via the service’s API. This prevents implicit service contracts between microservices and ensures that microservices can’t become tightly coupled. Data isolation also allows the developer to choose, for each service, the type of data store that best suits its needs.
Scale out via the process model
The Unix process model is largely a predecessor to a true microservices architecture, insofar as it allows specialization and resource sharing for different tasks within a monolithic application. In a microservices architecture, you can horizontally scale each service independently, to the extent supported by the underlying infrastructure. With containerized services, you further get the concurrency recommended in the Twelve‑Factor App, for free.
Maximize robustness with fast startup and graceful shutdown
Instances of a service need to be disposable so they can be started, stopped, and redeployed quickly, and with no loss of data. Services deployed in Docker containers satisfy this requirement automatically, as it’s an inherent feature of containers that they can be stopped and started instantly. Storing state or session data in queues or other backing services ensures that a request is handled seamlessly in the event of a container crash. We are also proponents of using a backing store to support crash‑only design.
Keep development, staging, and production as similar as possible
Keep all of your environments – development, staging, production, and so on – as identical as possible, to reduce the risk that bugs show up only in some environments. To support this principle, we recommend, again, the use of containers – a very powerful tool here, as they enable you to run exactly the same execution environment all the way from local development through production. Keep in mind, however, that differences in the underlying data can still cause differences at runtime.
Treat logs as event streams
Instead of including code in a microservice for routing or storing logs, use one of the many good log‑management solutions on the market, several of which are listed in the Twelve‑Factor App. Further, deciding how you work with logs needs to be part of a larger APM and/or PaaS strategy.
Run admin and management tasks as one‑off processes
In a production environment, run administrative and maintenance tasks separately from the app. Containers make this very easy, as you can spin up a container just to run a task and then shut it down.
Use the Twelve‑Factor App and these additional principles to help you create robust microservices‑based apps that are optimized for continuous delivery.
Explore more at teknonauts.
Ashish is technology freak with around 11+ years of experience in IT Landscape. He started his journey as a device driver programmer in 2010. From that point he has evolved as a Mobility and IOT Architect, he completed his M.Tech from BITS Pilani as System Architect. He is working as Principal Architect at Oracle currently.