1. The Problem: What Happens When Multiple Requests Access the Same Data?

Imagine we have a banking application.

A user has ₹500 in Account A and wants to transfer money from Account A to Account B.

Now suppose two requests arrive at almost exactly the same time:

At first, this may look simple. For every transfer, our backend should:

  1. Read the balance of Account A.
  2. Check whether enough money is available.
  3. Deduct the requested amount.
  4. Add it to Account B.

The problem appears when these operations happen concurrently.

Without proper locking/concurrency control

Suppose Account A initially contains:

₹500

Both requests read the balance before either request has updated it.

Initial Balance = ₹500

Request 1 reads → ₹500
Request 2 reads → ₹500

Both requests conclude:

"There is enough money available."

Now: