I have a table (codes) in relational database MySQL which stores a list of unique codes. The schema sample is given below.
Integer id
String codes
String user
Boolean is_available
id | codes | user | is_available
1 | ABC | | true
2 | XYZ | | true
Whenever a code is assigned to a user user_id is updated and is_available is made false.
1 | ABC | user_id | false
There are multiple instances of a service running which fetch the code from database and gives it to the user. Each request must give a unique code.
GET /code -> returns a unique code
As there are multiple instances what is the best way to handle the concurrency.
- Is update and then select the correct way to do it ? Does it make the full table scan ?
- Read optimistic locking , where we can set retries on a failed attempt but dont feel this is a good approach.
- Setting isolation level to serializable is suggested but i dont think it should be used in production environment.
- Having an centralised blocking queue which pops a unique code each time the request is made but it will be a single point of failure.
I have read a lot of theroy on this but I am looking for how it should be implemented on an enterprise scale application having thousands on concurrent calls.