What happens when two people try to withdraw money from same acoount at the same time?
What happens when two people try to withdraw money from same acoount at the same time?
Imagine you and your family each have debit cards linked to the same bank account. One card is used by your mother, and another by you, studying in a different city. Both of you can access the account simultaneously, checking the balance without any issues because readers don’t block each other. But what happens when both of you try to withdraw money from your ATMs at the same time? Here’s where Multi-Version Concurrency Control (MVCC) comes into play to keep your transactions accurate and consistent.

The Challenge:
Suppose your account has a balance of $1000. You and your mother each decide to withdraw $100. Without MVCC, both of you might see the balance as $1000 and each attempt to withdraw $100. If both transactions were processed simultaneously without proper control, the balance could incorrectly show as $900, which is not accurate.
How MVCC Fixes This:
MVCC ensures that concurrent transactions don’t mess with each other’s results. Here’s a simplified rundown of how it works:
1- Concurrent Reads: Both you and your mother can check the account balance simultaneously without blocking each other. You both see the balance as $1000.
2- Transaction Isolation: When you both initiate a withdrawal, MVCC ensures that only one of these transactions is processed at a time. It creates a temporary version of the data for each transaction, so while both of you see the balance as $1000, each transaction is isolated from the other.
3- Consistency and Validation: When the first transaction (your mother’s) is processed, it deducts $100, leaving $900. If the second transaction (yours) tries to process at the same time, MVCC detects the inconsistency because the balance has already changed due to the first transaction.
4- Handling Conflicts: MVCC ensures that either:
The second transaction is declined and must be retried with the updated balance ($900)
or
The second transaction is rolled back, ensuring that only the first transaction succeeds and updates the balance correctly.
The Result:
With MVCC, you and your mother’s transactions will always reflect the true balance of $800 after both withdrawals, maintaining the account’s consistency and integrity. MVCC not only prevents discrepancies but also ensures that your account remains accurate and reliable, even when multiple people are accessing it at the same time. This mechanism helps banks manage transactions smoothly and avoids the chaos that could arise from simultaneous withdrawals.
← PostgreSQL Blog