From af0180abc960f72ba5717335300db364e7f11ff1 Mon Sep 17 00:00:00 2001 From: Adam Wiggins Date: Sun, 5 Jun 2011 01:09:33 -0700 Subject: [PATCH] 12th factor: disposability (sliced off of processes) --- content/admin-processes.md | 2 +- content/dev-prod-parity.md | 2 +- content/disposability.md | 14 ++++++++++++++ content/logs.md | 2 +- content/processes.md | 14 +------------- content/toc.md | 11 +++++++---- web.rb | 2 +- 7 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 content/disposability.md diff --git a/content/admin-processes.md b/content/admin-processes.md index 1891079..683fdc4 100644 --- a/content/admin-processes.md +++ b/content/admin-processes.md @@ -1,4 +1,4 @@ -## XI. Admin processes +## XII. Admin processes ### One-off admin/management tasks The [process formation](/concurrency) represents the array of processes that are used to do the app's regular business (such as handling web requests) as it runs. Separately, developers will often wish to do one-off administrative or maintenance tasks for the app, such as: diff --git a/content/dev-prod-parity.md b/content/dev-prod-parity.md index 9fc7ca7..d21ec2d 100644 --- a/content/dev-prod-parity.md +++ b/content/dev-prod-parity.md @@ -1,4 +1,4 @@ -## IX. Dev/prod parity +## X. Dev/prod parity ### Parity between development and production Historically, there is a substantial gap between development (a developer making live edits to a local [deploy](/codebase) of the app) and production (a running deploy of the app accessed by end users). This is a gap in time: a developer may work on code that doesn't go to production for days, weeks, or even months. It's a gap in personnel: developers write code, ops engineers deploy it. And it's a gap in environments: a developer may be using Nginx, SQLite, and OS X, while the production deploy uses Apache, MySQL, and Linux. diff --git a/content/disposability.md b/content/disposability.md new file mode 100644 index 0000000..368e77b --- /dev/null +++ b/content/disposability.md @@ -0,0 +1,14 @@ +## IX. Disposability +### Fast startup and graceful shutdown maximize robustness + +The twelve-factor app's [processes](/processes) are architected to be stopped and started at a moment's notice, facilitating fast elastic scaling and rapid deployment of [code](/codebase) or [config](/config) changes. This is a key element in ensuring robustness of a production deployment. + +Processes shut down gracefully when they receive a [SIGTERM](http://en.wikipedia.org/wiki/SIGTERM) signal from the process manager. For a web process, graceful shutdown is achieved by ceasing to listen on the service port (thereby refusing any new requests), allowing any current requests to finish, and then exiting. Implicit in this model is that HTTP requests are short (no more than a few seconds), or in the case of long polling, the client is written to seamlessly attempt a reconnect in the case of losing the long-poll connection. + +For a worker process, graceful shutdown is achieved by returning the current job to the work queue. For example, on RabbitMQ the worker can send a `NACK`; on Beanstalkd, the job is returned to the queue automatically whenever a worker disconnects. Lock-based systems such as Delayed Job need to be sure to release their lock on the job record. Implicit in this model is that all jobs are [reentrant](http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29), which typically is achieved by wrapping the results in a transaction, or making the operation [idempotent](http://en.wikipedia.org/wiki/Idempotence). + +In this model, minimizing startup time of any individual app process is desirable. Ideally, a process takes a few seconds from the time the launch command is executed until the process is up and ready to receive requests or jobs. But even a large app should try to keep its startup to to no more than 30 seconds. Briefer startup time provides more agility for deploys, code changes, scaling up; and it aids robustness, because the process manager can more easily move processes to new physical machines when warranted. + +Processes should also be robust against sudden death, in the case of a failure in the underlying hardware. While this is a much less common occurrence than a graceful shutdown with `SIGTERM`, it can still happen. Use of a robust queueing backend (such as Beanstalkd) that returns jobs to the queue when clients disconnect or time out, can make all the difference here. Either way, a twelve-factor app is architected to handle unexpected, non-graceful terminations. [Crash-only design](http://lwn.net/Articles/191059/) takes this concept to its [logical extreme](http://couchdb.apache.org/docs/overview.html). + + diff --git a/content/logs.md b/content/logs.md index 8e0b669..7927bdc 100644 --- a/content/logs.md +++ b/content/logs.md @@ -1,4 +1,4 @@ -## X. Logs +## XI. Logs ### Logs are event streams *Logs* provide visibility into the behavior of a running app. While they are sometimes written to a file on disk, this is only an output format - not the essence of what logs really are. diff --git a/content/processes.md b/content/processes.md index b2ab1d9..1ce8a93 100644 --- a/content/processes.md +++ b/content/processes.md @@ -1,5 +1,5 @@ ## VI. Processes -### Stateless, disposable processes handle application logic +### Stateless processes handle application logic The business logic of an app happens in the app code. This code gets executed in the execution environment as one or more *processes*. @@ -13,15 +13,3 @@ Asset packagers (such as [Jammit](http://documentcloud.github.com/jammit/) or [d Some web systems rely on "sticky sessions" - that is, caching user session data in memory of the app's process and expecting future requests from the same visitor to be routed to the same process. Sticky sessions are a violation of twelve-factor and should never be used or relied upon. Session state is a good candidate for a datastore that offers time-expiration, such as Memcached or Redis. -#### Processes are disposable - -Processes should be architected to be stopped and started at a moment's notice, facilitating fast elastic scaling and rapid deployment of code or config changes. This is a key element in ensuring robustness of a production deployment. - -Processes should be prepared to shut down gracefully at any time on receiving a [SIGTERM](http://en.wikipedia.org/wiki/SIGTERM) signal from the process manager. For a web process, graceful shutdown is achieved by ceasing to listen on the service port (thereby refusing any new requests), allowing any current requests to finish, and then exiting. Implicit in this model is that HTTP requests are short (no more than a few seconds), or in the case of long polling, the client is written to seamlessly attempt a reconnect in the case of losing the long-poll connection. - -For a worker process, graceful shutdown is achieved by returning the current job to the work queue. For example, on RabbitMQ the worker can send a `NACK`; on Beanstalkd, the job is returned to the queue automatically whenever a worker disconnects. Lock-based systems such as Delayed Job need to be sure to release their lock on the job record. Implicit in this model is that all jobs are [reentrant](http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29), which typically is achieved by wrapping the results in a transaction, or making the operation [idempotent](http://en.wikipedia.org/wiki/Idempotence). - -Developers should attempt to keep process startup time low. Ideally, it should only take a few seconds from the time the launch command is executed until the process is up and ready to receive requests or jobs. But even a large app should try to keep its startup to to no more than 30 seconds. Briefer startup time provides more agility for deploys, code changes, scaling up; moreover, it helps robustness, because the process manager can more easily move processes to new physical machines when warranted. - -Processes should also be robust against sudden death, in the case of a failure in the underlying hardware. While this is a much less common occurrence than a graceful shutdown with `SIGTERM`, it can still happen. Use of a robust queueing backend (such as Beanstalkd) that returns jobs to the queue when clients disconnect or time out, can make all the difference here. Either way, a twelve-factor app is architected to handle unexpected, non-graceful terminations. [Crash-only design](http://lwn.net/Articles/191059/) takes this concept to its [logical extreme](http://couchdb.apache.org/docs/overview.html). - diff --git a/content/toc.md b/content/toc.md index bc718d2..753574b 100644 --- a/content/toc.md +++ b/content/toc.md @@ -17,7 +17,7 @@ The Twelve Factors ### Strict separation of build stage and run stage ## [VI. Processes](/processes) -### Stateless, disposable processes handle application logic +### Stateless processes handle application logic ## [VII. Port binding](/port-binding) ### Services exported via port binding @@ -25,11 +25,14 @@ The Twelve Factors ## [VIII. Concurrency](/concurrency) ### Scale up via the process model -## [IX. Dev/prod parity](/dev-prod-parity) +## [IX. Disposability](/disposability) +### Fast startup and graceful shutdown maximize robustness + +## [X. Dev/prod parity](/dev-prod-parity) ### Parity between development and production -## [X. Logs](/logs) +## [XI. Logs](/logs) ### Logs are event streams -## [XI. Admin processes](/admin-processes) +## [XII. Admin processes](/admin-processes) ### One-off admin/management tasks diff --git a/web.rb b/web.rb index d221b73..a53c0cc 100644 --- a/web.rb +++ b/web.rb @@ -5,7 +5,7 @@ get '/' do erb :home end -TOC = %w(codebase dependencies config backing-services build-release-run processes port-binding concurrency dev-prod-parity logs admin-processes) +TOC = %w(codebase dependencies config backing-services build-release-run processes port-binding concurrency disposability dev-prod-parity logs admin-processes) get '/:factor' do |factor| halt 404 unless TOC.include?(factor)