Django in multiple regions with Postgres

hiya! I’m considering deploying an app on to take advantage of how nice multi-region stuff seems to be, but I’m a bit worried about the database stuff. It seems like for writes to the database I’d want to use the replay header to send the request to the same region as where my database is, but I’m not sure how exactly I’d do that with Django. I think I’d just want to use middleware to replay all HTTP POST requests in the database’s region, but I’m worried that this won’t be enough. I’m not sure how exactly Django does database stuff for things like sessions. Can the session backend update like an expiry time or something on a GET request?

It seems like other frameworks have libraries from to resolve this but I didn’t see one for Django.

Hi @catgirl

It depends on what database you’re using and how you’ve setup your database.

If you’re only using a single VM for your database then you don’t really need to worry about that stuff as you can connect to it and will route the connection to your VM no matter which region the connection originates from (there’s no separation between regions).

The only thing to take into consideration is that if you have your database in one region and your backend server in another region, there will be latency overhead for each query depending on the distance between the two regions.


I’d be using Fly Postgres.

What about in cases where my backend is in multiple regions along with read replicas? It seems like I’d need something in my Django app to handle replaying writes in the primary region, a Django equivalent of fly-ruby.

The trickery from fly-ruby is pretty much all at the app level and fairly trivial to implement yourself.

The basic principle is that an http request comes in, you handle it on the closest available VM. That VM is connected to the closest available database replica. If the HTTP tries to perform a database write and the replica is read-only then your postgres library will throw an error. You then need to catch that error and return an HTTP 409 response with the Fly-Replay header set with the value region=[primary DB region] .

1 Like