Recently while developing an app for a client I came across an interesting problem. So before I explain the meaning of these mumbo-jumbo words let me explain the problem :

image.png

[ A screenshot from the session where I was designing my approach to create a notification based verification process ].

In this system, there are two key user roles: Admin and Executive. When an executive updates a record to log a payment for a deal, the update isn’t finalized until an admin verifies it. Since multiple admins exist, a notification is sent to all of them whenever a payment update is made. The deal’s status remains unconfirmed until an admin approves or rejects it.

To handle this, the database includes a field—let’s call it isAccepted—which determines the verification status. This can either be a boolean (true/false) or an enum with values such as Accepted”**, **“Declined”**, or **“Pending to offer better clarity and flexibility.

This is the point I questioned :

<aside> ⚡

What if more than one admin verifies the payment record at the SAME TIME ? What if an admin accepts it and the other admin rejects it ? What should be the state of “isAccepted” field in the Database ?

</aside>

Wouldn’t there be a race condition for this isAccepted field ? How do we solve this ?

Another case would be to imagine an online text editor where say 5 people are trying to edit the same line of text.


Pessimistic Concurrency Control / Lock

image.png

Imagine 3 admins competing at the same time to update the isAccepted field in the DB.

[ The red one declines the verification notification, while the other 2 green coded ones accept the payment update notification ].

The main thing to focus here is the “LOCK” (you might know it from Go or Rust as Mutex). This is the key to reaching the database.

image.png

Assume the first admin gets access to the lock. This does two things:

  1. Blocks the other admins from UPDATING the SAME field as the LOCKED admin. ( So nobody else can now update the isAccepted field ).
  2. Updates the isAccepted field in the DB to “Declined” / false.