Understanding Deadlocks in Modern Systems

Understanding Deadlocks in Modern Systems

A deep dive into how deadlocks occur in concurrent databases and application runtimes, exploring Coffman conditions, Wait-For Graphs, lock ordering, and proven strategies to prevent deadlock cycles in production.

DeadlockConcurrencyPostgreSQLDatabasesArchitecture

What Is a Deadlock?

High-concurrency Backend Architecture တွေမှာ Database Queries တွေ၊ Thread Pools တွေနဲ့ Background Workers တွေ ပြိုင်တူ အလုပ်လုပ်ကြရပါတယ်။ System ပေါ်မှာ Concurrent Transactions တွေ များပြားလာတဲ့အခါ Database Response Time နှေးကွေးသွားတာထက် ပိုဆိုးတဲ့ ပြဿနာတစ်ခု ကြုံတွေ့ရတတ်ပါတယ်။ အဲဒါကတော့ Deadlock ခေါ် အပြန်အလှန် ချည်နှောင်မိပြီး ရှေ့မဆက်နိုင်တော့တဲ့ ပိတ်ဆို့မှု ဖြစ်ပါတယ်။

ရိုးရိုးရှင်းရှင်း ပြောရရင် Deadlock ဆိုတာ Thread သို့မဟုတ် Database Transaction နှစ်ခု (သို့မဟုတ် နှစ်ခုထက်မက) ဟာ တစ်ဖက်က ချုပ်ကိုင်ထားတဲ့ Shared Resource (ဥပမာ Row Lock သို့မဟုတ် Memory Mutex) ကို ရယူဖို့ အပြန်အလှန် စောင့်ဆိုင်းနေရင်း ဘယ်ဘက်ကမှ ဆက်လက် လုပ်ဆောင်နိုင်ခြင်း မရှိတော့ဘဲ ရပ်တန့်သွားတဲ့ အခြေအနေ ဖြစ်ပါတယ်။

ဥပမာအားဖြင့် Transaction A က Row 1 ကို Lock ယူထားပြီး Row 2 ကို စောင့်နေချိန်မှာ၊ Transaction B က Row 2 ကို Lock ယူထားပြီး Row 1 ကို ပြန်စောင့်နေတဲ့အခါ Deadlock ဖြစ်ပေါ်လာပါတယ်။ ပြင်ပကနေ အတင်းအကျပ် ဖြတ်တောက်မပေးမချင်း ဒီ Transactions တွေဟာ တစ်ဖက်နဲ့တစ်ဖက် စောင့်နေရင်း System Resources တွေကို ပိတ်မိစေပါတယ်။

The Four Coffman Conditions

Computer Science မှာ Deadlock တစ်ခု ဖြစ်ပေါ်လာဖို့အတွက် အခြေခံ လိုအပ်ချက် ၄ ချက် ရှိပါတယ်။ ၁၉၇၁ ခုနှစ်မှာ Edward G. Coffman Jr. က ဖော်ထုတ်ခဲ့တဲ့ ဒီအချက်တွေကို Coffman Conditions လို့ ခေါ်ဆိုပြီး၊ အဆိုပါ ၄ ချက်စလုံး တစ်ပြိုင်နက်တည်း ဖြစ်ပေါ်မှသာ Deadlock ဖြစ်နိုင်ပါတယ်။

Loading flowchart...
  1. Mutual Exclusion: Resource တစ်ခုကို အချိန်တစ်ခုအတွင်း Process တစ်ခုတည်းကသာ သီးသန့် အသုံးပြုခွင့် ရှိရပါမယ်။ အခြား Process တွေက မျှဝေသုံးစွဲခွင့် မရှိပါဘူး။
  2. Hold and Wait: Process တစ်ခုက Resource အနည်းဆုံး တစ်ခုကို ချုပ်ကိုင်ထားဆဲ အခြေအနေမှာ နောက်ထပ် Resource အသစ်တစ်ခုကို ထပ်မံ ရယူဖို့ စောင့်ဆိုင်းနေခြင်း ဖြစ်ပါတယ်။
  3. No Preemption: Process တစ်ခုက ရယူထားတဲ့ Resource ကို အခြား Process တစ်ခုက အတင်းအကျပ် လုယူခွင့် မရှိပါဘူး။ မူလ Process က အလုပ်ပြီးမြောက်လို့ သူ့အလိုအလျောက် Release လုပ်ပေးမှသာ ရယူနိုင်ပါတယ်။
  4. Circular Wait: Process တွေအချင်းချင်း တစ်ခုက တစ်ခုဆီက Resource ကို ကွင်းဆက်ပုံစံ စောင့်ဆိုင်းနေတဲ့ Closed Loop သို့မဟုတ် Circular Dependency တစ်ခု ဖြစ်ပေါ်နေခြင်း ဖြစ်ပါတယ်။

ဒီ အချက် ၄ ချက်ထဲက တစ်ချက်တည်းကို ဖျက်ဆီးပစ်နိုင်ရုံနဲ့ Deadlock ဖြစ်ပေါ်မှုကို လုံးဝ တားဆီးနိုင်ပါတယ်။

Database Transaction Deadlocks

လက်တွေ့ Web Development မှာ အဖြစ်အများဆုံး Deadlock ဟာ Relational Database Management Systems (PostgreSQL, MySQL InnoDB) တွေမှာ ဖြစ်ပွားလေ့ ရှိပါတယ်။ အထူးသဖြင့် Financial Transfers သို့မဟုတ် E-commerce Inventory Allocation လိုမျိုး Multi-row Updates တွေမှာ အတွေ့ရများပါတယ်။

ဥပမာ Customer A က Customer B ဆီ ငွေလွှဲချိန်မှာ User Table ရဲ့ Row A နဲ့ Row B ကို Update လုပ်ရပါတယ်။ တစ်ချိန်တည်းမှာပဲ Customer B က Customer A ဆီ ငွေပြန်လွှဲတဲ့ Transaction က ပြိုင်တူ ဝင်ရောက်လာပါတယ်။

  • Transaction 1 (A -> B): Row A ကို SELECT ... FOR UPDATE နဲ့ Lock ယူလိုက်ပါတယ်။
  • Transaction 2 (B -> A): Row B ကို SELECT ... FOR UPDATE နဲ့ Lock ယူလိုက်ပါတယ်။
  • Transaction 1: Row B ကို ထပ်မံ Update လုပ်ဖို့ ကြိုးစားချိန်မှာ Transaction 2 က Lock ယူထားတဲ့အတွက် Block ဖြစ်ပြီး စောင့်နေရပါတယ်။
  • Transaction 2: Row A ကို ထပ်မံ Update လုပ်ဖို့ ကြိုးစားချိန်မှာ Transaction 1 က Lock ယူထားတဲ့အတွက် Block ဖြစ်သွားပါတယ်။

ဒီအခြေအနေမှာ Database Engine ဟာ Deadlock Detection Algorithm ကို အသုံးပြုပြီး Transaction တစ်ခုကို အရှုံးပေး စေခိုင်းကာ (Victim Transaction အဖြစ် သတ်မှတ်ပြီး) ERROR: deadlock detected (SQLSTATE 40P01) နဲ့ အလိုအလျောက် Rollback လုပ်ပစ်လေ့ ရှိပါတယ်။

Concurrency Deadlocks in Application Code

Database Level အပြင် Application Code Level မှာလည်း Multi-threading သို့မဟုတ် Shared Memory Access လုပ်တဲ့အခါ Deadlocks တွေ ဖြစ်ပွားနိုင်ပါတယ်။ Node.js Worker Threads, Go Goroutines, Java Threads ဒါမှမဟုတ် Python Multiprocessing တွေမှာ Mutex Locks တွေကို သုံးစွဲချိန်မှာ Lock Acquisition Order မညီမျှရင် ဒီပြဿနာ ပေါ်ပေါက်တတ်ပါတယ်။

အောက်ပါ TypeScript ဥပမာမှာ Database Deadlock မဖြစ်အောင် Multi-row Lock Acquisition Order ကို တစ်သမတ်တည်း စီစဉ်ပေးတဲ့ Deterministic Lock Ordering နည်းလမ်းကို ကြည့်ရှုနိုင်ပါတယ်။

async function transferFunds(fromId: string, toId: string, amount: number) {
  // Always lock accounts in deterministic alphanumeric order to prevent deadlocks
  const [firstId, secondId] = [fromId, toId].sort()

  return await db.transaction(async (tx) => {
    // Acquire row-level pessimistic locks in predictable sequence
    const acc1 = await tx.account.findUnique({ where: { id: firstId }, lock: 'forUpdate' })
    const acc2 = await tx.account.findUnique({ where: { id: secondId }, lock: 'forUpdate' })

    if (!acc1 || !acc2) throw new Error('Account not found')

    // Perform business updates safely without circular wait condition
    await tx.account.update({ where: { id: fromId }, data: { balance: { decrement: amount } } })
    await tx.account.update({ where: { id: toId }, data: { balance: { increment: amount } } })
  })
}

Resource IDs တွေကို အမြဲတမ်း ငယ်စဉ်ကြီးလိုက် Sort လုပ်ပြီးမှ Lock ယူတဲ့အတွက် Transaction အားလုံးဟာ တူညီတဲ့ Order အတိုင်းသာ Lock ကို တောင်းဆိုကြပါတယ်။ ဒါကြောင့် Circular Wait Condition လုံးဝ မဖြစ်ပေါ်နိုင်တော့ပါဘူး။

Deadlock Detection vs Deadlock Prevention

Software Engineering မှာ Deadlock ကို ကိုင်တွယ်ဖြေရှင်းဖို့ မဟာဗျူဟာ နှစ်မျိုး ရှိပါတယ်။

  1. Deadlock Prevention: System ဒီဇိုင်း ရေးဆွဲကတည်းက Coffman Conditions ထဲက အနည်းဆုံး တစ်ခုကို ချိုးဖျက်ထားတဲ့ နည်းလမ်း ဖြစ်ပါတယ်။ Resource တွေကို ကြိုတင် စီစစ်ထားတဲ့ Order အတိုင်းသာ Lock ယူစေခြင်း (Lock Hierarchy) သို့မဟုတ် Lock မသုံးဘဲ Optimistic Concurrency Control (OCC) ကို သုံးစွဲခြင်းတို့ ပါဝင်ပါတယ်။ Prevention လုပ်ထားရင် Runtime မှာ Deadlock လုံးဝ မဖြစ်နိုင်တော့ပါဘူး။

  2. Deadlock Detection and Recovery: Transactions တွေကို လွတ်လပ်စွာ Lock ယူခွင့် ပေးထားပြီး နောက်ကွယ်က Background Engine က Wait-For Graph (WFG) တည်ဆောက်ကာ Directed Cycle ရှိမရှိ ပုံမှန် စစ်ဆေးပါတယ်။ PostgreSQL မှာ deadlock_timeout (Default: 1 second) ပြည့်သွားတဲ့အခါ Background Process က Graph ထဲမှာ Cycle တွေ့ရင် Transaction တစ်ခုကို Abort လုပ်ပြီး Rollback ချပေးပါတယ်။

လက်တွေ့ High-throughput Systems တွေမှာ Detection အပေါ်ချည်း မှီခိုနေရင် Rollback ကြောင့် Database CPU Spikes နဲ့ Client-side Errors တွေ မကြာခဏ ဖြစ်ပေါ်နိုင်တဲ့အတွက် Prevention နည်းလမ်းကို ဦးစားပေး ဒီဇိုင်းဆင်သင့်ပါတယ်။

Practical Strategies to Eliminate Deadlocks

Production Environment မှာ Deadlocks တွေကို လျှော့ချဖို့ အင်ဂျင်နီယာတွေ လိုက်နာသင့်တဲ့ အကောင်းဆုံး နည်းလမ်းများစွာ ရှိပါတယ်။

  • Keep Transactions Short and Focused: Transaction Scope ထဲမှာ ကြာမြင့်တဲ့ Third-party API Calls, Email Notifications သို့မဟုတ် File Uploads တွေကို လုံးဝ မထည့်သွင်းသင့်ပါဘူး။ Database Transaction ဟာ စက္ကန့်ပိုင်း မဟုတ်ဘဲ မီလီစက္ကန့် အနည်းငယ်အတွင်း အပြီးသတ် Commit လုပ်နိုင်ရပါမယ်။ Lock ကိုင်ထားချိန် နည်းလေလေ၊ တိုက်ဆိုင်မှု ဖြစ်နိုင်ခြေ နည်းလေလေ ဖြစ်ပါတယ်။
  • Use Optimistic Locking with Versioning: Read အကြိမ်ရေ များပြီး Write အကြိမ်ရေ နည်းတဲ့ စနစ်တွေမှာ Row Lock မယူဘဲ Record မှာ version Column တစ်ခု ထည့်သွင်းထားနိုင်ပါတယ်။ Update လုပ်ချိန်မှာ WHERE id = :id AND version = :currentVersion နဲ့ စစ်ဆေးပြီး ပြိုင်တူ ပြင်ဆင်မှု ရှိခဲ့ရင် Application Layer ကနေ Safe Retry ပြုလုပ်နိုင်ပါတယ်။
  • Implement Exponential Backoff with Jitter: Database က Deadlock Error ပြန်ပေးလာတဲ့အခါ ချက်ချင်း တစ်ပြိုင်နက် ပြန် Retry လုပ်ရင် ထပ်မံ Deadlock ဖြစ်ဖို့ ရာခိုင်နှုန်း များပါတယ်။ Random Jitter ပါဝင်တဲ့ Exponential Backoff Algorithm သုံးပြီး ခေတ္တ စောင့်ဆိုင်းကာမှ Retry လုပ်သင့်ပါတယ်။
  • Reduce Transaction Isolation Level When Appropriate: အမြဲတမ်း SERIALIZABLE Isolation Level သုံးမယ့်အစား လိုအပ်တဲ့ Business Use Case ပေါ် မူတည်ပြီး READ COMMITTED ကို သင့်တင့်စွာ ရွေးချယ် အသုံးပြုနိုင်ပါတယ်။

Comparing Locking and Concurrency Strategies

System Architecture တစ်ခုချင်းစီရဲ့ Concurrency လိုအပ်ချက်ပေါ် မူတည်ပြီး ရွေးချယ်နိုင်တဲ့ နည်းလမ်းတွေကို အောက်ပါအတိုင်း နှိုင်းယှဉ်နိုင်ပါတယ်။

StrategyMechanismPerformance OverheadBest Use Case
Deterministic Lock OrderingAcquire resources in globally sorted identifier sequenceMinimal (In-memory sorting)Multi-row batch database updates
Optimistic Concurrency ControlVersion column checks at commit phase without locksVery low read cost, aborts on conflictHigh-read, low-contention architectures
Pessimistic Row LockingExplicit row-level locks via SELECT FOR UPDATEHigh lock contention and query latencyHigh-stakes financial ledger balances
Queue-Based SerializationSingle-consumer worker processes tasks sequentiallyThroughput bottleneck per queue partitionFlash sales, single inventory counters

Final Thoughts

Deadlock ဆိုတာ Distributed Systems နဲ့ Modern Databases တွေမှာ Concurrency မြင့်တက်လာချိန်မှာ ရှောင်လွှဲမရ ကြုံတွေ့ရတတ်တဲ့ သဘာဝ ဖြစ်စဉ်တစ်ခု ဖြစ်ပါတယ်။ ဒါပေမဲ့ ဒါကို စနစ်ရဲ့ ပုံမှန် ချွတ်ယွင်းချက်အဖြစ် လက်ခံထားဖို့ မလိုပါဘူး။

Coffman Conditions ၄ ချက်ကို ရှင်းလင်းစွာ ချိန်ထိုးပြီး Deterministic Lock Ordering ကို အသုံးချခြင်း၊ Transaction Boundaries တွေကို တိုတောင်းအောင် ထိန်းသိမ်းခြင်းနဲ့ လိုအပ်ပါက Optimistic Locking သို့မဟုတ် Queue Serialization ဖြင့် decouple လုပ်ခြင်းက high-concurrency environments တွေမှာ deadlock free ဖြစ်တဲ့ data integrity ကို အပြည့်အဝ အာမခံပေးနိုင်ပါတယ်။