Why Magento Reindexing Can Lock Your Catalog
How a full Magento reindex locked MySQL catalog tables during a sale on one project, and how switching to scheduled Mview changelog indexing helped.
· 2 min read · Erik Kubica
In 2017, I spent a Black Friday evening at a terminal while an enterprise Magento 2 store slowed to a crawl.
The symptom was clear: customer checkouts stopped. The server CPU was idling at 12%, but MySQL had 180 active threads stuck in Waiting for table metadata lock.
The merchant had run a product catalog import for 85,000 SKUs right before launching a 20% discount promotion. A scheduled cron job triggered bin/magento indexer:reindex catalog_product_category.
Suddenly, every customer trying to add a pair of shoes to their basket was queued behind a full catalog rebuild that took 42 minutes to complete.
How the Table Lock Happens
Magento's legacy indexing mode, "Update on Save", can cause this when traffic is high.
When you reindex a flat catalog or category association table, Magento creates a temporary shadow table (such as catalog_category_product_index_replica), populates it with millions of denormalized rows, and executes an atomic table swap:
RENAME TABLE catalog_category_product_index TO catalog_category_product_index_old,
catalog_category_product_index_replica TO catalog_category_product_index;
In MySQL, RENAME TABLE requires an exclusive metadata lock (MDL_EXCLUSIVE).
If a customer checkout query has been executing for even 8 milliseconds on catalog_category_product_index, the RENAME TABLE statement pauses and waits for that read lock to release. The part that hurt us: MySQL queues later read requests behind the pending DDL statement.
So every incoming customer query on the storefront waited behind the pending RENAME TABLE. Within about 15 seconds on that store, the MySQL connection pool of 250 connections was exhausted, PHP-FPM workers filled up, and Nginx started returning 504 Gateway Timeout errors.
In my experience, full catalog reindexes are best kept away from peak traffic hours.
What Helped: Mview Changelog Indexing
What helped on that project was switching every indexer from "Update on Save" to "Update by Schedule", which uses Magento's materialized view (Mview) changelog engine.
Instead of rebuilding entire tables across 85,000 SKUs, Mview installs MySQL row-level triggers on your catalog tables:
-- MySQL trigger installed by Magento Mview
CREATE TRIGGER trg_catalog_product_entity_after_update
AFTER UPDATE ON catalog_product_entity
FOR EACH ROW
BEGIN
INSERT IGNORE INTO catalog_product_category_cl (version_id, entity_id)
VALUES (NEW.row_id, NEW.entity_id);
END;
When a product updates, the trigger appends a single integer ID into a lightweight changelog table (_cl). A background cron worker reads only the modified entity IDs in batches of 500 rows, updates the index tables incrementally, and clears the processed version IDs:
$ bin/magento indexer:set-mode schedule
$ bin/magento cron:run --group=index
It might not fit every store. Scheduled mode relies on cron running reliably and on the triggers being in place, so it is worth checking both before you switch.
What We Saw Afterwards
After we moved that client to scheduled changelog indexing and stopped running full reindexes during the day:
- Catalog Reindexing Duration: Dropped from 42 minutes down to roughly 120 ms per incremental batch.
- Lock Wait Time: Dropped from 18,000 ms timeouts down to about 0.4 ms.
- Peak Checkout Capacity: Held at around 380 req/s through the peak flash sales, with no dropped transactions that I know of.
Those numbers are from that one store and its setup, so treat them as an example, not a benchmark. If checkouts hang during a reindex on your Magento store, looking at MySQL lock queues is a reasonable first step.