[Solved] Event ordering/ race condition

Omar Gamil Asks: Event ordering/ race condition
Here’s a scenario I need help with.

Say you have a dating app (distributed, multiple instances), that has a table likes

| id | user_id | liked_id |notification_sent|

Code:
If User A likes User B
    Insert into likes
    If likes contains (B likes A)
        Send match notification(A,B)
        Set notification_sent true for both rows

Now, what happens if both users A and B like each other at the same time? (race condition)? there’s a chance that the second if will be missed, as the rows are not inserted yet.

Solution A: Have a background service that runs periodically, checking for A likes B AND B likes A AND notification_sent=false

that’s not really realtime

Solution B: Always retry the 2nd check, but that just seems wasteful.

Solution C: Lock the whole table! Nope!.

Is there any othert solution to force order of events/avoid the race condition?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.