Managing Deadlocks in Shared Databases | Generated by AI

Home 2026.04

Question: In a shared database used by multiple downstream systems, we can’t control other teams’ application logic. So is it realistic to ensure consistent lock ordering across all transactions, and are we limited to only controlling our own application behavior?

Answer:

You’re right about the core constraint: in a shared database, you do not control global behavior. Each team owns its own transactions, query patterns, and update order. So the “perfect world rule” (global consistent lock ordering) is usually not enforceable end-to-end.

But the conclusion is not “there’s nothing we can do”—it becomes:

You cannot control everyone, but you can still shape the system so deadlocks become rare and survivable.


1. What is actually realistic in shared DB environments

In systems like MySQL or IBM Db2 used by multiple services:

You can’t control:

You can control:

So yes: local control only, not global coordination


2. So does “consistent lock ordering” still matter?

Yes—but only within your own application boundary.

Think of it like this:

Even if other teams are chaotic, your own consistency:

In practice, a large portion of deadlocks in production systems are actually self-caused within one service, not cross-service chaos.


3. Why batching vs single SQL is not the real answer

Your earlier framing:

200ms lock vs 20ms lock → batching is better

This is not the key dimension.

A better model:

Deadlock probability depends on:

NOT just:

So batching can actually go either way:

Approach Effect
Many small auto-commit SQLs shorter locks, but many interleavings
Batch transaction fewer interleavings, but longer lock window

So there is no universal “better”—only tradeoffs.


4. In shared DB systems, the real strategy is different

Instead of trying to eliminate deadlocks globally, production systems do this:

(1) Accept deadlocks as normal

Both MySQL and DB2 explicitly assume:

So production-grade systems rely on:

retry + idempotency


(2) Minimize your “deadlock footprint”

You can reduce your chance of participating:


(3) Design for retry safety

If a deadlock happens:

So architecture assumes:

“conflicts are inevitable, failure is transient”


5. The key mental correction

Your intuition:

“We should reduce lock time to reduce deadlocks”

More accurate model:

“We should reduce conflict probability per transaction, and assume retries will happen”

Because:


6. What “good engineering” looks like in shared DB environments

A mature approach usually includes:

At application level (your control)

At system level (shared reality)


7. Final answer

Yes—you are correct that:

In a shared database, you cannot enforce global lock ordering.

But the correct conclusion is not to focus on global control. Instead:

In real-world large systems, stability comes less from “avoiding all deadlocks” and more from:

“making deadlocks rare + making retries safe + keeping impact small”


If you want, I can break down a real production pattern like: how high-scale banking systems or payment systems design DB access to survive constant deadlocks without user-visible failure.


Back Donate