12-Factor App Principles
The 12-Factor App methodology is a set of principles for building scalable, maintainable, cloud-native applications. These principles make your app easy to deploy to any cloud (AWS, GCP, Azure), horizontally scalable, and operationally simple.
Key 12-Factor Factors
- **I. Codebase**: One repo, many deploys (dev/staging/prod). Never split one app into multiple repos; never bundle multiple apps into one repo
- **II. Dependencies**: Explicitly declare all dependencies in package.json. Never rely on system-wide packages. npm ci in Docker ensures reproducible installs
- **III. Config**: Store config in environment variables — NOT in code or config files committed to git. Anything that changes between deployments: DATABASE_URL, JWT_SECRET, REDIS_URL
- **IV. Backing Services**: Treat databases, caches, message queues as attached resources — connected via URLs. Swap from local Postgres to AWS RDS by changing DATABASE_URL (zero code change)
- **VI. Processes**: Execute the app as stateless processes. No local file storage, no in-memory sessions. Store state in backing services (Redis, S3, DB)
- **VII. Port Binding**: Export services via port binding. Your app binds to $PORT and accepts connections — the load balancer routes to it
- **VIII. Concurrency**: Scale out via the process model — run more instances. Supported by cluster mode and horizontal scaling behind a load balancer
- **IX. Disposability**: Fast startup (< 5s), graceful shutdown (drain in-flight requests, close DB connections). Enables zero-downtime deploys and auto-scaling
- **XI. Logs**: Treat logs as event streams. Write to stdout/stderr — never to log files. Infrastructure collects them
Tip
Tip
Practice 12Factor App Principles in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Alpine + multi-stage + non-root = production-ready.
Practice Task
Note
Practice Task — (1) Write a working example of 12Factor App Principles from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with 12Factor App Principles is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready node code.
Key Takeaways
- The 12-Factor App methodology is a set of principles for building scalable, maintainable, cloud-native applications.
- *I. Codebase**: One repo, many deploys (dev/staging/prod). Never split one app into multiple repos; never bundle multiple apps into one repo
- *II. Dependencies**: Explicitly declare all dependencies in package.json. Never rely on system-wide packages. npm ci in Docker ensures reproducible installs
- *III. Config**: Store config in environment variables — NOT in code or config files committed to git. Anything that changes between deployments: DATABASE_URL, JWT_SECRET, REDIS_URL