This is a more logical problem rather than SQL, but I hope you will be able to help me.
I have a list of wallets in my database, something like this:
wallet_id SERIAL PRIMARY KEY | balance BIGINT
I am getting requests from clients, to register their wallets in the database, there is a registration fee for that, there is a third party that decides what the fee will be based on the wallet_id
, I don't know what logic they use and can't predict the fee. I do however have to make sure that the client has sufficient balance to register a wallet. I also need to make sure that all their wallets ids are serial. I don't have a goal to register as many clients as possible, I simply need to respond to each client request for registering if the registration succeeded or not and what was the fee the client was charged with.
And to add on top of that I got multiple processes that communicate with the database and trying to register clients. So my question is how can I synchronize those processes in order to succeed in my task?
I thought about a solution where I lock the entire table, register the wallet in a transaction, determinate the registration fees and if there is an insufficient balance I roll back. But I don't like the idea of locking the entire table, I would prefer to somehow use a temporary table or something else... Any ideas?