HACKER Q&A
📣 Jonnax

How to distribute/sync a postgresdb geographically?


So I'm wondering, if I create a Django application that uses a Postgres DB, and I want to be able to deploy it in multiple locations to reduce latency.

How would I ensure that an update in user data against one dB is synced with the other DBs.

Assuming there are DBs in America, Asia and Europe?

Are there any solutions to do syncing on the database level?

Does Django provide any of this functionality?

Should the problem be approached from the database level or the application level?


  👤 ThePhysicist Accepted Answer ✓
If you’re fine with a single master that you write to and geographically distributed slaves for reading it’s not so difficult, you can use WAL streaming or log shipping to keep your slaves in sync with your master. You can use some additional tooling (e.g. haproxy and heartbeat) to do an automated failover in case your master goes down. Building a fully distributed multi-master setup is not possible with native Postgres as far as I know, there are commercial offerings like CitusDB that might help you with that though.

👤 nuggien
I don't know of any good native postgres solutions, but you could try https://www.cockroachlabs.com/ which may or may not be a drop in replacement for postgres, depending on your use cases.

👤 atsaloli
This is called master-master replication or multi-master replication. Postgres has been working toward it for years, and making progress. Not an easy problem to solve and get right. Postgres is not there yet but closer than before.