DEV Community

Cover image for Optimistic Locking & Pessimistic Locking
Hirushi Nethmini
Hirushi Nethmini

Posted on

Optimistic Locking & Pessimistic Locking

In Fintech domain, especially when handling transactions Optimistic Locking & Pessimistic Locking mechanisms play a pivotal role in ensuring data integrity and preventing concurrency issues.
So, I’m here to share my knowledge and insights on these two types of locking.

Let’ assume a scenario, I have a bank account with a balance of Rs. 5000. I can make payments through mobile banking application and debit card. I given my debit card to my mother to buy goods at home. Then, she went to the supermarket and came to pay Rs. 4500 for goods. At the same time, I pay my mobile phone bill which is Rs. 3000.
Then the two transactions happened at the same time.
Transaction 1 – Rs. 4500 – through debit card
Transaction 2 – Rs. 3000 – through mobile banking app
Account Balance – Rs. 5000.

If both transactions are completed, it leads to a drop of account balance below zero or data inconsistency.
The solution to this problem is to use locking. There are two types of locking mechanisms.

  1. Pessimistic Locking
  2. Optimistic Locking

Pessimistic Locking

It is a locking strategy when a transaction reads a record, it should be locked. Then other transactions cannot read or modify that record until the lock is released. This approach is used in high concurrency environments.

Types of Pessimistic Locks:

  1. PESSIMISTIC_READ – Allows reading but it blocks writing.
  2. PESSIMISTIC_WRITE – Blocks both reading and writing.
  3. PESSIMISTIC_FORCE_INCREMENT – like PESSIMISTIC_WRITE but also increment the version number even if no changes made.

PESSIMISTIC_READ
PESSIMISTIC_READ is the locking strategy shared access which means all transactions can read the record but cannot modify it while the lock is held. It blocks the write operation.
@Lock(value = LockModeType.PESSIMISTIC_READ)
Optional<AccountBalance> findById(Integer id);

PESSIMISTIC_WRITE
Transactions cannot do read write operation until the lock release. No transaction can perform read write operations until the current transaction finishes.
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
Optional<AccountBalance> findById(Integer id);

PESSIMISTIC_FORCE_INCREMENT
It is like PESSIMISTIC_WRITE, but the only one difference is it forces the version field of the entity to increment.
@Lock(value = LockModeType.PESSIMISTIC_FORCE_INCREMENT)
Optional<AccountBalance> findById(Integer id);

Optimistic Locking

It allows multiple transactions to read and write data simultaneously, without holding locks.

Types of Optimistic Locks:

  1. OPTIMISTIC
  2. OPTIMISTIC_FORCE_INCREMENT

OPTIMISTIC
It increases the version number when a record changes.
@Lock(value = LockModeType.OPTIMISTIC)
Optional<AccountBalance> findById(Integer id);

OPTIMISTIC_FORCE_INCREMENT
It increases the version number when transactions perform read or write operations.
@Lock(value = LockModeType.OPTIMISTIC_FORCE_INCREMENT)
Optional<AccountBalance> findById(Integer id);

Top comments (0)