I have a multi-regional deploy of postgres db. The...
# orm-help
k
I have a multi-regional deploy of postgres db. There's a primary region where writes are allowed. Every other region does not allow writes (only reads) for consistency. When a write is made to the primary region, that write is propogated automatically to the other regions in the cluster. My question is, is it possible to provide two different database URLs? One for reads and the other for writes?
j
Unfortunately not yet: https://github.com/prisma/prisma/issues/172 There is an idea for a middleware as a workaround in there, but no one has built that yet.
There are probably solutions where the load balancer takes care of selecting the correct server and you just have 1 connection string - if you use one of those, that would of course work. (I can not name them right now though - I know of custom deployments only)
k
Gotcha. So my only alternative is to have two clients and use them as needed I guess
Actually it occurred to me that you could run into race conditions where the write finishes, but then you read from the closer region and it hasn't gotten the data propagation yet so it's inconsistent
That would be really nasty
c
k
Can I deploy that db to multiple regions and have consistency across them?
My goal is to have regional deployments of my app and all related services so it's fast everywhere in the world
So if it can't do that then it's DOA for me.
c
Check out the video...it says you can replicate across multiple AWS regions. That said, I think the typical app has a lot more impactful low hanging fruit when it comes to performance optimization than trying to deploy it in multiple regions.
k
I dunno... eliminating a 500ms overhead for someone in Sydney for every request feels like it's worth pursuing... Not sure what other optimizations could make that size of an impact...
c
I agree 100% if you're talking about something like using Next.js to deploy a static build to a global CDN, thereby speeding up potentially hundreds of browser requests over a relatively slow home/office Internet connection. However, by distributing your database you're only speeding up the connection between your API and your database (which happens over big pipes between data centers). People obsess about stuff like this meanwhile they're serving up 2MB image files 😂
k
Well, I've already got a handle on the image file thing by using cloudinary + transforms. Not much more I can do to optimize that, so I'm moving to another area to optimize things 🙃
I guess you could say that I've already picked all the low hanging fruit and I'm experimenting with ladders to get the rest 😅
Aurora looks interesting though, thanks for sharing!
Wish it weren't an amazon thing... AWS scares me.
c
Lol me too I've always used Heroku, Netlify, etc. however for a job I'm working on I wanted to explore AWS as an option for putting everything in one place. I studied and got the entry-level AWS certification just to get more familiar with what's available. Elastic Beanstalk can supposedly make things easier to set up.
Regarding distributed databases, https://fauna.com/ looks super interesting but I haven't tried it.
k
Yup, I looked into fauna, but didn't feel great about how intrusive their API would be on my entire codebase. Feel much better about prisma where I can move to different postgres hosts as I see fit. Also... I mean freak... Prisma!! It's amazing 😍 prisma rainbow
c
Yes that's exactly what scared me off about AWS Amplify...talk about vendor lock-in!
👍 1
j
Actually it occurred to me that you could run into race conditions where the write finishes, but then you read from the closer region and it hasn't gotten the data propagation yet so it's inconsistent 
Welcome to a distributed system - that is usually the problems that start when you have multiple machines holding data. Different systems give different guarantees here, and you have to figure out which ones are most important to you. Prisma will probably stay pretty neutral there, and let your replication setup (that makes sure the data that is written is also available on the read replicas) deal with this.
👍 1
b
Similar to Fauna, but interface-compatible with Postgres is CockroachDB. Never used it myself but it sounds like it could be a potential solution for you, they’ve even got a blog post specifically about Reducing Latency with Follower Reads Spoke too soon, Prisma Client works but Prisma Migrate doesn’t. I can imagine some hacks to get around that but it’s sounding not ideal
j
Fair warning: "interface-compatible" is a very stretchy term, which leads to CDB not working with Prisma Introspection and Migrate, only Prisma Client is kinda happy with it if you hand write your schema.
b
Ah you beat me to editing my comment 🙂
👍 1
prisma rainbow 1
From what I know then - which is admittedly not much - sounds like potential solutions are: 1. Deal with eventual consistency (no additional effort) 2. Go multi-master and shard customer data based on their geo-location (application-level change) 3. Deploy a distributed system layer on top of postgres. Citus is a common option. (database-level change) 4. Deploy CockroachDB and figure out how to migrate the schema without Prisma’s help (ops-level change)