Mastering Locks in High-Concurrency Systems: From Pessimistic to Distributed

This article explains the different lock types—pessimistic, optimistic, and distributed—illustrates their use cases with code examples, compares four practical solutions for high‑concurrency deduction scenarios, and offers guidance on selecting the right locking strategy.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
Mastering Locks in High-Concurrency Systems: From Pessimistic to Distributed

Lock Types

Different scenarios require different locks. The main categories are pessimistic locks (synchronize), optimistic locks (volatile, CAS, DB version), and distributed locks (Redis).

Optimistic Lock

Optimistic lock assumes no conflict when reading data and checks for changes before updating. Example pseudocode:

reduce() {</code>
<code>    select total_amount from table_1;</code>
<code>    if (total_amount < amount) { return failed; }</code>
<code>    // other business logic</code>
<code>    update total_amount = total_amount - amount where total_amount > amount;</code>
<code>}

Database version numbers and CAS are typical optimistic lock implementations.

Pessimistic Lock

Pessimistic lock assumes the worst case and locks the data during read. Example:

reduce() {</code>
<code>    // other business logic</code>
<code>    int num = update total_amount = total_amount - amount where total_amount > amount;</code>
<code>    if (num == 1) { /* business logic */ }</code>
<code>}

Java's synchronized and database row locks belong to this category.

Deduction Operation Example

In high‑concurrency scenarios such as inventory or balance deduction, naive updates can cause overselling (negative stock). Four solutions are discussed:

Solution 1: Synchronous Exclusive Lock

Using synchronized provides simplicity but suffers from performance loss and cannot work across processes.

Solution 2: Database Row Lock

Locks the row in the database, solving cross‑process issues but still incurs blocking, transaction isolation, connection exhaustion, and possible deadlocks.

Solution 3: Redis Distributed Lock

Redis (or Zookeeper) can act as a distributed lock. Important points include atomic set‑nx with expiration, handling lock timeout, and “renewal lock” to extend expiration safely.

Solution 4: Database Optimistic Lock

Use a version check or conditional update (WHERE total_amount > amount). If the update affects zero rows, another transaction has already deducted the amount, preventing negative values. Combine with transaction rollback for full consistency.

Summary

Experienced engineers often choose database version numbers or distributed locks, while beginners may start with Java synchronized or row locks. Understanding the trade‑offs of each lock type helps select the right locking strategy for high‑concurrency resource protection.

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.

JavaConcurrencydistributed-lockoptimistic-lockpessimistic-lock
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

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.