Magento 2 Performance Optimization for Enterprise Scale
A detailed guide to optimising Magento 2: full-page cache with Varnish, Redis sessions, Elasticsearch tuning, database query optimisation, and CDN integration.
Magento 2 is a powerful e-commerce platform, but it demands careful configuration to reach peak performance. For stores handling thousands of SKUs and tens of thousands of daily orders, page load speed directly impacts conversion rate and revenue. The Ventra Rocket team has helped multiple clients reduce Magento load times from 8–10 seconds down to under 2 seconds.
1. Full-Page Cache with Varnish
Varnish Cache is the single most impactful optimisation step for Magento. Properly configured, Varnish can serve thousands of requests per second without touching PHP.
# /etc/varnish/default.vcl
sub vcl_recv {
# Bypass cache for admin and checkout
if (req.url ~ "^/admin" || req.url ~ "^/checkout") {
return (pass);
}
# Long-term cache for static assets
if (req.url ~ "\.(css|js|png|jpg|gif|svg|woff2)$") {
unset req.http.Cookie;
return (hash);
}
}
sub vcl_backend_response {
# Cache product pages for 1 hour
if (bereq.url ~ "^/catalog/product") {
set beresp.ttl = 1h;
set beresp.grace = 15m;
}
}
Enable Varnish in Magento Admin: Stores → Configuration → Advanced → System → Full Page Cache.
2. Redis for Session and Cache Backend
Replace file-based cache with Redis to reduce disk I/O and accelerate session read/write operations.
// app/etc/env.php
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'database' => '2',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'max_concurrency' => '6',
'break_after_frontend' => '5',
]
],
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379',
'compress_data' => '1',
]
],
'page_cache' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0',
]
]
]
],
3. Elasticsearch Catalog Search Tuning
Magento 2.4+ requires Elasticsearch. Proper configuration significantly improves search speed and relevancy.
Optimise Index Settings
{
"index": {
"number_of_shards": 5,
"number_of_replicas": 1,
"refresh_interval": "30s",
"max_result_window": 100000
}
}
Switch to Schedule Reindex for Production
bin/magento indexer:set-mode schedule catalog_product_price
bin/magento indexer:set-mode schedule cataloginventory_stock
bin/magento indexer:set-mode schedule catalog_product_flat
bin/magento indexer:set-mode schedule catalog_category_flat
4. Database Query Optimisation
Enable Query Cache and Slow Query Log
# /etc/mysql/conf.d/magento.cnf
[mysqld]
innodb_buffer_pool_size = 4G
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 2
query_cache_type = 1
query_cache_size = 128M
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
Truncate Log Tables Regularly
-- Log tables accumulate rapidly and degrade query performance
TRUNCATE TABLE report_event;
TRUNCATE TABLE report_viewed_product_index;
TRUNCATE TABLE customer_log;
TRUNCATE TABLE customer_visitor;
5. Static Content and Image Optimisation
# Deploy static content with parallel workers
bin/magento setup:static-content:deploy -f --jobs 4 en_US vi_VN
# Batch-optimise JPEG images
find pub/media/catalog/product -name "*.jpg" | \
xargs -P 4 -I{} jpegoptim --max=85 --strip-all {}
6. CDN Integration
Serving static assets via CDN significantly reduces latency for geographically distributed users.
bin/magento config:set web/unsecure/base_static_url \
"https://cdn.yourdomain.com/pub/static/"
bin/magento config:set web/unsecure/base_media_url \
"https://cdn.yourdomain.com/pub/media/"
Real-World Results
After applying all of the above optimisations, one Ventra Rocket client achieved:
- TTFB: 1.8s → 0.18s
- Page Load Time: 9.2s → 1.7s
- Lighthouse Performance Score: 28 → 89
- Conversion Rate: +32%
Each Magento project has unique characteristics. Contact Ventra Rocket for a free audit and an optimisation plan tailored to your system.