Dealing with concurrent updates

Does anyone have suggestions on resources, concepts and topics to look into on handling concurrent updates? Like I have two different systems possibly updating the same record/object, how should I be handling concurrent updates to prevent duplicates and other issues. The two different systems have their own db but eventually the shared data has to be written in a middleware postgres db that has to handle the concurrent scenarios.
53 Replies
JavaBot
JavaBotā€¢2mo ago
āŒ› This post has been reserved for your question.
Hey @Kale Vivi! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xddā€¢2mo ago
try database locking optimistic and pessimistic depending on the use case
dan1st
dan1stā€¢2mo ago
it depends on what you are dealing with if it's about concurrency within your application, you can use language mechanisms: https://discord.com/channels/648956210850299986/1027179147086282752 If you it's about synchronizing access to a DB from multiple applications, you would want to use the mechanisms provided by the DB (e.g. locking or transactions)
Kale Vivi
Kale ViviOPā€¢2mo ago
How does the db or language know the order of the inserts/updates/changes? Is it based on ids or timestamps or..? I'll start reading up on the concepts above. thanks!
JavaBot
JavaBotā€¢2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1stā€¢2mo ago
if it gets a request for doing an operation and it can execute the operation, it does that
JavaBot
JavaBotā€¢2mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Kale Vivi
Kale ViviOPā€¢2mo ago
Hey, I guess I have questions on handling something like this: I have an user facing application A that is a SaaS (full stack web app) that can make an update to the same record in another user facing application B (full stack). Both have their own data stores but I have to sync updates to both data stores using a middleware layer that also has it's own db. Using locks and transactions should handle majority of the cases? I mean also there is latency between systems, so thinking to keep track using a timestamp for each system and compare timestamps?
dan1st
dan1stā€¢2mo ago
maybe consider adding an API in application B for application A to update stuff? though you could also let both access the common DB
Kale Vivi
Kale ViviOPā€¢2mo ago
What do you mean by both access the common DB? I mean both systems will update the common db via apis/streaming etc Eventually, we would like to retire Application B, hence why better not to add api and other items in B Just need to solve this issue temporarily until App A is fully good and well
JavaBot
JavaBotā€¢2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Kale Vivi
Kale ViviOPā€¢2mo ago
I'm thinking at this point I will need to compare timestamps but not sure how much more overhead this introduces
dan1st
dan1stā€¢2mo ago
oh so A is replacing B
Kale Vivi
Kale ViviOPā€¢2mo ago
eventually but need to keep both live for some time so thats' where the data sync is needed until then
dan1st
dan1stā€¢2mo ago
So both have the same data but in a different format and both do both reading and writing?
Kale Vivi
Kale ViviOPā€¢2mo ago
yeah exactly
dan1st
dan1stā€¢2mo ago
Is it maybe possible that you replace the writing functionality first? so that application B becomes read-only and all write access happens with application A?
Kale Vivi
Kale ViviOPā€¢2mo ago
Unfortunately no both need to be live until the full cutover
dan1st
dan1stā€¢2mo ago
Do you need more or less realtime updates?
Kale Vivi
Kale ViviOPā€¢2mo ago
ideally realtime updates between systems if possible but thinking 5 to 10 min delay is fine too
dan1st
dan1stā€¢2mo ago
I think the best option is the following: - Add an API to application A that allows updating and reading stuff - change application B to use that API for all its operations instead of the DB so you let application A be the DB of application B or you use the same DB for both applications i.e. have more or less the same schema (you might be able to use views or stored procedure for some things)
Kale Vivi
Kale ViviOPā€¢2mo ago
App A is vendor locked, we can't make direct changes to it. They have their own apis we can refer to however to get data in and out of the system
dan1st
dan1stā€¢2mo ago
alternatively (if there are not that many different types of write applications), you can do the following: - Add an API to application A that allows application B to modify A's DB - Add an API to application B that allows application A to modify B's DB
Kale Vivi
Kale ViviOPā€¢2mo ago
App B is retiring, so we don't want to add more functionality to App B. We are building out the middleware however that sits between A and B that can handle keeping everything in sync
dan1st
dan1stā€¢2mo ago
Is there an option on getting notified via an API for every change being made in application B? i.e. can application A subscribe to all changes in application B?
Kale Vivi
Kale ViviOPā€¢2mo ago
We can subscribe to App A not app B. The are webhooks functionality in App A
dan1st
dan1stā€¢2mo ago
So you have absolutely no option of getting informed about changes being made in app B?
Kale Vivi
Kale ViviOPā€¢2mo ago
One thought is setting up a kafka producer in app B to get the updates in real time and we can consume the updates in the middleware adn then eventually post that to app A
dan1st
dan1stā€¢2mo ago
you need something that gets all updates of app B and sends that to app A, yes
Kale Vivi
Kale ViviOPā€¢2mo ago
I mean let's say I have something liek that How do I handle simul. updates? I have to compare timestamps right
dan1st
dan1stā€¢2mo ago
when you make an operation in app A, you use application B's API to perform the same change and you can maybe do something like rolling back the transaction in A if that one fails Do you have access to app B's database? Do you have an option to block changes in app B (e.g. via your middleware)? Can you maybe simulate application B's database using application A's database?
Kale Vivi
Kale ViviOPā€¢2mo ago
What do you mean by simulate app B and mean by block changes? Middleware also has it's own db
dan1st
dan1stā€¢2mo ago
You have a middleware between app B and the database - Are you controlling that middleware/can you change that? Also is it an option for app A to just use the DB of app B instead of its own?
Kale Vivi
Kale ViviOPā€¢2mo ago
App A + DB <> Middlware + DB <> App B + DB It's more like this right now No App A can't use app B db
dan1st
dan1stā€¢2mo ago
Does app B communicate with the DB directly? or is there something in between you could intercept?
JavaBot
JavaBotā€¢2mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
ayylmao123xdd
ayylmao123xddā€¢2mo ago
couldnt you use some sort of pessimistic locking or optimistic in both apps wouldnt that just solve all the problems as long as u can edit both apps
dan1st
dan1stā€¢2mo ago
they can only modify one of the apps - they said that before and both apps have different databases
ayylmao123xdd
ayylmao123xddā€¢2mo ago
o mamma mia hol up theres middleware plus db so just apply the locking on the middleware and then once that operation is done you can let the other db know to do stuff hmmmmmmmmmmmmmmmmmmmmmm what about using something to make a queue kafka sounds like a great answer to this have 1 channel for both apps to do stuff and 1 same channel for both apps to receive instructions
dan1st
dan1stā€¢2mo ago
I'm not sure about what happens with the middleware of app B (the one they cannot modify) that's what I was trying to find out But they cannot change (much) about app B
ayylmao123xdd
ayylmao123xddā€¢2mo ago
then u cant do anything with app b db really
dan1st
dan1stā€¢2mo ago
this is why I asked about it
ayylmao123xdd
ayylmao123xddā€¢2mo ago
unless u connect the db from app b to app a
dan1st
dan1stā€¢2mo ago
but there are some possibilities - but these depend on the answers to these things
ayylmao123xdd
ayylmao123xddā€¢2mo ago
yes your sudoness ur new title šŸ™
dan1st
dan1stā€¢2mo ago
there's also some APIs of app B but there are limitations on it
ayylmao123xdd
ayylmao123xddā€¢2mo ago
yea kinda weird architecture ngl
dan1st
dan1stā€¢2mo ago
well at least it's better than the last one and I'll probably not need to automod it it's migrating from B (foreign vendor) to A
ayylmao123xdd
ayylmao123xddā€¢2mo ago
what was bad about the last one smh it was the merge of two words skibidi and majesty
dan1st
dan1stā€¢2mo ago
the first one
ayylmao123xdd
ayylmao123xddā€¢2mo ago
oh yea that makes sense
Kale Vivi
Kale ViviOPā€¢2mo ago
Hey, I'll diagram some things and get some feedback soon just to clarify any doubts here
JavaBot
JavaBotā€¢5w ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?