Databases 8 min read

Pessimistic vs Optimistic Locking: A Story-Based Explanation

This article uses a humorous story of two threads battling over inventory updates to illustrate the principles, advantages, and drawbacks of pessimistic and optimistic locking in distributed systems, helping readers understand when to apply each concurrency control method.

Architects' Tech Alliance
Architects' Tech Alliance
Architects' Tech Alliance
Pessimistic vs Optimistic Locking: A Story-Based Explanation

In large distributed systems, concurrent writes are inevitable, and ensuring data consistency during simultaneous accesses is a core challenge.

The article tells a story about two threads, Wangcai and Xiao Qiang, who both need to reduce the stock of the same product, leading to lost updates when they write without coordination.

Wangcai proposes a solution: before accessing the inventory, acquire a lock that blocks other threads until the operation finishes, illustrating the concept of a pessimistic lock.

The narrative shows how the lock forces Xiao Qiang to wait, causing delays and potential bottlenecks, and notes that this approach assumes conflicts are likely, which can reduce overall concurrency.

Later, Wangcai introduces an optimistic lock: add a version field to the inventory record, read both stock and version, and when updating, check that the version has not changed; if it has, abort and retry.

The story demonstrates optimistic locking through a series of steps where Wangcai successfully updates the stock while Xiao Qiang encounters version mismatches, forcing him to reread and retry until he finally succeeds.

After the story, the article summarizes both mechanisms: pessimistic locking prevents conflicts by locking resources early, guaranteeing consistency but hurting performance and risking deadlocks; optimistic locking assumes conflicts are rare, allowing concurrent reads and writes and checking for conflicts only at commit time, which works well under low contention but may cause repeated retries under high contention.

Both methods are explained with concrete examples, and the article concludes that the choice between them depends on the expected conflict rate and performance requirements of the system.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsdatabaseConcurrency Controloptimistic lockpessimistic-lock
Architects' Tech Alliance
Written by

Architects' Tech Alliance

Sharing project experiences, insights into cutting-edge architectures, focusing on cloud computing, microservices, big data, hyper-convergence, storage, data protection, artificial intelligence, industry practices and solutions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.