prisma / extension-read-replicas

Prisma Client Extension to add read replica support to your Prisma Client
Apache License 2.0
96 stars 6 forks source link

Support for PlanetScale "Read-only regions" (global replica that can not be in the same region as original database) #15

Open janpio opened 9 months ago

janpio commented 9 months ago

PlanetScale does read replicas a bit differently. They have two separate concepts:

  1. Database replicas: https://planetscale.com/docs/concepts/replicas The number of replicas depends on your account plan.
  2. Read-only replicas: https://planetscale.com/docs/concepts/read-only-regions You can add additional globally distributed replicas to your database.

This issue is about 2), use case 1) is covered in https://github.com/prisma/extension-read-replicas/issues/5.


Technically this extension already supports the connection string that you get for read-only replicas, but there is no way to make the extension to select the correct one that makes sense because of the location where your application runs. There is also no way to send read queries to the primary when the app runs in the same location as the primary automatically.

To choose the correct read-only region we would probably want to have some "selection criteria". The extension does not support that yet. Right now it is dumb, and there is no way for it to take any logic (e.g. by location) into account. That is why I think we need a new issue.

I could imagine that queries somehow get information about where the application is running, and then some logic/function decides the correct read replica based on that data (instead of randomly selecting a replica from the list).

agcty commented 7 months ago

Yeah queries definitely need to get that information, the question is just where and when does it get the information from.

Could either be: 1) At prisma client instantiation 2) For every query

I think 1) makes more sense than 2) but there might be an option missing that I can't think of yet. In any case I think you'd definitely want to be able to write custom logic to route to the right read replica.