Mitigating Deadlocks in high concurrency environments - Shopify
Aug 14, 2024

Summary
This post details the implementation of composite primary keys in MySQL to mitigate deadlocks during concurrent up-insert operations. By including the `account` column in the composite primary key alongside the existing `id`, data is physically ordered on disk by account. This compartmentalizes gap locks within each process instance, preventing overlaps and dramatically reducing deadlock occurrences without requiring application-level logic changes. A unique index on `id` was added for backward compatibility.
More from the Data Store Selection and Usage trail
- We replaced Redis with MySQL for inventory reservations—and it scaled (2026) - Shopify
- Safely Adding NOT NULL Columns to Your Database Tables - Shopify
- Double Entry Transition Tables: How We Track State Changes At Shopify - Shopify
- Shopify’s Unique Data Science Hierarchy Of Needs - Shopify
- Upgrading MySQL at Shopify - Shopify