Optimistic concurrency control in DBMS Theory - Time & Space Complexity
We want to understand how the time cost of optimistic concurrency control changes as more transactions run.
How does the system handle many users trying to update data at the same time?
Analyze the time complexity of the following optimistic concurrency control steps.
BEGIN TRANSACTION;
READ data;
-- user works on data locally --
VALIDATE that data was not changed by others;
IF validation passes THEN
COMMIT changes;
ELSE
ROLLBACK and retry;
END IF;
This code shows how a transaction reads data, checks for conflicts before saving, and retries if needed.
Look for repeated checks or retries that affect time.
- Primary operation: Validation step checking for conflicts.
- How many times: Depends on number of retries if conflicts occur.
As more transactions run, conflicts may increase, causing more retries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 validations, few retries |
| 100 | About 100 validations, some retries |
| 1000 | About 1000 validations, more retries possible |
Pattern observation: More transactions mean more validations and possible retries, increasing total work.
Time Complexity: O(n)
This means the time grows roughly in direct proportion to the number of transactions trying to commit.
[X] Wrong: "Validation always takes constant time regardless of transactions."
[OK] Correct: While a single validation is typically constant time, the overall time grows with concurrent transactions due to increased retries from conflicts.
Understanding how optimistic concurrency control scales helps you explain database behavior under load and design better systems.
"What if the system used pessimistic locking instead? How would the time complexity change?"
Practice
Optimistic Concurrency Control in databases?Solution
Step 1: Understand the optimistic concurrency control concept
It assumes that conflicts between transactions are rare, so it does not lock data during transaction execution.Step 2: Identify when conflicts are checked
Conflicts are detected only when a transaction tries to commit, by checking if data was changed by others.Final Answer:
Conflicts between transactions are rare and can be checked at commit time -> Option DQuick Check:
Optimistic concurrency control = Conflicts checked at commit [OK]
- Thinking data is locked during the whole transaction
- Assuming conflicts are prevented upfront
- Confusing optimistic with pessimistic locking
Solution
Step 1: Identify conflict detection methods in optimistic control
Optimistic concurrency control uses version numbers or timestamps to detect if data was changed by another transaction.Step 2: Eliminate locking and blocking options
Locking rows or blocking reads are typical of pessimistic control, not optimistic.Final Answer:
Using version numbers or timestamps on data -> Option BQuick Check:
Conflict detection = Version/timestamp check [OK]
- Confusing locking with optimistic control
- Thinking transactions are serialized automatically
- Assuming reads are blocked during writes
Solution
Step 1: Understand version checking in optimistic concurrency
Each update increments the version number. The first user updates version 5 to 6 successfully.Step 2: Check second user's commit attempt
The second user still has version 5 but data is now version 6, so commit fails due to version mismatch.Final Answer:
The second user's commit fails due to version mismatch -> Option CQuick Check:
Version mismatch causes commit failure [OK]
- Assuming both commits succeed
- Thinking system locks data automatically
- Ignoring version number checks
Solution
Step 1: Identify key step in optimistic concurrency control
It requires checking the version or timestamp before committing to detect conflicts.Step 2: Analyze the developer's problem
If conflicts are not detected, likely the version/timestamp check is missing or incorrect.Final Answer:
They are not checking version or timestamp before commit -> Option AQuick Check:
Missing version check = No conflict detection [OK]
- Confusing locking with optimistic control
- Assuming serialization detects conflicts
- Mixing optimistic and pessimistic methods
Solution
Step 1: Understand conflict resolution in optimistic concurrency
When conflict is detected, the system can try to merge changes or reject one update to maintain data integrity.Step 2: Compare with other concurrency methods
Locking or queuing are pessimistic methods, not used in optimistic concurrency control.Final Answer:
Automatically merge changes if possible or reject one update -> Option AQuick Check:
Conflict resolution = Merge or reject update [OK]
- Thinking locking is used in optimistic control
- Assuming silent overwrites happen
- Confusing queuing with optimistic concurrency
Ready to prove it?
Pass the test and this pattern moves unknown
