← Lessons

quiz vs the machine

Gold1480

Databases

Optimistic Concurrency Control

Detecting conflicts at commit instead of holding locks.

5 min read · core · beat Gold to climb

Two Philosophies

Pessimistic control locks a row while you work so no one else can change it. Optimistic concurrency control assumes conflicts are rare. It lets everyone read freely and only checks for a conflict at commit time.

How The Check Works

  • Each row carries a version number or timestamp.
  • A transaction reads the version, does its work, then on update says update where version still matches.
  • If another writer bumped the version first, the update matches zero rows and the transaction retries.

When To Prefer It

  • Workloads with low contention where conflicts are uncommon.
  • Web apps where holding locks across user think time is dangerous.
  • It avoids lock waits but pays with retries when conflicts do occur.

Key idea

Optimistic concurrency control skips locks and instead validates a version at commit, succeeding cheaply under low contention and retrying when a conflicting write slipped in.

Check yourself

Answer to earn rating on the learn ladder.

1. Optimistic concurrency control checks for conflicts:

2. Optimistic control works best when: