1.12

Describe the Magento Caching System

Understanding Magento's caching system is crucial for performance optimization and proper application functioning.

Why This Matters: The Magento caching system is crucial for performance and proper functioning. Understanding how to manage different cache types is essential for development and troubleshooting.

Caching System Overview

mindmap root((Magento Cache)) Architecture Zend_Cache Frontend Adapter Backend Storage What is Cached Configuration XML HTML Output Schema Info Attributes/Entities Cache Types config layout block_html full_page collections db_ddl Management Tools bin/magento CLI Admin Panel cache:clean cache:flush Storage Options File System Database Redis

Magento Caching System Architecture

The Magento caching system is based on the Zend_Cache component and consists of two critical pieces:

Cache Frontend

Magento\Framework\Cache\Frontend\Adapter\Zend

Provides an interface for developers to work with the cache.

Purpose: Standardized API for cache operations (get, set, delete, etc.)

Cache Backend

Magento/Framework/Cache/Backend/

Defines how exactly cache is stored (file system, Redis, database, etc.).

Purpose: Actual storage implementation and retrieval mechanism

What Does Magento Cache?

The most important elements that are cached by Magento:

Configuration XML Files

  • layout.xml
  • config.xml
  • ui_components.xml
  • System configuration

HTML Output

  • Block output
  • Complete pages (FPC)
  • Rendered content

Schema Information

  • Table structures
  • Column names and types
  • Database relationships

Internal Operations

  • Attribute metadata
  • Entity information
  • System configuration

Cache Configuration

etc/cache.xml

Cache configuration is stored in module-specific etc/cache.xml files.

Key cache.xml files in Magento 2.4:
  • module-eav/etc/cache.xml
  • module-translation/etc/cache.xml
  • module-customer/etc/cache.xml
  • module-webapi/etc/cache.xml
  • module-page-cache/etc/cache.xml
  • module-store/etc/cache.xml
  • module-integration/etc/cache.xml

Important Cache Types

config - Magento Configuration

Stores configuration from XML files along with entries in the core_config_data table.

When to refresh:
  • Adding system configuration entries (/etc/adminhtml/system.xml)
  • Making XML configuration modifications
  • Changing values in Admin → Stores → Configuration

Command: bin/magento cache:flush config

layout - Layout XML Updates

With Magento's extensive layout configuration, a lot of CPU cycles are used in combining and building these rules.

When to refresh:
  • Making changes to app/design/[area]/layout/ folders
  • Modifying app/code/*/view/[area]/layout/ files
Development Tip: Usually disabled for frontend development

Command: bin/magento cache:flush layout

block_html - Block HTML Output

Output from the toHtml() method on a block. Obtaining HTML from a block can be expensive, so caching allows this output to be reused.

Benefits: HTML output can be reused in other locations or pages
Development Tip: Usually disabled for frontend development

collections - Database Query Results

Stores multi-row results from database queries.

This cache helps reduce database load by storing frequently accessed collections.

db_ddl - Database Table Structure

Stores database schema information including table structure.

vendor/magento/framework/DB/Adapter/Pdo/Mysql.php
When to refresh:
  • After running setup:upgrade
  • When database schema changes are made

config_webservice - API Configuration

Stores configuration for REST and SOAP APIs.

When to refresh:
  • Adding methods to API service contracts
  • Modifying webapi.xml files

full_page - Full Page Cache (FPC)

The final layer of caching in a Magento application. Entire HTML page output can be stored.

Storage Options:
  • File system - Default option
  • Database - Alternative storage
  • Redis - Fastest option (recommended for production)
Development Best Practice:
  • Leave FPC OFF during frontend development
  • Turn it ON before deploying to ensure no caching issues

Cache Management Commands

View Cache Status

bin/magento cache:status

Shows a list of all cache types and their current status (enabled/disabled).

Clean Cache

bin/magento cache:clean

Cleans all cache types. This is the recommended first approach.

Specific Cache Types:
bin/magento cache:clean config layout
Cleans only the specified cache types.

Flush Cache

bin/magento cache:flush

Completely removes all cache data from cache storage.

Specific Cache Types:
bin/magento cache:flush config layout
Flushes only the specified cache types.

Enable/Disable Cache

bin/magento cache:enable [cache_types]
bin/magento cache:disable [cache_types]

Enable or disable specific cache types.

Example:
bin/magento cache:disable layout block_html

Cache:Clean vs Cache:Flush

cache:clean (Recommended)

Cleans only Magento cache tags without affecting other applications that might share the same cache storage.

Use when:
  • Making configuration changes
  • Updating layout files
  • As the first troubleshooting step

cache:flush

Completely purges all data from cache storage. More aggressive approach.

Use when:
  • cache:clean doesn't solve the issue
  • You need a complete cache reset
  • Troubleshooting persistent cache issues

Magento's Recommendation

Run cache:clean operations first. If this doesn't solve the problem, then flush the cache storage with cache:flush.

Manual Cache Management

File System Cache

rm -rf var/cache/*
rm -rf var/page_cache/*

Manually delete cache files from the file system.

Redis Cache

redis-cli
SELECT [database_index]
FLUSHDB

Connect to Redis and flush the specific database.

⚠️ Important: Sessions and content caching should never share the same Redis database. Ideally, use separate Redis instances.

Cache Storage Types

Storage Type Performance Use Case Configuration
File System ⭐⭐ Moderate Development, small sites Default (var/cache/)
Database ⭐ Slow Shared hosting limitations app/etc/env.php
Redis ⭐⭐⭐⭐⭐ Fastest Production (recommended) app/etc/env.php

Caching Strategy in Different Environments

Development Environment

Commonly Disabled Caches:
  • layout
  • block_html
  • full_page

Reason: Faster iteration and immediate feedback on changes

Production Environment

All Caches Enabled
  • Use Redis for best performance
  • Enable Full Page Cache
  • Configure proper cache warmup

Reason: Maximum performance and optimal user experience

Server Caching vs Browser Caching

Magento includes two means of caching:

Server Caching

  • All cache types discussed above
  • Stored on server (Redis, file system, DB)
  • Managed via CLI or Admin

Browser Caching

  • Static resources (CSS, JS, images)
  • Controlled via HTTP headers
  • Managed by web server configuration

Quick Reference: Cache Types

Cache Type Identifier What's Cached When to Flush
Configuration config XML configs + core_config_data XML or admin config changes
Layouts layout Layout XML files Layout file modifications
Block HTML block_html Block toHtml() output Template or block changes
Collections collections Database query results Data model changes
DDL db_ddl Database schema After setup:upgrade
Web Services config_webservice REST/SOAP API config API service contract changes
Full Page full_page Complete HTML pages Any frontend changes
Translations translate Translation dictionaries Translation file changes
EAV eav EAV attribute metadata Attribute modifications

Exam Tips

Key Points to Remember

  • Magento caching is based on Zend_Cache component
  • Two main pieces: Frontend (interface) and Backend (storage)
  • cache:clean is recommended first, then cache:flush if needed
  • Know the purpose of each major cache type (config, layout, block_html, full_page)
  • Redis is the recommended cache backend for production
  • Full Page Cache should be disabled during frontend development
  • Cache configuration is stored in etc/cache.xml files
  • Manual cache clearing: rm -rf var/cache/* or Redis FLUSHDB
  • The config cache stores both XML and database configuration
  • Sessions and cache should use separate Redis instances/databases

Further Reading