Back to site

Redis as application cache

Deploy Redis on a HolyCloud VPS and use it for session, object, and query caching to offload PHP and MySQL.

Redis as application cache

Redis is an in-memory key-value store, ideal for application cache, PHP sessions, and light queues. On a HolyCloud Performance VPS, it reduces pressure on MySQL and PHP-FPM.

Prerequisites

  • VPS with 1 GB+ free RAM for Redis (adjust maxmemory)
  • Compatible application (Laravel, Symfony, WordPress with plugin, PrestaShop, etc.)
  • sudo access
  • Firewall: port 6379 local only unless you run a dedicated cluster

Install Redis

sudo apt update
sudo apt install -y redis-server
redis-cli ping

Configuration /etc/redis/redis.conf:

bind 127.0.0.1 ::1
protected-mode yes
maxmemory 512mb
maxmemory-policy allkeys-lru
appendonly yes
sudo systemctl enable --now redis-server

Minimum security

# strong password
sudo redis-cli CONFIG SET requirepass "MOT_DE_PASSE_LONG"

Persist in redis.conf:

requirepass MOT_DE_PASSE_LONG

Never expose Redis without a password on a public IP.

PHP object cache (Predis example)

composer require predis/predis
<?php
$client = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
    'password' => getenv('REDIS_PASSWORD'),
]);

$key = 'article:' . $id;
if ($client->exists($key)) {
    $data = json_decode($client->get($key), true);
} else {
    $data = fetch_from_database($id);
    $client->setex($key, 3600, json_encode($data));
}

PHP sessions in Redis

Install the extension:

sudo apt install -y php-redis

/etc/php/8.3/fpm/php.ini:

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?auth=MOT_DE_PASSE_LONG"

Restart PHP-FPM. Verify:

redis-cli -a MOT_DE_PASSE_LONG KEYS 'PHPREDIS_SESSION:*' | head

Laravel

.env:

CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=MOT_DE_PASSE_LONG
php artisan config:cache
php artisan cache:clear

WordPress (object cache plugin)

Use a recognized Redis Object Cache plugin. wp-config.php:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PASSWORD', 'MOT_DE_PASSE_LONG');
define('WP_CACHE', true);

TTL strategies

| Data type | Suggested TTL |

|----------------|-------------|

| HTML page fragment | 60–300 s |

| Config / menus | 3600 s |

| User session | 24 h (sliding) |

| Rate-limit counter | 60 s |

Invalidate cache on write:

$client->del(['article:' . $id, 'articles:list']);

Quick monitoring

redis-cli -a MOT_DE_PASSE_LONG INFO memory
redis-cli -a MOT_DE_PASSE_LONG DBSIZE
redis-cli -a MOT_DE_PASSE_LONG --latency

Alert if used_memory approaches maxmemory — LRU evictions.

High availability (note)

On a single VPS, Redis is a single point of failure. For critical production, consider Redis on a second VPS, replication, or a managed service — beyond this single-node guide.

Troubleshooting

| Symptom | Action |

|----------|--------|

| Redis OOM | Increase VPS RAM or lower maxmemory |

| Connection refused | bind, firewall, wrong password |

| “Ghost” data | Missing TTL or forgotten invalidation |

| High latency | redis-cli --latency, large values → compression |

Need help?

Open a HolyCloud ticket with INFO, RAM plan, and stack (PHP version, CMS) if you are unsure about maxmemory sizing.