Backing services
Databases, caches, queues and mail servers are resources the app attaches to by URL, and can swap without a code change.
On this page
Treat backing services as attached resources. A backing service is anything the app talks to over the network to do its job: a database, a cache like Redis, a message queue, an SMTP server, an object store.
The rule is that the app makes no distinction between a service you run and one someone else runs. Both are just a URL in the config. Swap one for the other, and the code doesn't change.
1.Attached by URL
The app connects to whatever DATABASE_URL points at. Today that's a Postgres server on the same machine.
#In code
import os
from sqlalchemy import create_engine
import redis
engine = create_engine(os.environ["DATABASE_URL"])
cache = redis.Redis.from_url(os.environ["REDIS_URL"])Nothing in this file knows whether Postgres runs on the same machine, in a container next door, or at a cloud provider.
#What counts as a backing service
| Service | Example | Attached by |
|---|---|---|
| Database | PostgreSQL, MySQL | DATABASE_URL |
| Cache | Redis, Memcached | REDIS_URL |
| Queue | RabbitMQ, SQS | AMQP_URL |
| Postmark, any SMTP server | SMTP_URL | |
| File storage | S3, Cloud Storage | S3_BUCKET plus credentials |
Referenced by