Acquire locks in a consistent global order, keep transactions short, lower isolation where it's safe, and retry the loser when a deadlock still happens. A deadlock is a cycle: transaction A holds row 1 and waits for row 2, while B holds row 2 and waits for row 1 — neither can proceed, so the DB kills one.
The cycle you must break
Tx A Tx B
holds row 1 ──── waits ────▶ row 2 (held by B)
▲ │
│ waits │ holds
└────────── row 1 ◀────────────┘
(cycle = deadlock; DB aborts one victim)
