Header

What is The Twelve-Factor App Methodology?

Open Source, Tips & Tricks, and What Is

Post Image

The Twelve-Factor App methodology is a set of best practices for building modern, scalable, and maintainable software-as-a-service (SaaS) applications. Created by developers at Heroku, these guidelines help teams create applications that are both portable and resilient. In this post, we'll explore each factor and understand why they're crucial for modern application development.

1. Codebase

One codebase tracked in revision control, many deploys. Your application should have a single codebase managed through version control (like Git), which can be deployed to multiple environments (development, staging, production).

# Example repository structure
my-application/
├── src/
├── tests/
├── .git/
└── README.md

2. Dependencies

Explicitly declare and isolate dependencies. Your application should clearly declare all its dependencies using a dependency management system.

// Example package.json
{
  "dependencies": {
    "express": "4.17.1",
    "mongoose": "5.12.3"
  }
}

3. Config

Store configuration in the environment. Configuration that varies between deployments should be stored in environment variables, not in the code.

// Instead of hardcoding configuration
const config = {
  databaseUrl: process.env.DATABASE_URL,
  apiKey: process.env.API_KEY
};

4. Backing Services

Treat backing services as attached resources. External services (databases, caches, message brokers) should be accessed via URLs or other locators stored in the config.

5. Build, Release, Run

Strictly separate build and run stages. The deployment process should have distinct build, release, and run stages.

# Example deployment flow
build → create artifacts
release → combine artifacts with config
run → execute the application

6. Processes

Execute the app as one or more stateless processes. Applications should be stateless and share-nothing. Any needed state should be stored in a backing service.

7. Port Binding

Export services via port binding. Your application should be self-contained and expose services through port binding.

const port = process.env.PORT || 3000;
app.listen(port);

8. Concurrency

Scale out via the process model. Applications should be able to scale horizontally by adding more processes rather than making processes bigger.

9. Disposability

Maximize robustness with fast startup and graceful shutdown. Processes should be disposable, meaning they can be started or stopped at a moment's notice.

10. Dev/Prod Parity

Keep development, staging, and production as similar as possible. All environments should be as similar as possible to avoid deployment surprises.

11. Logs

Treat logs as event streams. Your application shouldn't concern itself with storing or managing logs. Instead, log events should be treated as streams.

// Log to stdout instead of files
console.log(`Processing order ${orderId}`);

12. Admin Processes

Run admin/management tasks as one-off processes. Administrative tasks should be run as one-off processes in an identical environment as the regular application.

Why Use The Twelve-Factor App Methodology?

The Twelve-Factor methodology offers several benefits:

  • Portability: Applications can be deployed on any platform that supports the runtime environment
  • Scalability: Easy horizontal scaling and addition of resources
  • Maintainability: Clear separation of concerns and standardized practices
  • Continuous Deployment: Simplified deployment process
  • Cloud-Native Ready: Perfect for cloud platforms and modern hosting services

Implementing with DeployHQ

DeployHQ supports the Twelve-Factor methodology through various features:

  • Environment Variables: Easily manage different configurations for various environments, using configuration files (environment variables are coming soon)
  • Build Commands: Automate your build process using our Build Pipeline
  • Deployment Scripts: Handle complex deployment scenarios using Deployment Scripts
  • Multiple Servers: Deploy to different environments with ease using Deployment Strategies
  • Version Control Integration: Track your codebase changes effectively directly from your Codebase

Conclusion

The Twelve-Factor App methodology provides a solid foundation for building modern applications. By following these principles, you can create applications that are easier to develop, deploy, and scale. When combined with DeployHQ's powerful deployment features, you can ensure your applications follow these best practices while maintaining an efficient deployment workflow.

Ready to implement the Twelve-Factor methodology in your deployments? Sign up for DeployHQ and start optimising your deployment process today.


This post is part of our "What Is" series, helping developers understand key concepts and methodologies in modern software development.

A little bit about the author

Facundo is the CTO at DeployHQ. He oversees our software engineering team by day and, in his free time, enjoys hobbies such as cycling, spending time in nature, and the company of Bono 🐶

Tree

Proudly powered by Katapult. Running on 100% renewable energy.