---
title: "Mitigating Deadlocks in high concurrency environments"
description: "Concurrent batch upserts into an InnoDB table keyed by a sequential id can deadlock even when processes touch different rows, because the gap locks taken on unique-key collisions span neighbouring index entries that other processes also need."
url: "https://swe.quest/articles/mitigating-deadlocks-in-high-concurrency-environments-f958e9ca77901412"
canonical: "https://shopify.engineering/mitigating-deadlocks-in-high-concurrency-environments"
source: "Shopify Engineering"
published: "2024-08-14"
difficulty: "medium"
kind: "Case study"
reading_minutes: 3
tags: ["mysql","concurrency","storage-engines","databases"]
---

# Mitigating Deadlocks in high concurrency environments

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2024-08-14, 3 min read, Case study. Original: [https://shopify.engineering/mitigating-deadlocks-in-high-concurrency-environments](https://shopify.engineering/mitigating-deadlocks-in-high-concurrency-environments)

## Summary

Concurrent batch upserts into an InnoDB table keyed by a sequential id can deadlock even when processes touch different rows, because the gap locks taken on unique-key collisions span neighbouring index entries that other processes also need. Shopify mitigated this by changing the primary key to a composite (account, id), so rows are physically clustered per account and each per-account ETL process's gap locks stay within its own range. An extra unique index on id preserves existing joins with no application changes. The fix let them remove artificial delays added to avoid deadlocks and significantly increased import job throughput.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [MySQL](https://swe.quest/tags/mysql.md) (Data & databases)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- MySQL InnoDB basics ([easy posts on MySQL](https://swe.quest/tags/mysql.md?difficulty=easy))
- database locking ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- primary keys and indexes

## Keywords

MySQL, InnoDB, gap locks, composite primary key, deadlocks

## Related posts

- [Debugging deadlocks in Postgres](https://swe.quest/articles/debugging-deadlocks-in-postgres-e07d42201c9784eb.md): incident.io · 2025-02-04 · Medium · 6 min read · PostgreSQL, Concurrency, Debugging, Consistency models
  Postgres deadlocks come from transactions acquiring locks in inconsistent orders, and the main fix is to restructure transactions so they always lock resources in the same sequence.
- [We replaced Redis with MySQL for inventory reservations—and it scaled](https://swe.quest/articles/we-replaced-redis-with-mysql-for-inventory-reservations-and-it-scaled-2f8fab4ab3565f02.md): Shopify Engineering · 2026-05-12 · Medium · 10 min read · MySQL, E-commerce, Concurrency, Scaling, Performance engineering
  Shopify moved checkout inventory reservations from Redis to MySQL by modeling one row per sellable unit and using SELECT ...
- [How savepoints quietly throttled our Postgres queue](https://swe.quest/articles/how-savepoints-quietly-throttled-our-postgres-queue-a32737dd90a62c34.md): incident.io · 2026-10-06 · Hard · 15 min read · PostgreSQL, Performance engineering, Storage engines, Concurrency, Scaling
  Using a SAVEPOINT per tick in a Postgres-backed job queue made every locked row carry a MultiXact ID in xmax.
- [One, Two, Skip a Few...](https://swe.quest/articles/one-two-skip-a-few-b1af2f9386afbc09.md): incident.io · 2021-07-12 · Medium · 8 min read · PostgreSQL, Replication, Incidents & postmortems, Databases
  Postgres sequences pre-log 32 values to the WAL (SEQ\_LOG\_VALS), so replicas see sequence state as it would be after a primary crash.
- [When upserts don’t update but still write: Debugging Postgres performance at scale](https://swe.quest/articles/when-upserts-don-t-update-but-still-write-debugging-postgres-performance-at-cd64ef72ac9ca8ec.md): Datadog Engineering · 2026-03-23 · Medium · 11 min read · PostgreSQL, Performance engineering, Storage engines, Debugging, SQL
  A Postgres \`INSERT ... ON CONFLICT DO UPDATE ... WHERE\` upsert still locks the conflicting row even when the WHERE clause blocks the update.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
