Concurrency Control: Pessimistic Vs Optimistic Locking

Ayush Gupta
3 min readAug 18, 2024
Photo by Jose Fontano on Unsplash

Not a Medium Member, read this article here.

In database management, concurrency control is all about ensuring that multiple transactions can happen at the same time without causing issues.

When different processes try to access or modify the same data simultaneously, you need a way to manage these interactions to keep the data consistent and accurate.

This is where pessimistic locking and optimistic locking come into play.

Let’s dive into what each approach means and when to use them.

Pessimistic Locking

Pessimistic locking is like playing it safe. The system assumes that conflicts between transactions are likely, so it locks the data as soon as a transaction starts.

This means no other transaction can access or modify that data until the first transaction is done.

For example: in a bank’s database, when a transaction starts to transfer money from one account to another, it locks both accounts. Other transactions trying to access these accounts must wait until the transfer is completed, ensuring that the balance calculations remain accurate.

Key Points:

  • Locks Data Early: As soon as a transaction begins, the…

--

--

Ayush Gupta
Ayush Gupta

Written by Ayush Gupta

Generalist || Sharing what I know || Software Engineering || AI || Game Theory || Life

Responses (2)