For PHP web applications requiring high availability Redis storage, Redis Cluster (introduced in Redis 3+) provides automatic sharding and failover capabilities. While early versions had stability concerns, Redis 5+ has significantly improved production readiness with:
- Automatic data partitioning across multiple nodes
- Master-replica replication with failover
- Linear scalability up to 1000 nodes
When implementing Redis Cluster for PHP session storage:
// Sample PHPRedis cluster configuration
$redis = new RedisCluster(
null,
[
'redis-node1:6379',
'redis-node2:6379',
'redis-node3:6379'
],
1.5, // Timeout
1.5, // Read timeout
false, // Persistent
'auth_password'
);
// Session handler configuration
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://redis-node1:6379?auth=password,tcp://redis-node2:6379?auth=password');
Solution | Pros | Cons |
---|---|---|
Redis Sentinel | Mature, simple monitoring | No automatic sharding |
Redis Cluster | Full HA solution | Client-side complexity |
Proxy Solutions (Twemproxy) | Client-agnostic | Additional failure point |
Key recommendations for Redis in load-balanced PHP environments:
- Use pipelining for batch operations:
$redis->multi(Redis::PIPELINE) ->set('session:1', $data1) ->set('session:2', $data2) ->exec();
- Configure appropriate timeout values (typically 1-3x your average operation time)
- Monitor memory fragmentation ratio and eviction policies
Essential metrics to track in production:
- Cluster state using
redis-cli --cluster check
- Replication lag with
INFO replication
- Node health via
PING
responses
For automated failover integration with pfSense:
#!/bin/bash
# Basic health check script for pfSense
if ! redis-cli -h $REDIS_MASTER PING | grep -q PONG; then
pfctl -F states -t redis_states
# Trigger load balancer reconfiguration
fi
Redis Cluster has significantly matured since its introduction in Redis 3.0. While early versions had limitations for production use, current implementations (Redis 5+) offer robust solutions for:
- Automatic sharding across multiple nodes
- Master-replica replication with failover
- Partial resynchronization after network partitions
For PHP web applications requiring high availability, consider these proven architectures:
// Sample PHPRedis cluster configuration
$redis = new RedisCluster(
null,
[
'redis-node1:6379',
'redis-node2:6379',
'redis-node3:6379',
],
1.5, // timeout
1.5, // read_timeout
false, // persistent
'password'
);
For PHP session storage in load-balanced environments:
// Configure PHP to use Redis for sessions
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://redis-cluster:6379?auth=password&persistent=1');
// Alternative using PHPRedis
$redis = new Redis();
$redis->pconnect('redis-cluster', 6379);
$redis->auth('password');
session_set_save_handler(
[$redis, 'sessionOpen'],
[$redis, 'sessionClose'],
[$redis, 'sessionRead'],
[$redis, 'sessionWrite'],
[$redis, 'sessionDestroy'],
[$redis, 'sessionGC']
);
Benchmark your Redis cluster configuration under production-like loads:
- Test failover scenarios (kill -9 master processes)
- Measure latency during resharding operations
- Validate cross-datacenter performance if applicable
Solution | Pros | Cons |
---|---|---|
Redis Sentinel | Mature, simple setup | No automatic sharding |
Redis Cluster | Auto-sharding, modern | Complex client support |
Memcached | Simple horizontal scaling | No persistence |
Here's a Docker Compose snippet for testing Redis Cluster:
version: '3'
services:
redis-node1:
image: redis:6
command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379
redis-node2:
image: redis:6
command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6379
redis-cluster-init:
image: redis:6
command: redis-cli --cluster create redis-node1:6379 redis-node2:6379 --cluster-replicas 1
depends_on:
- redis-node1
- redis-node2
- For new deployments, use Redis 7+ Cluster with PHPRedis 5.3+
- Implement circuit breakers in your PHP application
- Monitor cluster health with Prometheus and Grafana