Level 1: Bronze
A memorized definition. Not wrong, but it stops there.
Pessimistic locks first and optimistic checks later.
2 stars Asked often.
In one or two sentences
Pessimistic locking assumes conflicts are common and locks first. Optimistic locking assumes they are rare and only checks at write time whether the row changed.
Say it out loud, or type it below. What you type never leaves this device.
A memorized definition. Not wrong, but it stops there.
Pessimistic locks first and optimistic checks later.
Correct, but too broad or missing context.
Pessimistic locking locks the row when it is read so others cannot touch it. Optimistic locking does not lock. It keeps a version value and compares it on save to see whether the row changed in between.
Reaches principle, use and trade-off, with words that invite the next question.
Choose by how likely and how costly conflicts are. If the same row is edited concurrently often, optimistic locking causes many failures and retries, so pessimistic is better. If it is rare, optimistic avoids lock waits and gives better throughput. Pessimistic locking can deadlock if locks are taken in different orders, so use one order. In optimistic locking an update that includes the version in its condition and changes zero rows signals a conflict, and you retry. Do not hold a lock while calling an outside service.
These are likely next questions from an interviewer. Think of your answer first, then press the button.
1 / 3
How do you implement optimistic locking?
Add a version column and update only if the version is still the one you read. If zero rows change, treat it as a conflict and try again.
2 / 3
What is SELECT FOR UPDATE?
A statement that takes a write lock on the selected rows until the transaction ends, so others cannot modify them.
3 / 3
How do you reduce deadlocks?
Use one lock order, keep transactions short, and retry when the database detects one and fails a transaction.