I have a mobile application and server based on Symfony which gives API for the mobile app.
I have a situation, where users can like Post
. When users like Post
I add an entry in ManyToMany table that this particular user liked this particular Post
(step 1). Then in Post
table I increase likesCounter (step 2). Then in User
table I increase gamification points for user (because he liked the Post
) (step 3).
So there is a situation where many users likes particular Post
at the same time and deadlock occurs (on Post
table or on User
table).
How to handle this? In Doctrine Docs I can see solution like this:
<?php
try {
// process stuff
} catch (\Doctrine\DBAL\Exception\RetryableException $e) {
// retry the processing
}
but what should I do in catch
part? Retry the whole process of liking (steps 1 to 3) for instance 3 times and if failed return BadRequest to the mobile application? Or something else?
I don't know if this is a good example cause maybe I could try to rebuild the process so the deadlock won't happen but I would like to know what should I do if they actually happen?