I’m trying to create a system of shared keys acros...
# orm-help
m
I’m trying to create a system of shared keys across my app. The idea is that there’s a central store of keys in the database and: 1. A server can request an available key, which is returned and marked as in use. 2. A server can relinquish its key (or the hold on the key will eventually expire), which will mark it as available. 3. If no keys are available the server can generate a new set of keys to repopulate the table (these keys require some specific formatting logic so I don’t think the can be generated inside the database) I see Prisma supports transactions, but they seem to be per request (i.e. they are atomic within a single request but if two separate servers simultaneously request a key, return a key, etc. you can get a data race). So how would one implement this using Prisma? P.S. - I’ve never worked with databases before (my background is in game, mobile and front-end development) so feel free to point out things that should be glaringly obvious 😉 P.P.S. - The reason I think transactions are not safe across multiple requests is this issue: https://github.com/prisma/prisma/issues/3242
n
You should be able to use interactive transactions, even in the case of two separate server requests at the same time, you could use optimistic locking to make sure you don’t run into dirty reads/writes. This article is a good read to know more about it: https://ketanbhatt.com/db-concurrency-defects/