Skip to content

Commit

Permalink
12th factor: disposability (sliced off of processes)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wiggins committed Jun 5, 2011
1 parent 8e7f6e8 commit af0180a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion 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:
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down
14 changes: 14 additions & 0 deletions 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).


2 changes: 1 addition & 1 deletion 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.
Expand Down
14 changes: 1 addition & 13 deletions 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*.

Expand All @@ -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).

11 changes: 7 additions & 4 deletions content/toc.md
Expand Up @@ -17,19 +17,22 @@ 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

## [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
2 changes: 1 addition & 1 deletion web.rb
Expand Up @@ -5,7 +5,7 @@
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)
Expand Down

0 comments on commit af0180a

Please sign in to comment.