API / Microservices
Explain 12-Factor App methodology.
Building microservices using the 12-Factor App methodology ensures consistency, scalability, and ease of deployment in cloud environments.
- Codebase: One codebase tracked in revision control, many deploys. Each microservice should have its own repository to ensure independent lifecycle management.
- Dependencies: Explicitly declare and isolate dependencies. Use tools like Maven, Gradle, or Docker to ensure the app doesn't rely on pre-installed system libraries.
- Config: Store configuration in the environment. Keep credentials and resource handles in environment variables rather than hardcoding them in the source.
- Backing Services: Treat backing services as attached resources. Databases, caches, and queues should be interchangeable via configuration changes without modifying code.
- Build, Release, Run: Strictly separate build and run stages. A build is transformed into a release by adding config, which is then executed in the run environment.
- Processes: Execute the app as one or more stateless processes. Persist any data that needs to survive a restart in a stateful backing service like a database.
- Port Binding: Export services via port binding. The app should be self-contained and listen for requests on a specific port rather than relying on an external web server injector.
- Concurrency: Scale out via the process model. Handle increased load by adding more instances of the process (horizontal scaling) rather than just making one process bigger.
- Disposability: Maximize robustness with fast startup and graceful shutdown. Apps should be able to start or stop at a moment's notice without data loss or long delays.
- Dev/Prod Parity: Keep development, staging, and production as similar as possible. This minimizes "it works on my machine" bugs by using the same backing services across all environments.
- Logs: Treat logs as event streams. The app should write to
stdout, leaving the aggregation and storage of those logs to the execution environment or specialized tools. - Admin Processes: Run admin/management tasks as one-off processes. Scripts for database migrations or maintenance should run in the same environment and against the same release as the app.
More Related questions...