prisma / extension-read-replicas

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

Support for PlanetScale "database replicas" (local replica that has to be enabled with `USE @replica`) #5

Open janpio opened 1 year ago

janpio commented 1 year 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 1), use case 2) is covered in https://github.com/prisma/extension-read-replicas/issues/15


The "Database replicas" from 1) are not selected via connections strings, but instead you have to run a command on a connection to make it use read replicas: USE @replica https://planetscale.com/docs/concepts/replicas#how-to-use-replicas-in-your-planetscale-database

We would need a way to "identify" these replicas (maybe a different extension "function"), and then a mechanism to make sure that the SQL command is run on each connection that is used in the "replica clients". Currently Prisma Client does not have the functionality to enforce that as far as I know.

tbarn commented 1 year ago

Hey @janpio! I was wanting to test it out with option 2 and was trying to think what might be the recommended way of doing it. The developer would need to tell pass what region read replica to go to when they are querying.

janpio commented 1 year ago

To choose the correct read-only region (which are different from your default region), yes - we would probably want to have some "selection criteria". The extension does not support that yet.

We should probably have an independent issue for that. 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).

tbarn commented 1 year ago

We usually would expect the user to define the code that selects the correct read-only region (for now).

But I am not even sure how to direct it to a specific read-only region if there were multiple ones. I can't use the multiple replicas example from the docs because it is an array where it picks one randomly. Maybe I missed something in the code.

janpio commented 1 year ago

Yes, that is not implemented 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.

Then we can make this one about figuring out a way to tell it that there is a same region replica that needs USE @replica executed once to enable it. And the other issue is about the global read-only replicas, where logic is helpful.

janpio commented 1 year ago

I split the "read-only regions" off into its own issue: https://github.com/prisma/extension-read-replicas/issues/15

janpio commented 7 months ago

Progress: https://planetscale.com/blog/introducing-global-replica-credentials + https://planetscale.com/docs/concepts/replicas