Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Concurrency Control Strategies in MySQL: Pessimistic and Optimistic Locking

Notes 1

Concurrency control mechanisms are essential for maintaining data integrity when multiple transactions attempt to modify shared resources simultaneously. These mechanisms serialize access, ensuring that conflicting operations do not lead to inconsistent states. While read operations generally proceed without restriction, write operations require coordination to prevent race conditions.

Implementation Layers

Serialization can be handled at the application level using language-specific primitives, such as synchronized blocks in Java or Mutex in Go. However, in distributed systems, database-level locking is often preferred. This approach centralizes coordination within the storage engine, simplifying architecture across multiple service instances.

Pessimistic Concurrency Control

This strategy assumes conflicts are likely and prevents them by holding locks throughout the transaction duration. In MySQL, this relies on the InnoDB engine and explicit transaction management.

Mechanism: The core involves locking rows during the read phase to prevent other transactions from modifying them until the current transaction completes. This is typically achieved using SELECT ... FOR UPDATE.

Example Scenario: Consider a banking system where a user's balance must be checked before deduction.

START TRANSACTION;

-- Lock the specific row to prevent concurrent modifications
SELECT balance FROM accounts 
WHERE account_id = 1001 
FOR UPDATE;

-- Perform business logic (e.g., deduct funds)
UPDATE accounts 
SET balance = balance - 50 
WHERE account_id = 1001;

COMMIT;

Lock Granularity: InnoDB utilizes row-level locking when the query utilizes a unique index or primary key. If the condition lacks an index, the engine may escalate to a table-level lock, severely impacting performance. Locks are held until the transaction is committed or rolled back.

Optimistic Concurrency Control

Optimistic locking operates on the assumption that conflicts are rare. It does not lock data during reads but verifies integrity before committing writes.

Mechanism: A version number or timestamp column tracks the state of the record. Updates are only successful if the record has not changed since it was last read.

Example Scenario: Managing inventory stock where multiple users might attempt to purchase the same item.

-- Read current state
SELECT stock_count, revision FROM products 
WHERE product_id = 505;

-- Attempt update only if revision matches
UPDATE products 
SET stock_count = stock_count - 1, 
    revision = revision + 1 
WHERE product_id = 505 
AND revision = 10;

Conflict Resolution: If the UPDATE statement affects zero rows, it indicates another transaction modified the data concurrently. The application must then decide whether to retry the operation or notify the user.

Performance and Selection Criteria

Pessimistic Approach:

  • Pros: Guarantees data consistency without retry logic; suitable for high-contention write scenarios.
  • Cons: Reduces throughput due to waiting; potential for deadlocks.

Optimistic Approach:

  • Pros: Maximizes throughput in low-contention environments; avoids lock overhead.
  • Cons: Requires application-level retry logic; performance degrades under high write contention due to frequent failures.

Architectural Considerations

For standard business transactions, MySQL locking mechanisms provide sufficient consistency guarantees. However, in extreme high-concurrency scenarios, such as flash sales, database locks may become a bottleneck. In such cases, offloading coordination to in-memory stores like Redis or using message queues for asynchronous processing can offer better scalability. The choice between locking strategies should align with the specific contention patterns and consistency requirements of the business domain.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.