silvertree / m2-maintenance-cache-backend
Cache-based maintenance mode system that uses Redis/cache backends instead of filesystem checks
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 0
Forks: 0
Open Issues: 0
Type:magento2-module
Requires
- php: >=8.2
- magento/framework: *
README
Module: Silvertree_MaintenanceCacheBackend
Version: 1.0.1
Compatibility: Magento 2.4+ / PHP 8.2+
Overview
This module replaces Magento's filesystem-based maintenance mode with a configurable cache backend system, provides better scalability for distributed environments.
Key Features
- Persistent Maintenance Mode: Survives
cache:flush
andcache:clean
operations - Multiple Backend Support: Redis, File, Memcached, or any Magento-supported cache backend
- IP Whitelist Support: Full compatibility with Magento's IP whitelist feature
- Graceful Fallback: Automatically falls back to filesystem when cache is unavailable
- Zero Breaking Changes: Maintains full compatibility with existing maintenance commands
- Performance Optimized: Dedicated cache backend
Installation
-
Install the module:
composer require silvertree/m2-maintenance-cache-backend php bin/magento module:enable Silvertree_MaintenanceCacheBackend php bin/magento setup:upgrade
-
Configure cache backend (see the Configuration section below)
-
Test the configuration:
php bin/magento maintenance:enable php bin/magento maintenance:status php bin/magento cache:flush php bin/magento maintenance:status # Should still show enabled php bin/magento maintenance:disable
Configuration
Add a cache/maintenance
section to your app/etc/env.php
file. When not configured, the module gracefully falls back
to Magento's default filesystem behavior.
Redis Backend (Recommended)
'cache' => [ 'maintenance' => [ 'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis', 'backend_options' => [ 'server' => '127.0.0.1', 'database' => '14', 'port' => '6379', 'password' => '', 'timeout' => '2.5', 'compression_threshold' => '2048', 'compression_library' => 'gzip', 'automatic_cleaning_factor' => 0 ] ] ]
Memcached Backend Example (untested).
'cache' => [ 'maintenance' => [ 'backend' => 'Magento\\Framework\\Cache\\Backend\\Memcached', 'backend_options' => [ 'servers' => [ [ 'host' => 'localhost', 'port' => 11211, 'weight' => 1 ] ], 'compression' => true, 'compatibility' => false ] ] ]
Usage
The module works transparently with all existing Magento maintenance commands:
# Enable maintenance mode php bin/magento maintenance:enable # Enable with IP whitelist php bin/magento maintenance:enable --ip=192.168.1.1 --ip=10.0.0.1 # Check status php bin/magento maintenance:status # Allow additional IPs php bin/magento maintenance:allow-ips 192.168.1.100 # Disable maintenance mode php bin/magento maintenance:disable
Architecture
Plugin System
The module uses Magento's plugin system to intercept MaintenanceMode
operations:
aroundIsOn()
: Checks cache for maintenance status and IP whitelistaroundSet()
: Sets maintenance status in cachearoundSetAddresses()
: Manages IP whitelist in cache
Cache Key Structure
- Maintenance Status:
MAINTENANCE_MODE_STATUS
- IP Addresses:
MAINTENANCE_MODE_ADDRESSES
Fallback Behavior
When cache is unavailable or not configured:
- Service operations return
false
or empty arrays - Plugin methods call
$proceed()
to use original filesystem logic - System continues operating with filesystem-based maintenance mode
- No exceptions thrown - graceful degradation
Performance Considerations
Redis Configuration
- Dedicated Database: Use a separate Redis database (14) to avoid conflicts
- Persistence: Configure Redis persistence to survive server restarts
- Memory: Maintenance data is minimal (~100 bytes per status)
- Compression: Enable compression for larger IP whitelists
File Backend
- Directory Permissions: Ensure the web server has read/write access
- Disk Space: File backend uses minimal disk space
- Performance: Slower than Redis but suitable for single-server deployments
Troubleshooting
Maintenance Mode Not Persisting
Problem: Maintenance mode is disabled after cache:flush
Solutions:
- Verify
cache/maintenance
configuration inapp/etc/env.php
- Check Redis connectivity and database configuration
- Review system logs for cache backend errors
- Test cache backend with:
php bin/magento cache:status
IP Whitelist Not Working
Problem: IP addresses are not properly whitelisted
Solutions:
- Verify IP addresses are correctly stored:
php bin/magento maintenance:status
- Check cache backend connectivity
- Review plugin logs for IP comparison issues
- Test with filesystem fallback by removing cache configuration
Cache Backend Errors
Problem: Cache backend connection failures
Solutions:
- Check Redis/Memcached service status
- Verify connection parameters (host, port, password)
- Review system logs for specific error messages
- Test connection manually with tools like
redis-cli
Performance Issues
Problem: Slow maintenance mode operations
Solutions:
- Use Redis backend for better performance
- Optimize cache backend configuration
- Enable compression for large IP whitelists
- Monitor cache backend performance metrics
Development
Custom Cache Backends
To implement a custom cache backend:
- Extend
Magento\Framework\Cache\Backend\AbstractBackend
- Configure in
cache/maintenance/backend
- Ensure compatibility with cache frontend interface
- Test fallback behavior when backend fails