<article>

  <h2 id="iv_backing_services">IV. Backing services</h2>

  <h3 id="treat_backing_services_as_attached_resources">Treat backing services as attached resources</h3>

  <p>A
    <em>backing service</em> is any service the app consumes over the network as part of its normal operation. Examples include
    datastores (such as
    <a href="http://dev.mysql.com/" target="_blank">MySQL</a> or
    <a href="http://couchdb.apache.org/" target="_blank">CouchDB</a>), messaging/queueing systems (such as
    <a href="http://www.rabbitmq.com/" target="_blank">RabbitMQ</a> or
    <a href="http://kr.github.com/beanstalkd/" target="_blank">Beanstalkd</a>), SMTP services for outbound email (such as
    <a href="http://www.postfix.org/" target="_blank">Postfix</a>), and caching systems (such as
    <a href="http://memcached.org" target="_blank">Memcached</a>.
  </p>

  <p>Backing services like the database are traditionally managed by the same systems administrators as the app’s runtime deploy.
    In addition to these locally-managed services, the app may also have services provided and managed by third parties.
    Examples include SMTP services (such as
    <a href="http://postmarkapp.com/" target="_blank">Postmark</a>), metrics-gathering services (such as
    <a href="http://newrelic.com/" target="_blank">New Relic</a> or
    <a href="http://www.loggly.com/" target="_blank">Loggly</a>), binary asset services (such as
    <a href="http://aws.amazon.com/s3/" target="_blank">Amazon S3</a>), and even API-accessible consumer services (such as
    <a href="http://dev.twitter.com/" target="_blank">Twitter</a>,
    <a href="http://code.google.com/apis/maps/index.html" target="_blank">Google Maps</a>, or
    <a href="http://www.last.fm/api" target="_blank">Last.fm</a>).</p>

  <p>
    <strong>The code for a twelve-factor app makes no distinction between local and third party services.</strong> To the app, both
    are attached resources, accessed via a URL or other locator/credentials stored in the
    <a href="./config">config</a>. A
    <a href="./codebase">deploy</a> of the twelve-factor app should be able to swap out a local MySQL database with one managed by a third party
    (such as
    <a href="http://aws.amazon.com/rds/" target="_blank">Amazon RDS</a>) without any changes to the app’s code. Likewise, a local SMTP server could be swapped with a third-party
    SMTP service (such as Postmark) without code changes. In both cases, only the resource handle in the config needs to
    change.
  </p>

  <p>Each distinct backing service is a
    <em>resource</em>. For example, a MySQL database is a resource; two MySQL databases (used for sharding at the application
    layer) qualify as two distinct resources. The twelve-factor app treats these databases as
    <em>attached resources</em>, which indicates their loose coupling to the deploy they are attached to.</p>

  <p>
    <img alt='A production deploy attached to four backing services.' class='full' src='.././assets/images/attached-resources.png'
    />
  </p>

  <p>Resources can be attached and detached to deploys at will. For example, if the app’s database is misbehaving due to a hardware
    issue, the app’s administrator might spin up a new database server restored from a recent backup. The current production
    database could be detached, and the new database attached – all without any code changes.</p>

</article>
