<< back to Guides

๐Ÿงช Introduction to The Twelve-Factor App

The Twelve-Factor App is a methodology for building software-as-a-service (SaaS) apps that are portable, resilient, and scalable. Originally developed by engineers at Heroku, it defines twelve principles that guide the development of cloud-native applications.

This guide walks through each factor with clear explanations and practical examples.


๐Ÿงฑ 1. Codebase

One codebase tracked in version control, many deploys

โœ… Best Practice: Use Git with clearly separated branches per environment.


โš™๏ธ 2. Dependencies

Explicitly declare and isolate dependencies

// Node.js example
{
  "dependencies": {
    "express": "^4.18.2"
  }
}

๐Ÿ”ง 3. Config

Store configuration in the environment

// Bad
db.connect('postgres://user:pass@localhost')

// Good
db.connect(process.env.DATABASE_URL)

๐Ÿ’พ 4. Backing Services

Treat backing services as attached resources

Examples:


๐Ÿš€ 5. Build, Release, Run

Strictly separate build and run stages

Each stage should be repeatable and idempotent.


๐Ÿ‘ท 6. Processes

Execute the app as one or more stateless processes


๐Ÿ›ฃ๏ธ 7. Port Binding

Export services via port binding

// Express.js example
app.listen(process.env.PORT || 3000)

๐Ÿงช 8. Concurrency

Scale out via the process model

Example: In a Procfile (Heroku format):

web: node server.js
worker: node worker.js

โณ 9. Disposability

Maximize robustness with fast startup and graceful shutdown


๐Ÿ“ˆ 10. Dev/Prod Parity

Keep development, staging, and production as similar as possible


๐Ÿ“‹ 11. Logs

Treat logs as event streams

// Example log output
console.log("User signed in", user.id)

๐Ÿ› ๏ธ 12. Admin Processes

Run admin/management tasks as one-off processes

// Example: DB migration command
node migrate.js

๐Ÿ“Œ Summary Table

# Factor Purpose
1 Codebase Single repo, many deploys
2 Dependencies Explicit and isolated
3 Config Store in env vars
4 Backing Services Treat as attached, swappable resources
5 Build, Release, Run Separate stages of deployment
6 Processes Stateless, share-nothing architecture
7 Port Binding Self-contained service via ports
8 Concurrency Scale via process model
9 Disposability Fast startup and shutdown
10 Dev/Prod Parity Keep environments as similar as possible
11 Logs Stream logs, external aggregation
12 Admin Processes One-off admin tasks as standalone processes

๐Ÿ“š Further Reading


<< back to Guides