---
title: "Dynamic ProxySQL Query Rules"
description: "Shopify runs thousands of ProxySQL instances in front of MySQL and uses query rules to reroute, rewrite or reject queries matching a regex, often during incidents."
url: "https://swe.quest/articles/dynamic-proxysql-query-rules-1a5fc44afbe14316"
canonical: "https://shopify.engineering/dynamic-proxysql-query-rules"
source: "Shopify Engineering"
published: "2021-04-20"
difficulty: "medium"
kind: "Case study"
reading_minutes: 10
tags: ["mysql","configuration","resilience","incident-response","distributed-systems"]
---

# Dynamic ProxySQL Query Rules

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2021-04-20, 10 min read, Case study. Original: [https://shopify.engineering/dynamic-proxysql-query-rules](https://shopify.engineering/dynamic-proxysql-query-rules)

## Summary

Shopify runs thousands of ProxySQL instances in front of MySQL and uses query rules to reroute, rewrite or reject queries matching a regex, often during incidents. To make rules safe, they patched their ProxySQL fork with a dry-run mode (active=0, log=1) that logs matched queries with exponential backoff instead of acting on them. A web app writes rules to a global key-value store, and a sidecar next to each ProxySQL polls it and syncs the mysql\_query\_rules table. If the sidecar and ProxySQL drift, both rule lists are wiped and rebuilt, favouring consistency over availability; rules also carry TTLs, and rules that must always be present are loaded statically from proxysql.cnf.

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)
- [Configuration management](https://swe.quest/tags/configuration.md) (Reliability & operations)
- [Resilience](https://swe.quest/tags/resilience.md) (Distributed systems)
- [Incidents & postmortems](https://swe.quest/tags/incident-response.md) (Reliability & operations)
- [Distributed systems](https://swe.quest/tags/distributed-systems.md) (Distributed systems)

## Prerequisites

- MySQL read replicas ([easy posts on MySQL](https://swe.quest/tags/mysql.md?difficulty=easy))
- database proxies ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))
- sidecar containers ([easy posts on Containers](https://swe.quest/tags/containers.md?difficulty=easy))

## Keywords

ProxySQL, MySQL, query rules, Shopify

## Related posts

- [Beyond synthetic testing: Capturing and replaying real database workloads at Airbnb](https://swe.quest/articles/beyond-synthetic-testing-capturing-and-replaying-real-database-workloads-at-ad8d4a3a0c1cef6e.md): Airbnb Engineering · 2026-10-06 · Medium · 11 min read · MySQL, Testing, Capacity planning, Performance engineering, Databases
  Airbnb replaced fragmented per-language client-side query logging with a client-agnostic capture point in ProxySQL, so real production MySQL traffic can be replayed offline.
- [How Shopify Dynamically Routes Storefront Traffic](https://swe.quest/articles/how-shopify-dynamically-routes-storefront-traffic-4416894393bf5cd2.md): Shopify Engineering · 2021-04-09 · Medium · 9 min read · Load balancing, Deployment, Refactoring & migrations, Configuration management, Resilience
  Shopify shifted storefront traffic from its legacy Rails storefront to a rewritten renderer using a rule-based routing system in nginx/OpenResty Lua load balancers, with rules stored as JSON in a control plane and managed via the spy chatbot so changes skip CI and deploys.
- [Shard Balancing: Moving Shops Confidently with Zero-Downtime at Terabyte-scale](https://swe.quest/articles/shard-balancing-moving-shops-confidently-with-zero-downtime-at-terabyte-scale-76a6177fa357eea4.md): Shopify Engineering · 2021-09-24 · Medium · 13 min read · MySQL, Distributed databases, Schema migrations, Scaling
  Shopify rebalances its MySQL shards by moving individual shops between pods online, with downtime confined to a brief cutover.
- [Optimizing Data Reliability: Heroku Connect & Drift Detection](https://swe.quest/articles/optimizing-data-reliability-heroku-connect-drift-detection-210645ff0f1dc0fb.md): Heroku Engineering · 2024-06-26 · Easy · 4 min read · Data pipelines, Consistency models, PostgreSQL
  Heroku Connect syncs Salesforce data into Postgres by polling time windows, and long-running Salesforce transactions that commit after the window has moved can be missed, causing drift.
- [Partitioning GitHub’s relational databases to handle scale](https://swe.quest/articles/partitioning-github-s-relational-databases-to-handle-scale-7b5ea75acfc088c0.md): GitHub Engineering · 2021-09-27 · Medium · 9 min read · MySQL, Distributed databases, Scaling, Schema migrations, Ruby
  GitHub split its monolithic \`mysql1\` MySQL cluster by first virtually partitioning tables into schema domains (declared in YAML) and enforcing boundaries with query and transaction linters, then physically moving domains to new clusters via Vitess vertical sharding or a custom ProxySQL/GTID-based write-cutover script.

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)
