Where a Magento 2 shop is actually slow: a top-to-bottom checklist
15 min read
Part 1 of 1 in Fast Magento
“The shop is slow” is the most common message I get from a client. It is also the least useful one. Slow where? For whom? Since when? Magento 2 has about six places where seconds can hide, and they need different tools. This is the list I go through, always in the same order, before I change a single line of code.
The order matters. The first two steps fix most shops I see. The last two are where developers usually start, and that is why performance work so often takes weeks instead of days.
0. Measure the right page
Developers test the homepage because that is what opens when you type the URL. Customers barely see it. Most sessions in a mid-size shop start on a category or a product page from a Google result or an ad, and the moments where people lose patience are “add to cart” and “place order”.
So before anything else I take four URLs and measure each one the same way:
- a category page with filters
- a product page (ideally a configurable one with many options)
- the cart after adding one product
- the first checkout step
For each I want three numbers: time to first byte, whether the page came from cache, and how many SQL queries the page needed. The first two come from one curl:
curl -s -o /dev/null -w 'TTFB: %{time_starttransfer}s total: %{time_total}s\n' \
https://shop.example/men/jackets.html
Run it three times. The first request may be a cache miss, the next two should be fast. If all three are slow, the page is not cached at all. If the first is slow and the others are fast, the cache works, and the question becomes how often real visitors are the “first”.
Time to first byte, or TTFB, is the time between sending the request and receiving the first byte of the answer. It is the number that tells you how long the server was thinking. Everything after it is downloading, and that is a frontend topic.
1. Full-page cache
Full-page cache means the finished HTML of a page is stored and served again without running PHP. In Magento 2 that store is Varnish, a separate program that sits between the visitor and the web server. A cached page answers in a few milliseconds. An uncached one runs the whole Magento stack: hundreds of PHP classes, dozens of SQL queries, often half a second or more.
This is why the cache hit rate is the single most important number in the whole checklist. A shop with a 95% hit rate can survive slow PHP. A shop with 40% cannot be saved by any PHP optimisation.
How to check the hit rate
Production
On the Varnish server, varnishstat keeps running counters:
varnishstat -1 | grep -E 'MAIN\.cache_(hit|miss|hitpass) 'Divide hits by the sum of all three. Below 80% on a catalog-heavy shop is a problem. To see which URLs miss, log requests with their handling:
varnishncsa -F '%{Varnish:handling}x %s %r' | grep -v '^hit'Developer mode
When Magento runs in developer mode it adds an X-Magento-Cache-Debug header and Varnish passes it through:
curl -sI https://shop.test/men/jackets.html | grep -i x-magento-cache-debug
# X-Magento-Cache-Debug: HITNever switch production to developer mode for this. Use the Varnish tools instead.
What kills the cache
Almost always one of these four:
| Symptom | Usual cause | Fix |
|---|---|---|
| Every category page is a MISS | A block in the layout is marked cacheable="false", which makes the whole page uncacheable |
Find it with the grep below, move the dynamic part to a customer-data section or an AJAX call |
| Pages are cached but expire within minutes | A custom module cleans cache tags too broadly, for example on every order or cron run | Narrow the tags: invalidate one product, not catalog_product |
| Logged-in customers always MISS | Cookies or a custom cookie that Varnish uses as part of the cache key | Check vcl_hash in the VCL, strip cookies that do not affect the page |
| Same page cached many times | Marketing URL parameters (utm_*, gclid, fbclid) make every click a new cache entry |
Strip them in vcl_recv |
The grep for the first and worst case:
grep -rn 'cacheable="false"' app/code app/design vendor --include='*.xml' \
| grep -v -E 'checkout|customer|sales|paypal'
Those excluded modules legitimately mark their pages as uncacheable (cart, account, checkout). Anything else in the list deserves a question: why is a block on the catalog page not cacheable?
2. Cart and checkout
The cart and the checkout are never cached. Every click runs PHP, and every change to the cart makes Magento recalculate totals: subtotal, discounts, tax, shipping. That recalculation is called collectTotals, and it is the most expensive routine in the whole shop.
The problem is rarely the core. It is what third-party and custom modules attach to it. A plugin that checks stock in an external system on every quote save. A shipping method that calls a carrier API while the customer is still typing the postcode. A loyalty module that recalculates points for the whole cart on every item change.
How I find it:
-
Enable the built-in profiler on a staging copy with production data. It prints a tree of timers at the bottom of every page.
bin/magento dev:profiler:enable html -
Add one product to the cart, open the cart page, then the first checkout step. Look at the profiler output for
collectTotalsand count how many times it appears per request. One or two is normal. Five or more means something is triggering the quote to be saved repeatedly. -
List who is attached to the quote. Plugins on
Magento\Quote\Model\QuoteRepository::saveand on the totals collector are the usual suspects.grep -rn -E 'Quote\\Model\\(QuoteRepository|Quote\\TotalsCollector)' \ app/code vendor --include=di.xml -
Check customer-data sections. These are the small JSON blocks (mini cart, customer name, messages) the browser reloads after certain actions. A badly written
sections.xmlcan make the browser reload all sections after every click, which in turn triggers a full quote load each time. Open the browser network tab, click “add to cart” and count the requests tocustomer/section/load. -
Disable the profiler when done. It costs time itself.
bin/magento dev:profiler:disable
3. Database and indexers
If the cache is healthy and the checkout is clean but uncached pages are still slow, the next stop is MySQL. Two tools, both free.
Slow query log
There are two loggers: the one in MySQL, and Magento’s own. MySQL’s covers everything that touches the database, including cron and the API. Magento’s only sees the requests you make, but it writes the PHP call stack next to each query, so you immediately know which module asked for it. I usually start with Magento’s on a single slow page, then leave MySQL’s on for a day.
Magento logger
Log only queries slower than one second, with the call stack, into var/debug/db.log:
bin/magento dev:query-log:enable \
--include-all-queries=false \
--query-time-threshold=1 \
--include-call-stack=trueOpen the slow page a couple of times, read the log, then switch it off. It slows every request while it is on and is meant for staging:
bin/magento dev:query-log:disableSet --query-time-threshold=0.1 and --include-all-queries=true when you want to count queries per page rather than find the slow ones. A category page with 400 queries is a problem even if each one is fast.
MySQL slow log
MySQL can write every query that takes longer than a threshold to a file. One second is a good first threshold; on a shop that should be rare.
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 1;
SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log';Leave it on for a day of normal traffic, then summarise:
mysqldumpslow -s t -t 20 /var/log/mysql/slow.logWhat to look for
Three patterns cover most of what I find:
- Layered navigation on big catalogs. The filter counts on category pages join EAV tables (Magento stores product attributes in several tables, which is flexible and slow). From roughly 50,000 products this needs Elasticsearch or OpenSearch doing the work, not MySQL.
- Custom reports or exports running
SELECT *onsales_orderwithout a date range. - Queries without an index from custom modules.
EXPLAINthe query; if you seetype: ALLon a table with a million rows, add the index.
Indexers
Indexers build the flat, fast tables (prices, stock, category products, search) out of the slow EAV ones. They can run in two modes. “Update on save” rebuilds the index inside the request that changed the data, so an import of 500 products or a price update from the ERP makes the admin, the API and sometimes the frontend wait. “Update by schedule” records the change and lets cron rebuild in the background.
bin/magento indexer:show-mode
bin/magento indexer:set-mode schedule
bin/magento indexer:status
On any shop with an ERP, a feed or a PIM pushing data, all indexers should be on schedule. The status command also shows the backlog: if an indexer has thousands of pending entries for hours, cron is not keeping up, which brings us to the last check in this section.
Cron
Magento’s cron runs the indexers, sends emails, cleans caches and processes queues. When it falls behind, the symptoms look like slow indexing, missing emails and stale prices. The table cron_schedule tells the story:
SELECT status, COUNT(*) FROM cron_schedule GROUP BY status;
SELECT job_code, COUNT(*) AS pending
FROM cron_schedule
WHERE status = 'pending'
GROUP BY job_code
ORDER BY pending DESC
LIMIT 10;
Thousands of pending rows for one job code means that job is either too slow or crashing. Thousands of missed rows means cron is not running often enough or two cron processes are fighting over the same jobs.
4. PHP code
Only now the profiler. Not because the code is never the problem, but because profiling without the context from the previous steps produces a long list of “slow” functions and no idea which one matters.
I use Xdebug’s profiler on the staging copy. It writes a file per request that shows every function call with its own time and the time of everything it called.
; php.ini on staging only
xdebug.mode = profile
xdebug.start_with_request = trigger
xdebug.output_dir = /tmp/xdebug
With trigger the profiler stays off until you add XDEBUG_TRIGGER=1 as a cookie or GET parameter, so you can profile one page and not the whole staging site. The output opens in PhpStorm, QCachegrind or KCachegrind. Sort by “self time” and read the top ten.
What I typically find there:
- Repository calls inside a loop. A block lists 48 products and calls
productRepository->getById()for each one. That is 48 queries plus 48 full product loads instead of one collection. The fix is a collection with only the attributes the block uses. - Heavy constructors. A class that is injected into many places and does real work (loads config, opens a connection, builds a collection) in its constructor. Magento instantiates it even when the method using it is never called. Move the work into a lazy method or inject a factory.
ObjectManager::get()in custom code. Besides being bad practice, it bypasses the compiled dependency injection and creates objects at runtime, which is measurably slower on hot paths.- Third-party modules that are disabled in the admin but still loaded. Disabling a module in its configuration does not remove its plugins and observers. Disable it properly with
bin/magento module:disableor remove it.
5. Infrastructure
The server layer is where hosting companies like to start and where I prefer to end, because misconfiguration here is common but rarely the reason a specific page is slow. Still, these settings are cheap to check and each one is a measurable win.
| Setting | What to check | Why |
|---|---|---|
| PHP OPcache | opcache.memory_consumption ≥ 512 MB, opcache.max_accelerated_files ≥ 60000, opcache.validate_timestamps = 0 in production |
Magento has tens of thousands of PHP files; if they do not fit in OPcache, PHP recompiles them on every request |
| PHP-FPM | pm.max_children sized to available RAM, not left at the default of 5 |
Too few workers and requests queue; too many and the server swaps |
| Valkey (Redis on 2.4.8 and older) | Separate databases (or instances) for cache, full-page cache and sessions in app/etc/env.php |
File-based cache on a multi-server setup is slow and breaks invalidation. Since 2.4.9 Adobe lists Valkey instead of Redis; the configuration in env.php is the same |
| Nginx | Static files served by Nginx directly, gzip or brotli on, HTTP/2 enabled |
PHP should never serve images or JS |
| MySQL | innodb_buffer_pool_size around 70% of RAM on a dedicated database server |
If the hot data does not fit in memory, every query touches disk |
On Adobe Commerce Cloud most of this is managed for you and some of it cannot be changed. What you can still control is OPcache through the php.ini in the repository, the cache and session services through .magento/services.yaml, and the size of the plan, which is often the honest answer.
6. Frontend
This gets its own post later. The short version: once the HTML arrives quickly, the remaining time is JavaScript and images. In order of effect: remove third-party scripts you do not need (chat widgets, three analytics tools, a heatmap), enable JS bundling or move to a theme that already does it, lazy-load images below the fold, and measure Core Web Vitals on a real mid-range phone, not on a developer laptop.
The checklist
Copy this somewhere. It is the whole post in fifteen lines.
- Measure a category page, a product page, the cart and checkout step one, not the homepage
- Record TTFB for each, three runs, note whether the page came from cache
- Varnish hit rate above 80%; find the URLs that miss
- No
cacheable="false"outside cart, customer, checkout and payment modules - Cache tags invalidate single products, not the whole catalog
- Marketing URL parameters stripped in Varnish
-
collectTotalsruns once or twice per cart request, not five times - Known list of plugins on the quote repository and totals collector
- Customer-data sections reload only what changed
- Slow query log enabled on staging for a day, top 20 queries reviewed
- All indexers on “Update by schedule”, backlog near zero
-
cron_schedulewithout thousands of pending or missed rows - Profiled one slow page with Xdebug, top ten self-time functions explained
- OPcache, PHP-FPM, Valkey and MySQL buffer pool sized for the server
- Third-party frontend scripts counted and justified
What to do with the results
Fix the biggest thing first, measure again, then go to the next one. It sounds obvious, but the temptation is to fix everything you found in one branch and deploy. Then the shop is faster and nobody knows which change did it, which means nobody knows what to watch when it gets slow again in six months.
Each step above deserves a longer post. Varnish and cache tags are next, then indexers and the import pipeline, then the checkout. If there is one you want first, tell me.