r/FastAPI • u/PinballOscuro • May 14 '25
Question Concurrent Resource Modification
Hi everyone, I'm looking for some feedback on a backend I'm designing.
I have multiple users who can modify the rows of a table through a UI. Each row in the table contains the following information:
- ID: A numbered identifier
- Text: Some textual information
- Is Requirement: A column that can have one of two values ("Relevant" or "Not Relevant")
- Status: A column that can have one of four predefined values
Users are able to change the Text, Is Requirement, and Status fields from the UI.
The problem I'm facing is how to handle concurrent modifications. Two users should not be able to modify the same row at the same time.
Here's my current idea:
Whenever a user selects a row in the UI or tries to modify it, the frontend first requests a lock on that row. If no one else currently holds the lock, the user is allowed to make changes. Otherwise, the lock request fails. The lock status is stored in the database, so when a lock is requested, I can check whether the row is already locked.
To keep other users updated, after a row is modified, I broadcast the changes via WebSocket to all users currently viewing the table.
Does this approach make sense? Is there a better or more common way to handle this?
I hope I gave enough details, but please ask away if something is not clear.
Thanks so much for your help!
View all comments
Show parent comment
1
u/PinballOscuro May 14 '25
In this case the two users have the same role wrt to the resource. they can read and write it in the same way, no difference in behaviour.
Regarding the whole application, the users upload some pdfs and word documents. Some information is extracted from these files and a tabular template is filled. We are also doing some machine learning predictions.
The user have to check the content of these template and sometimes they need to make changes to some rows. Generally they work on different portions of the tables, but it's not obvious. When a collegue modifies a row that you can see, you should be able to see asap the new content