Redis Single Server Optimization Analyzer
| Analyzer ID | Category | Severity | Time To Fix |
|---|---|---|---|
redis-single-server-optimization | ⚡ Performance | Medium | 15 minutes |
What This Checks
When Redis runs on the same server as your Laravel application, this analyzer suggests using Unix sockets instead of TCP for measurably better performance.
Why It Matters
- Performance: Unix sockets are ~10-20% faster than TCP for local connections
- Overhead Reduction: Eliminates TCP/IP stack overhead (no network layer processing)
- Lower Latency: Reduces context switches between kernel and userspace
- Resource Efficiency: Fewer file descriptors and less memory usage
When Redis is on the same host, using TCP adds unnecessary overhead. Unix sockets provide direct inter-process communication without network stack involvement.
How to Fix
Quick Fix (5 minutes)
If you're aware Redis is local and want to keep TCP for now, this is informational. No immediate action required. TCP works correctly, just with slightly more overhead.
Proper Fix (15 minutes)
1. Configure Redis to Listen on Socket
Edit /etc/redis/redis.conf:
# Enable Unix socket
unixsocket /var/run/redis/redis.sock
unixsocketperm 770
# Optionally disable TCP (if only local access needed)
# port 0
# bind 127.0.0.1Restart Redis:
sudo systemctl restart redisVerify socket exists:
ls -la /var/run/redis/redis.sock2. Update Laravel Configuration
In config/database.php:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_SOCKET', '/var/run/redis/redis.sock'),
'database' => env('REDIS_DB', 0),
'password' => env('REDIS_PASSWORD'),
],
'cache' => [
'scheme' => 'unix',
'path' => env('REDIS_SOCKET', '/var/run/redis/redis.sock'),
'database' => env('REDIS_CACHE_DB', 1),
'password' => env('REDIS_PASSWORD'),
],
],In .env:
REDIS_SOCKET=/var/run/redis/redis.sock
REDIS_PASSWORD=nullCommon Socket Paths
| Distribution | Default Socket Path |
|---|---|
| Ubuntu/Debian | /var/run/redis/redis.sock |
| CentOS/RHEL | /var/run/redis/redis.sock |
| macOS (Homebrew) | /tmp/redis.sock |
| Docker | /var/run/redis/redis.sock (mount volume) |
Docker Configuration
docker-compose.yml:
services:
redis:
image: redis:7-alpine
command: redis-server --unixsocket /var/run/redis/redis.sock --unixsocketperm 770
volumes:
- redis-socket:/var/run/redis
app:
build: .
volumes:
- redis-socket:/var/run/redis
volumes:
redis-socket:Laravel .env for Docker:
REDIS_SOCKET=/var/run/redis/redis.sockPermissions
Ensure your PHP user can access the socket:
# Add www-data to redis group
sudo usermod -a -G redis www-data
# Or set socket permissions
sudo chmod 770 /var/run/redis/redis.sock
sudo chown redis:www-data /var/run/redis/redis.sockWhen Not to Use Sockets
- Remote Redis: When Redis is on a different server
- Redis Cluster: Clusters require TCP connections
- Sentinel/Replication: High-availability setups typically use TCP
- Elasticache/Managed Redis: Cloud services only offer TCP
Verification
# Test connection via socket
redis-cli -s /var/run/redis/redis.sock ping
# In Laravel
php artisan tinker
>>> Redis::ping()
=> "PONG"Performance Comparison
Illustrative figures from redis-benchmark on a typical Linux server:
| Connection Type | Requests/sec | Latency (avg) |
|---|---|---|
| TCP localhost | ~100,000 | 0.15ms |
| Unix socket | ~120,000 | 0.12ms |
| Improvement | ~20% | ~20% |
Actual results vary by hardware, Redis version, and workload. Run redis-benchmark -s /var/run/redis/redis.sock vs redis-benchmark -h 127.0.0.1 to measure your own environment.
ShieldCI Configuration
This analyzer is automatically skipped in CI environments ($runInCI = false) and only runs in production and staging environments.
Why skip in CI and development?
- Redis server configuration is not applicable in CI
- Local/Development/Testing environments may use TCP connections, which is acceptable
- Production and staging should use Unix sockets for optimal performance
Environment Detection: The analyzer checks your Laravel APP_ENV setting and only runs when it maps to production or staging. Custom environment names can be mapped in config/shieldci.php:
// config/shieldci.php
'environment_mapping' => [
'production-us' => 'production',
'production-blue' => 'production',
'staging-preview' => 'staging',
],Examples:
APP_ENV=production→ Runs (no mapping needed)APP_ENV=production-us→ Maps toproduction→ RunsAPP_ENV=local→ Skipped (not production/staging)
Additional behavior:
- Checks all Redis connections in
config/database.php - Reports higher severity for the default/primary connection
References
Related Analyzers
- MySQL Single Server Optimization - Similar optimization for MySQL
- Cache Driver Analyzer - Ensures optimal cache driver
- Session Driver Analyzer - Ensures optimal session driver