Does Only One Query “Hit” a Row at a Time?

Aditya Yadav
3 min readOct 9, 2024

--

1. Read Operations (SELECT Queries)

  • Concurrent Reads Allowed: Multiple transactions can read the same row simultaneously by acquiring shared locks (S).
  • Lock Compatibility: Shared locks are compatible with other shared locks, allowing high concurrency for read-heavy operations.

Implications:

  • No Blocking: Read operations do not block each other, promoting efficient data retrieval.
  • Data Stability: Ensures that data read by one transaction is not being modified by another.

2. Write Operations (UPDATE, DELETE Queries)

  • Exclusive Access Required: Only one transaction can modify a row at a time by acquiring an exclusive lock (X).
  • Lock Exclusivity: Exclusive locks are not compatible with other exclusive or shared locks, ensuring that no other transaction can read or write the row during the write operation.

Implications:

  • Preventing Conflicts: Ensures that write operations do not interfere with each other, maintaining data consistency.
  • Potential Blocking: Other transactions attempting to read or write the locked row must wait until the exclusive lock is released.

3. Mixed Operations

  • Read and Write Conflict: If one transaction holds a shared lock (reading) and another tries to acquire an exclusive lock (writing), the writing transaction will wait until the shared lock is released.
  • Write and Read Conflict: Similarly, if a transaction holds an exclusive lock (writing) and another tries to read, the reading transaction must wait.

4. Advanced Concurrency Control: MVCC

Some databases, like PostgreSQL, implement Multi-Version Concurrency Control (MVCC), which allows:

  • Non-Blocking Reads During Writes: Readers do not block writers and vice versa by maintaining multiple versions of data.
  • Snapshot Isolation: Each transaction sees a consistent snapshot of the database as of the start of the transaction.

Implications:

  • Increased Concurrency: Higher throughput as read and write operations do not block each other as much.
  • Complexity in Isolation Levels: Requires understanding of how MVCC interacts with different isolation levels to avoid anomalies

Practical Considerations

1. Choosing Appropriate Isolation Levels

Selecting the right isolation level impacts how locks are acquired and managed, affecting both data consistency and application performance.

  • Read Committed: Prevents dirty reads. Suitable for most applications.
  • Repeatable Read: Prevents dirty and non-repeatable reads. Useful when consistent read results within a transaction are essential.
  • Serializable: Ensures complete isolation, preventing all read anomalies. Best for highly sensitive transactions but may reduce concurrency.

2. Minimizing Lock Contention

  • Keep Transactions Short: Longer transactions hold locks for extended periods, increasing the likelihood of contention and deadlocks.
  • Order of Lock Acquisition: Acquire locks in a consistent order across transactions to reduce deadlock chances.
  • Granular Locking: Use row-level locks instead of table-level locks to increase concurrency.

3. Handling Deadlocks

  • Deadlocks Occur When: Two or more transactions are waiting indefinitely for each other to release locks.
  • Solutions:
  • Timeouts: Automatically abort transactions that wait too long.
  • Retry Logic: Implement retry mechanisms to attempt transactions again after a deadlock.
  • Consistent Lock Ordering: Prevent deadlocks by ensuring transactions acquire locks in the same sequence.

4. Monitoring and Logging

  • Monitor Lock Waits and Deadlocks: Use database monitoring tools to track lock-related issues.
  • Log Transaction Details: Keep logs of transactions and their operations to diagnose concurrency problems.

7. Summary

  • Multiple Queries Can Access the Same Row: Depending on the operation type and lock mode, multiple queries can read or write to the same row.
  • Concurrent Reads: Multiple SELECT queries can read the same row simultaneously using shared locks.
  • Exclusive Writes: Only one UPDATE or DELETE query can modify a row at a time using exclusive locks.

Lock Types and Isolation Levels Govern Access:

  • Shared Locks (S): Allow concurrent reads but prevent writes.
  • Exclusive Locks (X): Allow a single write operation and block other reads/writes.
  • Isolation Levels: Define the degree of visibility and interaction between concurrent transactions.

Sequelize’s Transaction Management:

  • Pass the Transaction Object: Essential for scoping queries within a transaction and managing locks appropriately.
  • Use Lock Options: Apply specific locks (UPDATE, SHARE, etc.) based on the operation's requirements.

Concurrency Control Strategies:

  • MVCC: Some databases support MVCC to enhance concurrency without heavy locking.
  • Best Practices: Keep transactions short, choose appropriate isolation levels, handle deadlocks, and monitor lock usage.

--

--

Aditya Yadav
Aditya Yadav

Written by Aditya Yadav

Software Engineer who talks about tech concepts in web development

No responses yet