Skip to content
Pro Analyzer — Available with ShieldCI Pro

Redis Single Server Optimization Analyzer

Analyzer IDCategorySeverityTime To Fix
redis-single-server-optimization⚡ PerformanceMedium15 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:

ini
# 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.1

Restart Redis:

bash
sudo systemctl restart redis

Verify socket exists:

bash
ls -la /var/run/redis/redis.sock

2. Update Laravel Configuration

In config/database.php:

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:

ini
REDIS_SOCKET=/var/run/redis/redis.sock
REDIS_PASSWORD=null

Common Socket Paths

DistributionDefault 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:

yaml
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:

ini
REDIS_SOCKET=/var/run/redis/redis.sock

Permissions

Ensure your PHP user can access the socket:

bash
# 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.sock

When 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

bash
# 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 TypeRequests/secLatency (avg)
TCP localhost~100,0000.15ms
Unix socket~120,0000.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:

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 to production → Runs
  • APP_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