SaaS: The Twelve-Factor app methodology explained
Tags: saas, cloud, devops, serverless
Published: 2021-06-20 23:45:40

The Twelve-Factor App methodology is a methodology for building software-as-a-service applications. These best practices are designed to enable applications to be built with portability and resilience when deployed to the web.  
![12-factors_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uyn56ay1tf8dsc5o7ej.jpg)
To understand this methodology we can divide the twelve factors into 3 key components:
![12-factors-3-components](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mop1e47n55jgyzp5ugzm.png) 
Next, we'll see the best practices that you should follow for every factor to comply with what this methodology says.
##Code Factors:  
###Factor 1 - CodeBase:
![codebase_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88bat7esyojurnh3a8eu.png) 
- Code must be tracked in a version control system (VCS) like Github, BitBucket, etc. 
- One-to-One relationship between the CodeBase and the application. 
- There can be multiple deploys of the application.
- Different versions of the CodeBase can be in each deployment.
   
###Factor 5 - Build, release and run:
![build_release_run_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqdkfa5ujzqt3hlctzj8.png)
- Build: Transform a CodeBase into an executable unit called build.
- Release: Combine build with configuration so that it's ready to run.
- Run: Runs the application.  

###Factor 10 - Dev/Prod parity:
![dev_prod_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pcf5i767q74tvl8alwp.png)
- Minimize differences between deployment and production environments.
- Back-end services should be the same across environments (dev/prod)  

##Deploy Factors:  

###Factor 2 - Dependencies:  
![dependecy_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwo8742ttq3rbi120am5.jpeg)
- Be aware that an app is only reliable as its least reliable dependency.
- Be sure that code explicitly declares any dependency.
  
###Factor 3 - Config:  
![config_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tivn8wk9v1yvqarq47c.png)
- Config contains everything that varies between deploys such as credentials and backing services locations.
- Configs must be kept separate from the code
- Store config in environment variables.  

###Factor 4 - Backing services:  
![back_end_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r94g7io1nlu15tdsocq7.png)
- Applications should not distinguish between local and third-party back-end services.
- All services should be accessed by URL and credentials so that can be swapped without changing the code.  

###Factor 6 - Processes:  
![process_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hcklo53ws070o5i0srs.png)
- Stateless and share nothing.
- Backing services store persistent data since memory and filesystems are not shared across processes.
- Data is centrally stored.  

###Factor 7 - Port binding:  
![ports_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67qg3oel4zzh9bxw1980.png)
- Export services by port binding. HTTP and other services are exported in this way.
- To bind a port usually you must declare a web server library.
- Applications can be backing services for other applications.  

###Factor 9 - Disposability:  
![disposability_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po4etp9xuwa9543758a3.png)
- Applications should have minimal process start-time and graceful termination.
- Quickly deploy code and configure changes.
- Easily scale applications. 
 
###Factor 11 - Logs:  
![logs_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7y490pmgjd6yljdfnyx.png)
- Applications should not concern themselves with storing logs.
- Applications should trend logs as event stream written to stdout.
- Execution environment captures the stream for all apps aggregates the logs and routes logs to their destination.

##Operate Factors:

###Factor 8 - Concurrency:  
![concurrency_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xlapzhnskergfq9tqs7.png)
- Concurrent processes can be used to scale the application.
- Stateless processes can be spun up without creating dependencies on other processes.  

##Factor 12 - Admin processes:  
![admin_process_img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goltagu96akqskkt2uzc.png)
- Enable one-off application management processes such as database migration.
- Run against a release using the same CodeBase configuration.
- Are included in the application code.

###Sources:
- The Twelve-Factors Methodology website: [12factor](https://12factor.net/)