While exploring deadlock management in databases, I stumbled upon an

Arpit Bhayani

Arpit Bhayani

Oct 26, 2024 • 2 min read


While exploring deadlock management in databases, I stumbled upon an interesting aspect involving descending indexes and slow-function implementations. Let’s dig deeper …

When databases perform operations like SELECT, UPDATE, or DELETE, the way rows are accessed and locked is critical.

Imagine we’ve indexed a column, say amount, in descending order. The natural consequence here is that whenever an UPDATE operation kicks in using this index, the database starts dealing with the highest amount values first and progresses downward.

Here’s where it gets tricky:

If another operation begins accessing the same dataset but starts from the lowest value upward, the overlap in accessed rows can lead to a deadlock. Essentially, one transaction waiting indefinitely for a lock held by another, and vice versa; a classic deadlock :) To prevent we need to

  1. make sure that the access of rows/index entries/resources happens uniformly (either low to high or high to low). This drastically reduces conflicts and thus deadlocks.

  2. keep your transactions to a bare minimum making sure that the locks are released as quickly as possible. This cuts down potential deadlock instances.

⚡ I keep writing and sharing my practical experience and learnings every day, so if you resonate then follow along. I keep it no fluff.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses