← InterviewIntermediate

How Does a Deadlock Happen, and How Do You Stop It?

Walk me through how a deadlock occurs and how you would prevent one in a system with several locks.

The scenario

A payments service hangs about once a week under load. Thread dumps show two threads: one holding lock(account:8814) waiting for lock(account:2201), the other the mirror image. CPU is near zero and 200 requests are queued.

What it tests

  • Whether the candidate can state the conditions rather than just the symptom
  • Whether they reach for lock ordering as the practical prevention
  • Whether they know how to detect a deadlock that has already happened

Answer it out loud first. Recognising a good answer is not the same skill as producing one.