Deadlock Handling aur MVCC in MySQL

Other Blogs

Blogs ❯❯ SQL

Image could not load

Deadlock Handling In MySQL by FlyD on Unsplash

जब multiple users एक साथ database को access करते हैं, तो उनके बीच data के operations में conflicts हो सकते हैं। Deadlock और MVCC (Multi-Version Concurrency Control) कुछ ऐसे concepts हैं जो ऐसे conflicts को handle करने में help करते हैं।

आज हम इन दोनो को समझेंगे, और Deadlock को Handle करना भी सीखेंगे ।

MVCC (Multi-Version Concurrency Control) क्या है?

MySQL के default storage engine, InnoDB, MVCC implement करता है जिससे concurrency improve होती है और readers और writers एक दूसरे को block नहीं करते।

  • जब transaction start होता है, वो एक consistent snapshot लेता है जिसमे only data committed पहले तक ही दिखाई देता है।

  • ये snapshot undo log से maintain होता है—अगर data change हुआ है तो InnoDB पुरानी versions use करता है read के लिए। 

  • READ COMMITTED में हर SELECT new ReadView लेता है, जबकि REPEATABLE READ में transaction के पहले SELECT का snapshot बार-बार repeat होता है।

Benefits :

  • Read operations lock-free रहती हैं → read-lock wait नहीं होती और deadlocks का risk कम होता है।

  • Consistent read without blocking writer processes.

MySQL में Deadlock क्या है ?

Deadlock तब होता है जब दो या ज़्यादा transactions एक दूसरे का resource lock चाहते रहते हैं—और कोई नहीं release करता।

MySQL automatically deadlocks detect करता है using a wait-for graph, फिर एक transaction को “victim” choose करके rollback करता है (usually जो least work किया हो).

Deadlock detect करने के तरीके :
  • SHOW ENGINE INNODB STATUS; command से latest deadlock detail मिलता है—transaction IDs, involved SQL, locks etc.

  • अगर innodb_print_all_deadlocks = ON set हो, तो सब deadlocks error log में record होंगे।

Deadlock Prevention & Handling

Consistent Lock Ordering
  • सब transactions को same resource order (tables, rows) में lock करने का pattern follow करना चाहिए. इससे circular waits avoid होते हैं।

 Short Transaction Duration
  • Transactions को छोटा रखो—unnecessary processing transaction के बाहर करो।

  • Large operations को batch‑wise break करो. Toga locks कम duration के लिए hold रहेंगे।

Suitable Isolation Level
  • Default REPEATABLE READ में gap locks use होती हैं जिससे deadlock hone के chances बढ़ जाते हैं।

  • अगर consistent read sequence required नहीं हो, तो READ COMMITTED use करो—less locking, fewer deadlocks.

Index Optimization
  • WHERE, JOIN, ORDER BY columns properly indexed hone चाहिए. Full scans से locks बढ़ जाते हैं।

  • Indexing से lock scope limited होता है।

Retry Logic In Application
  • Application में retry logic implement करो: अगर deadlock error आये (error code 1213), तो transaction को exponential backoff के साथ retry करो।

Use SAVEPOINTs (Optional)
  • Complex transactions में SAVEPOINT set करो, जिससे partial rollback possible हो without aborting full transaction.

Monitoring and Testing
  • Regularly deadlock logs review करो, patterns identify करो।

  • Tools use करो जैसे Percona PMM, Grafana dashboards, या SHOW ENGINE INNODB STATUS snapshots.

  • Load testing करके simulated contention detect करो।

MVCC से Deadlocks बचना

  • MVCC ensure करता है कि read operations non-blocking हो, जिससे readers के बीच deadlocks का risk कम हो जाता है।

  • लेकिन write transactions (INSERT, UPDATE, DELETE) अब भी row-level locking का use करते हैं, जिसमे deadlocks possible हैं (especially when two writes target same rows or index ranges).

  • MySQL का MVCC और InnoDB deadlock detection system यह ensure करता है कि user का system hang न हो — एक transaction automatically abort कर दिया जाता है और application उससे दुबारा try करती है।

🗣️ Reddit Users की Real Insights

Repeatable Read में gap locks कि वजह से delete operation 7M rows पर fail हुआ, but Read Committed पर same query 2 minutes में successful हुआ।

मतलब isolation level impact करता है deadlock behavior पर।

Avoid triggers and foreign keys for high concurrency workloads—these can cause deadlocks due तो implicit locking overhead.

Triggers का execution original transaction का हिस्सा होता है और यह easily से deadlock का reason बन सकते हैं।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !