Skip to content
lorebow

Backing services

Databases, caches, queues and mail servers are resources the app attaches to by URL, and can swap without a code change.

KFounder and editor 170 words 1 min read Find it on the map
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.

Swap a database without touching codepostgres://localhost/shopStep 1 of 3
AppDATABASE_URLPostgreson the same VMPostgresmanaged serviceRedisREDIS_URL

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

db.pypython
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

ServiceExampleAttached by
DatabasePostgreSQL, MySQLDATABASE_URL
CacheRedis, MemcachedREDIS_URL
QueueRabbitMQ, SQSAMQP_URL
EmailPostmark, any SMTP serverSMTP_URL
File storageS3, Cloud StorageS3_BUCKET plus credentials

Referenced by