Tối ưu hiệu suất Magento 2 cho doanh nghiệp quy mô lớn
Hướng dẫn chi tiết tối ưu Magento 2: full-page cache, Elasticsearch, Redis, CDN và database query optimization để đạt tốc độ tải trang dưới 2 giây.
Magento 2 là nền tảng e-commerce mạnh mẽ nhưng đòi hỏi cấu hình kỹ càng để đạt hiệu suất tối ưu. Với sàn thương mại điện tử xử lý hàng nghìn SKU và hàng chục nghìn đơn hàng mỗi ngày, tốc độ tải trang ảnh hưởng trực tiếp đến conversion rate và doanh thu. Đội ngũ Ventra Rocket đã giúp nhiều khách hàng cải thiện hiệu suất Magento từ 8-10 giây xuống dưới 2 giây.
1. Full-Page Cache với Varnish
Varnish Cache là bước đầu tiên và hiệu quả nhất để tăng tốc Magento. Với cấu hình đúng, Varnish có thể phục vụ hàng nghìn request/giây mà không chạm đến PHP.
# /etc/varnish/default.vcl
sub vcl_recv {
# Bỏ qua cache cho admin và checkout
if (req.url ~ "^/admin" || req.url ~ "^/checkout") {
return (pass);
}
# Cache static assets dài hạn
if (req.url ~ "\.(css|js|png|jpg|gif|svg|woff2)$") {
unset req.http.Cookie;
return (hash);
}
}
sub vcl_backend_response {
# Cache product pages 1 giờ
if (bereq.url ~ "^/catalog/product") {
set beresp.ttl = 1h;
set beresp.grace = 15m;
}
}
Kích hoạt Varnish trong Magento Admin: Stores → Configuration → Advanced → System → Full Page Cache.
2. Redis cho Session và Cache Backend
Thay thế file-based cache bằng Redis để giảm I/O disk và tăng tốc độ đọc/ghi session.
// app/etc/env.php
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
]
],
'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 cho Catalog Search
Magento 2.4+ yêu cầu Elasticsearch. Cấu hình đúng cải thiện đáng kể tốc độ tìm kiếm và relevancy.
Tối ưu Index Settings
{
"index": {
"number_of_shards": 5,
"number_of_replicas": 1,
"refresh_interval": "30s",
"max_result_window": 100000
}
}
Tắt reindex real-time cho production
# Chuyển sang schedule reindex thay vì real-time
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 Optimization
Bật Query Cache và 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
Xoá Log Tables định kỳ
-- Các bảng log tích lũy nhanh, ảnh hưởng query performance
TRUNCATE TABLE report_event;
TRUNCATE TABLE report_viewed_product_index;
TRUNCATE TABLE customer_log;
TRUNCATE TABLE customer_visitor;
-- Setup cron job dọn dẹp hàng tuần
5. Static Content và Image Optimization
# Deploy static content với parallel processing
bin/magento setup:static-content:deploy -f --jobs 4 vi_VN en_US
# Tối ưu images hàng loạt
find pub/media/catalog/product -name "*.jpg" | \
xargs -P 4 -I{} jpegoptim --max=85 --strip-all {}
# Enable WebP conversion (Magento 2.4.4+)
bin/magento config:set dev/static/sign 1
6. CDN Integration
Phân phối static assets qua CDN giảm latency đáng kể cho người dùng ở xa server.
# Cấu hình CDN base URL
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/"
Kết quả thực tế
Sau khi áp dụng đầy đủ các tối ưu trên, một khách hàng của Ventra Rocket đạt:
- Time to First Byte (TTFB): từ 1.8s → 0.18s
- Page Load Time: từ 9.2s → 1.7s
- Lighthouse Performance Score: từ 28 → 89
- Conversion Rate: tăng 32%
Mỗi dự án Magento có đặc thù riêng. Liên hệ Ventra Rocket để được audit miễn phí và plan tối ưu phù hợp với hệ thống của bạn.