In my application, I have InsertData HTTP method, where i am inserting multiple data in DB (postrgres).While inserting data, I am taking the MAX count from one of the column "ColIndex" and increment the "ColIndex" by 1. Increment operation will be happen based on the data count we received.
Note: Column "ColIndex" is not a primary key.
My problem is, When two users make a call at the same time to insert a different data. Data has been inserted successfully. But problem is User1 and User2 are getting same MAX count from column "ColIndex". How to wait till User1 response and proceed with User2 request?
Environment: .NET 6 & Postrgres
How to handle this Concurrent API call scenario without affecting performance?
2
Answers
You have to have some form of concurrency, which could be either pessimistic (taking a lock of some kind) or optimistic (lockless, but needs some way to resolve conflicts).
One way to do this, and to not take a lock would be to put a unique constraint on the
COLINDEX
column. Something likeIn your API code, implement a retry loop. Something like
How long you delay between tries is a whole other topic, so I suggest for now delaying for a random number of milliseconds.
@tymtam’s way is even easier, though both mine and their answer requires you to have the permissions to modify the DB schema.
You could make
ColIndex
an auto-increment column:Then:
Result: