Database for multiregional projects
Hello, I'd like to ask what the best practices are for databases serving users in multiple regions. In my case, majority of my users will be coming from either USA or Sweden (Europe). So ideally, I'd like to spin up one database in USA and one in Europe and have users connected to the one with the best latency for them.
But I'm a bit unsure whether this is really possible in any good way, without it being very "hacky".
Currently using: nextjs, payloadcms, vercel, neondb.
I'd assume majority of the heavy lifting would have to be done by my own backend/some sort of a middleware, as neon doesnt seem to be offering such services built in, yeah?
Would love to hear your takes or tips if you've dealt with something similar.
6 Replies
environmental-rose•18h ago
Hey!
Because you want cross-region, you won't be able to use read-replicas, and instead you'll need to use logical replication to another Neon project in another region. But do be aware that you only get eventually consistency with this setup. https://neon.com/docs/guides/logical-replication-guide
Neon
Get started with logical replication - Neon Docs
Neon's logical replication feature, available to all Neon users, allows you to replicate data to and from your Neon Postgres database Stream data from your Neon database to external destinations, enab...
stormy-goldOP•12h ago
Okay, so thats a way to sync data between multiple (in this case) neon databases, right? Forexample, one in region 1 and one in region 2.
When you say "you only get eventually consistency with this setup", what does that entail? Do you mean that there might be delays?
Are there any known/possible issues? Like what if two users, one in each db region, both modify the same column, what happens then during the sync? etc
Thank you very much, will be reading as much as I can find concerning this
environmental-rose•12h ago
it's single master, so all your writes would go to the same database, but reads could go to whichever is closer
stormy-goldOP•12h ago
Ah, sounds neat
environmental-rose•12h ago
And eventual consistency basically means if no new updates are made, eventually all reads will return the most recent value, or in simpler terms, what was written in the master will eventually make its way to the other database
Before you commit to this though, I would make very very sure you actually need this added complexity, and that you are aware of the ramifications of the eventually consistency model
stormy-goldOP•12h ago
Yeah I’m still not entirely sure about it. I will have to read a lot more thoroughly about it. Thank you very much for the actual info/answer though