Magento Performance Optimization — FPC, CDN, DB and Profiling
In this tutorial, you'll learn how to optimize Magento performance through Full Page Cache tuning, CDN integration, database query optimization, PHP profiling, and systematic performance testing.
What You'll Learn
- How to tune Full Page Cache with Varnish for 90%+ hit rates
- How to integrate CDNs like Cloudflare and Fastly for static assets
- How to optimize MySQL queries and table structures
- How to use profiling tools like Blackfire and XHProf
- How to measure and test performance improvements
Why It Matters
Page load time directly impacts revenue. A 100ms delay in load time can reduce conversion rates by 7%. Magento stores with poor performance lose customers to faster competitors. Performance optimization is not optional — it is a business requirement.
Real-World Use
An online furniture store with 50,000 SKUs was experiencing 6-second page loads. Analysis revealed that Varnish hit rate was only 40%, images were not optimized, and MySQL slow query log showed hundreds of unoptimized queries. After tuning Varnish, enabling CDN with image optimization, and indexing the slow queries, page load time dropped to 800ms and revenue increased by 15% over the next quarter.
Learning Path
flowchart LR
A[Caching] --> B[Indexing]
B --> C[Performance Optimization]
C --> D[Import & Export]
C --> E[Deployment]
D --> F[Maintenance & Upgrades]
E --> F
style C fill:#3b82f6,color:#fff
FPC Optimization
Full Page Cache with Varnish is the single most impactful performance improvement you can make.
Achieving 90%+ Hit Rate
A hit rate above 90% means Varnish serves most requests from memory without reaching Magento.
# Check current hit rate
varnishstat -1 | grep hit_rate
If your hit rate is below 90%, check:
- Are dynamic blocks (cart, compare, wishlist) excluded from cache?
- Is the cache TTL (time-to-live) too short?
- Are anonymous users getting cached pages?
- Is cache warming configured after deployment?
Cache TTL Settings
Configure cache TTL in Stores > Configuration > Advanced > System > Full Page Cache > TTL for public content. Default is 86400 seconds (24 hours). Adjust based on how often your product data changes.
Exclude Dynamic Blocks
Blocks that show user-specific content (mini cart, customer name) must be excluded from Full Page Cache. Magento handles this with cacheable="false" in layout XML:
<referenceBlock name="minicart">
<arguments>
<argument name="cacheable" xsi:type="boolean">false</argument>
</arguments>
</referenceBlock>
CDN Integration
A Content Delivery Network serves static assets from servers close to the visitor.
Cloudflare
Cloudflare is a popular CDN for Magento stores:
- Add your domain to Cloudflare
- Update nameservers
- Enable caching for static assets under Speed > Optimization
- Enable Automatic HTTPS Rewrites
- Configure page rules to cache anonymous requests
Fastly
Fastly is the default CDN for Adobe Commerce Cloud:
# Install Fastly module
composer require fastly/magento2
bin/magento setup:upgrade
bin/magento setup:di:compile
Configure in Stores > Configuration > Advanced > System > Full Page Cache > Fastly CDN.
Image Optimization
Serve images in WebP format with responsive sizes:
# Nginx configuration for WebP support
location ~* \.(jpg|jpeg|png|gif)$ {
expires 30d;
add_header Cache-Control "public, immutable";
try_files $uri $uri.webp $uri/ =404;
}
Enable lazy loading for images:
<img src="product.jpg" loading="lazy" alt="Product image" />
Database Optimization
MySQL is often the bottleneck in Magento performance.
Slow Query Log
Enable the slow query log to identify problematic queries:
# MySQL my.cnf
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow-query.log
long_query_time = 2
Analyze slow queries:
mysqldumpslow /var/log/mysql/slow-query.log
Table Optimization
Run table optimization periodically:
mysqlcheck -o --all-databases
Clean Log Tables
Magento accumulates log data that slows the database:
-- Check log table sizes
SELECT TABLE_NAME, ROUND(DATA_LENGTH/1024/1024, 2) AS size_mb
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE '%log%' OR TABLE_NAME LIKE '%report%'
ORDER BY size_mb DESC;
Clean logs from CLI:
bin/magento maintenance:enable
bin/magento cron:run --group=default
bin/magento cron:run --group=cleanup
bin/magento maintenance:disable
Partition Large Tables
Tables like sales_order and sales_order_item grow large on busy stores. Consider Partitioning them by date.
PHP Tuning
Optimize PHP configuration for Magento's requirements:
; php.ini for Magento
memory_limit = 1024M
max_execution_time = 180
max_input_time = 180
upload_max_filesize = 32M
post_max_size = 64M
opcache.enable = 1
opcache.memory_consumption = 512
opcache.max_accelerated_files = 100000
opcache.revalidate_freq = 60
realpath_cache_size = 4096K
realpath_cache_ttl = 600
OPcache
OPcache stores compiled PHP scripts in shared memory, eliminating the need to parse them on every request. For Magento, allocate at least 512MB of OPcache memory.
Profiling Tools
Profiling helps identify specific code paths that slow down the application.
Enable Magento Profiler
Add to app/etc/di.xml:
<type name="Magento\Framework\Profiler\Driver\Standard">
<arguments>
<argument name="output" xsi:type="array">
<item name="outputType" xsi:type="const">Magento\Framework\Profiler\Driver\Standard\Output\Firebug::OUTPUT_FULL</item>
</argument>
</arguments>
</type>
Blackfire
Blackfire.io is a PHP profiling tool with Magento-specific support. It shows:
- Time spent in each function call
- Database query count and duration
- Memory allocation
- I/O operations
XDebug Profiling
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = /var/log/xdebug
Generate a cachegrind file and analyze with QCacheGrind or KCacheGrind.
Performance Testing
Measure before and after each optimization to verify improvements.
Apache Bench
# Test 1000 requests with 10 concurrent users
ab -n 1000 -c 10 https://mystore.com/
Siege
siege -c 50 -t 60s https://mystore.com/
Magento Performance Toolkit
Adobe provides a performance testing toolkit:
composer create-project --repository=https://repo.magento.com magento/magento-performance-toolkit
php gen.php --command=generate --config=config.yaml
Common Mistakes
- Enabling Varnish but not excluding dynamic blocks, causing cached pages to show incorrect cart data and customer names
- Using a CDN for HTML pages without configuring cache rules, resulting in stale content being served to visitors
- Running
mysqlcheck -oduring business hours, locking tables and causing storefront errors - Setting OPcache memory too low for a large Magento installation, causing frequent cache misses and PHP script recompilation
- Not measuring performance before and after changes, making it impossible to know if optimizations actually helped
Practice Questions
- What is the minimum Varnish hit rate you should target for a production Magento store?
- Why should dynamic blocks like the mini cart be excluded from Full Page Cache?
- What OPcache memory value is recommended for Magento?
Challenge: Set up a performance monitoring script that captures Varnish hit rate, MySQL slow query count, PHP OPcache hit rate, and page load time every hour, then graphs the results.
FAQ
Mini Project
Conduct a full performance audit of a Magento store: enable Varnish and check hit rate, analyze MySQL slow query log and optimize the top 5 slowest queries, configure PHP OPcache with 512MB memory, enable a CDN for static assets, and document the before/after page load times using Chrome DevToolsk "DevTools" >}} Lighthouse report.
What's Next
Now that performance is optimized, learn about Magento Import and Export to manage product data efficiently. Then continue with Magento Deployment for CI/CD and pipeline strategies.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro