Database Fundamentals

0% completed

Previous
Next
Optimistic vs. Pessimistic Concurrency Control

Optimistic Concurrency Control (OCC)

OCC assumes that conflicts are rare and allows transactions to execute without restrictions until they are ready to commit. At the commit stage, the system checks for conflicts.

How OCC Works

Image
  1. Transaction Execution: A transaction reads data and makes changes locally without acquiring locks.
  2. Validation Phase: Before committing, the database checks if any conflicts occurred (e.g., if another transaction modified the same data).
  3. Commit or Rollback:
    • If no conflicts are detected, the transaction commits.
    • If conflicts are detected, the transaction rolls back and retries.

Advantages of OCC

  • No Lock Overhead: Transactions do not acquire locks, reducing system overhead.
  • Better Performance: Ideal for systems with low contention, as most transactions succeed without conflicts.
  • Higher Scalability: Works well in distributed systems where locking can be expensive.

Disadvantages of OCC

  • Rollback Costs: Transactions may have to restart from scratch if conflicts are detected during validation.
  • Inefficient in High Contention: Frequent conflicts lead to excessive rollbacks, reducing performance.

Use Cases

  • Systems with low contention where conflicts are infrequent.
  • Read-heavy workloads, such as reporting or analytics.
  • Distributed systems, where locking is expensive.

Real-World Example

  1. User A and User B both read the same product inventory.
  2. User A updates the inventory count and submits the transaction.
  3. When User B tries to update the inventory, the system validates and detects the conflict, prompting User B to retry.

Pessimistic Concurrency Control (PCC)

PCC assumes that conflicts are likely and prevents them by acquiring locks on data as soon as a transaction accesses it.

How PCC Works

Image
  1. Lock Acquisition: When a transaction reads or writes data, it acquires a lock to ensure no other transaction can access the same data concurrently.
  2. Conflict Prevention: Locks prevent other transactions from accessing the same data until the current transaction completes.
  3. Commit or Release: Once the transaction commits or rolls back, the locks are released.

Advantages of PCC

  • Prevents Conflicts: Transactions are serialized, ensuring no interference.
  • No Rollbacks: Conflicts are avoided upfront, eliminating the need for restarts.
  • Consistency Guarantees: Ideal for critical systems where correctness is paramount.

Disadvantages of PCC

  • Lock Overhead: Locking introduces significant overhead, especially in systems with many transactions.
  • Deadlocks: Multiple transactions can block each other, requiring deadlock detection and resolution.
  • Reduced Concurrency: Locking limits parallelism, impacting performance in high-concurrency systems.

Use Cases:

  • Systems with high contention, where conflicts are frequent.
  • Write-heavy workloads, such as banking or financial systems.
  • Critical applications that require strict consistency, like inventory management.

Real-World Example

  1. User A starts editing a product's inventory, acquiring a lock on the product row.
  2. User B tries to access the same product but is blocked until User A completes the transaction and releases the lock.

Key Differences Between OCC and PCC

AspectOptimistic Concurrency Control (OCC)Pessimistic Concurrency Control (PCC)
AssumptionConflicts are rareConflicts are frequent
LocksNo locks usedLocks are used
PerformanceHigh in low-contention scenariosHigh in high-contention scenarios
RollbackRollbacks occur if conflicts are detectedNo rollbacks due to conflicts
OverheadLow locking overheadHigh locking overhead
Use CaseRead-heavy or low-contention workloadsWrite-heavy or high-contention workloads

Choosing Between OCC and PCC

CriteriaUse OCCUse PCC
System ContentionLow contentionHigh contention
Transaction TypeRead-heavyWrite-heavy
Failure ToleranceHigh (rollbacks acceptable)Low (strict consistency required)
System TypeDistributed systems or systems with low conflictsCentralized systems with frequent conflicts

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next