Slave Connections
Leveraging slave connections for MySQL and Redis to improve performance with read-only replicas.
Slave Connections
Slave Connection Overview
What Are Slave Connections?
Slave connections enable read-only database connections that improve performance by offloading read operations from the master database.
Benefits
- Faster responses: Read operations are distributed
- Improved performance: Master database handles writes only
- Load balancing: Reduces load on primary database
Environment Variables
Two Key Variables
There are two environment variables that enable the use of read-only connections:
| Variable | Purpose | Default |
|---|---|---|
MYSQL_USE_SLAVE_CONNECTION |
Enable MySQL read-only connection | false |
REDIS_USE_SLAVE_CONNECTION |
Enable Redis read-only connection | false |
Default Behavior
Both variables default to false. You must explicitly enable them to use slave connections.
Configuration in .magento.env.yaml
Basic Configuration
Slave connections are configured in .magento.env.yaml:
stage:
deploy:
MYSQL_USE_SLAVE_CONNECTION: true
Enabling Both MySQL and Redis
stage:
deploy:
MYSQL_USE_SLAVE_CONNECTION: true
REDIS_USE_SLAVE_CONNECTION: true
Configuration Location
File: .magento.env.yaml
Section: stage → deploy
Pro Account Recommendations
Magento's Recommendation
For Pro accounts, Magento recommends enabling both Redis and MySQL slave connections:
# .magento.env.yaml
stage:
deploy:
MYSQL_USE_SLAVE_CONNECTION: true
REDIS_USE_SLAVE_CONNECTION: true
Why Enable Both?
- Performance: Maximizes read performance across all services
- Scalability: Better handles high traffic loads
- Resource optimization: Distributes database load efficiently
Redis Slave Connection Limitations
Important Restrictions
Critical Limitations for Redis
1. Not Available in Integration Environment
Redis read-only connection is NOT available in Integration environment.
2. Not Available with Custom CACHE_CONFIGURATION
Redis slave connection is NOT available if you specify custom details in the CACHE_CONFIGURATION environment variable.
When Redis Slave Connection Works
Redis slave connection is available when:
- Pro Staging: ✓ Available
- Pro Production: ✓ Available
- Integration: ✗ NOT Available
- With default Redis config: ✓ Available
- With custom CACHE_CONFIGURATION: ✗ NOT Available
MySQL Slave Connection
MySQL Configuration
stage:
deploy:
MYSQL_USE_SLAVE_CONNECTION: true
Availability
| Environment | MySQL Slave Connection |
|---|---|
| Integration | ✓ Available |
| Pro Staging | ✓ Available |
| Pro Production | ✓ Available |
No Restrictions
Unlike Redis, MySQL slave connections work in all environments without restrictions.
Complete Configuration Example
Recommended Pro Account Setup
# .magento.env.yaml
stage:
global:
# Global environment variables
SCD_ON_DEMAND: true
deploy:
# Enable slave connections for performance
MYSQL_USE_SLAVE_CONNECTION: true
REDIS_USE_SLAVE_CONNECTION: true
# Other deploy configurations
CACHE_CONFIGURATION:
_merge: true
SESSION_CONFIGURATION:
_merge: true
Important Note
If you're using custom CACHE_CONFIGURATION, remember that REDIS_USE_SLAVE_CONNECTION will NOT work!
Comparison: MySQL vs Redis Slave Connections
| Aspect | MySQL | Redis |
|---|---|---|
| Variable | MYSQL_USE_SLAVE_CONNECTION | REDIS_USE_SLAVE_CONNECTION |
| Default Value | false | false |
| Integration Environment | ✓ Works | ✗ Does NOT Work |
| Pro Staging/Production | ✓ Works | ✓ Works |
| With Custom Config | ✓ Works | ✗ NOT with CACHE_CONFIGURATION |
| Pro Recommendation | ✓ Enable | ✓ Enable |
How It Works
Read vs Write Operations
With Slave Connections Enabled
- Read operations (SELECT): Sent to slave (read-only replica)
- Write operations (INSERT, UPDATE, DELETE): Sent to master database
Performance Impact
By offloading read operations to a slave:
- Master database has more capacity for write operations
- Read operations are distributed across multiple nodes
- Overall application response time improves
- Database can handle higher traffic loads
Best Practices
Do's
- Enable both MySQL and Redis on Pro accounts
- Configure in stage → deploy section
- Test performance improvements
- Use default Redis config for slave connections
- Monitor database performance after enabling
- Enable in Staging before Production
Don'ts
- Don't expect Redis slave in Integration
- Don't use REDIS_USE_SLAVE_CONNECTION with custom CACHE_CONFIGURATION
- Don't forget both default to false
- Don't skip Pro recommendation
- Don't configure outside stage → deploy
- Don't assume it works without enabling
Troubleshooting
Redis Slave Not Working?
Check These Common Issues
- Integration environment: Redis slave is NOT available in Integration
- Custom CACHE_CONFIGURATION: Remove custom config or don't use slave connection
- Wrong configuration section: Must be in stage → deploy, not global
- Not enabled: Variable must be explicitly set to true
Exam Tips
Key Points to Remember
- Two variables: MYSQL_USE_SLAVE_CONNECTION and REDIS_USE_SLAVE_CONNECTION
- Default value: Both default to false
- Configuration file: .magento.env.yaml
- Configuration section: stage → deploy
- Purpose: Enable read-only connections to speed up responses
- Pro recommendation: Enable BOTH MySQL and Redis slave connections
- MySQL availability: Works in all environments (Integration, Staging, Production)
- Redis limitation 1: NOT available in Integration environment
- Redis limitation 2: NOT available with custom CACHE_CONFIGURATION
- Redis works in: Pro Staging and Production (with default config)
- Performance benefit: Offloads read operations from master database
- Operation split: Reads go to slave, writes go to master