HardπŸ‘€ 8-15 years 1 min read

What causes database deadlocks, and how do you prevent them in a busy OLTP system?

Asked inAmazonMicrosoftbankingDeloitte
#deadlock#lock ordering#transaction#oltp#production
Report issue

⚑ Short Answer

Two transactions acquire the same rows/locks in opposite order and wait on each other; the DB detects the cycle and kills one (deadlock victim). Prevent with a consistent lock/update order, short transactions, the right indexes (to lock fewer rows), and retry logic on the victim.

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat causes database deadlocks, and how do you prevent them in a busy OLTP system?”

🧠Mind Map Answer

Remember It Faster

Cause→opposite lock-acquisition order
DB action→detect cycle → kill a victim
Prevent→consistent order, short txns, good indexes
Handle→catch + retry the victim

πŸ”₯What If?

Think Beyond the Expected

Why can a MISSING index actually cause more deadlocks?

Without a good index, a query scans and locks far more rows (or escalates to range/table locks), widening the window for lock conflicts. Adding the right index narrows locking to the exact rows, reducing both contention and deadlocks.

πŸ˜‚Real World

OLTP deadlocks (order/inventory/ledger) are routine under load; the durable fixes are consistent update ordering, keeping transactions short, indexing to lock fewer rows, and idempotent retry on the deadlock victim.

🎯Interviewer's Expectation

Keywords they're listening for:

βœ“ opposite-order lock cycleβœ“ victim selectionβœ“ consistent orderingβœ“ short txns + indexes reduce lockingβœ“ retry the victim

⚠️Common Mistakes

  • βœ—Updating rows in inconsistent order across code paths
  • βœ—Long transactions holding locks across external calls
  • βœ—No retry on the deadlock victim

βœ…Best Practices

  • βœ“Acquire/update rows in a consistent order
  • βœ“Keep transactions short; index to lock fewer rows
  • βœ“Implement idempotent retry on deadlock errors

πŸ”Follow-up Questions

  • 1How do you read a deadlock graph / log?
  • 2Why do shorter transactions reduce deadlocks?
  • 3How does lock escalation contribute?

🧩Related Technologies

deadlock graphlock escalationretry/backoff

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β€” AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Transactions (SQL)
Interview question: "What causes database deadlocks, and how do you prevent them in a busy OLTP system?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions