SQL Server deadlocks interview questions 6 questions in 2 chapters

SQL Server deadlocks versus blocking, lock monitor, conversion deadlocks, Key Lookup deadlocks, RCSI, and error 1205 retries.

Part of SQL fundamentals learning path

  1. 01Blocking, detection and retry4 questions
  2. 02Conversion deadlocks and Key Lookup deadlocks

Blocking, detection and retry

  • 1.1 What is the difference between blocking and a deadlock? Medium 2 min

    Blocking is a one-way wait: the second session proceeds once the first releases its lock. A deadlock is a wait-for cycle, so nobody can finish unless the engine aborts one transaction. SQL Server does that with error 1205; blocking does not resolve itself that way, and by default it can last until the holder commits, rolls back, or a lock timeout fires.

    Read the full answer
  • 1.2 How does SQL Server detect and resolve a deadlock? Medium 2 min

    A lock monitor thread searches the wait-for graph for a cycle. The default interval is 5 seconds; if deadlocks are frequent it can drop as low as 100 milliseconds, and after a detection the next lock waits can trigger an immediate search. The engine then chooses a victim — lower DEADLOCK_PRIORITY first, then the transaction that is cheapest to roll back — rolls that transaction back, and returns error 1205.

    Read the full answer
  • 1.3 How do you minimize deadlocks in SQL Server? Medium 2 min

    I cannot reduce deadlocks to zero, but I can make them rare. I make every transaction lock objects in the same order, keep transactions short and free of user interaction, and use a selective index so a query locks fewer rows than a scan would. I also avoid REPEATABLE READ or SERIALIZABLE unless I need them, and I consider READ_COMMITTED_SNAPSHOT so readers do not take shared locks on the data.

    Read the full answer
  • 1.4 What should your application do when it receives error 1205? Medium 2 min

    Error 1205 means this session was the deadlock victim and its whole transaction has already been rolled back. I catch that error, retry the entire unit of work, and pause briefly with a randomized delay before the retry so the other session can finish and release its locks. If I ignore 1205, the application may keep going as if the writes were committed.

    Read the full answer

Conversion deadlocks and Key Lookup deadlocks

  • 2.1 Can a deadlock happen on a single row, and how do you prevent a conversion deadlock? Medium 2 min

    Yes. In REPEATABLE READ or SERIALIZABLE, two sessions can each take a shared lock on the same row, then both try to convert it to exclusive for an update. Neither conversion can proceed, which is a conversion deadlock. I prevent it by reading with UPDLOCK: only one session can hold an update lock on the resource, so the second waits from the start and no cycle forms.

    Read the full answer
  • 2.2 Can a simple SELECT cause a deadlock? Hard 3 min

    Yes. A SELECT that seeks a nonclustered index and then does a Key Lookup into the clustered index locks those two structures in one order. A concurrent UPDATE typically locks the clustered row first and then has to maintain the nonclustered index, which is the opposite order, so a cycle can form. I prevent it with a covering index, or I turn on READ_COMMITTED_SNAPSHOT so the SELECT no longer takes shared locks on the data. NOLOCK is the wrong fix, because it allows dirty reads.

    Read the full answer