/
Pack Hosting Panel

Redis configuration

Setting up your Redis instances using Hipex CI/CD


Introduction

Many applications use Redis for their caching needs. Because of this, we advice to treat your Redis configuration as code and include it as part of your codebase. In this article we explain how to set this up.

Configuration

Setting up a Redis instance is as easy as adding a service to your configuration:

$redis = new RedisService('cache', 7000);
$redis->setMaxMemory('4096M');
$configuration->addPlatformService($redis);

Now when your application deploys it will make sure there is a Redis instance running on port 7000 with 4GB memory.

Like any other task or service you can configure redis service for a specific stage or server roles.

$redis = new RedisService('cache', 7000);
$redis->setStage($stage);
$redis->setServerRoles([ServerRole::APPLICATION]);

Master / Slave setup

When running a cluster you usually run 1 redis instance as master on the load balancer and each app server gets their own redis instance.

$redisMaster = new RedisService('cache', 7000);
$redisMaster->setServerRoles([ServerRole::LOAD_BALANCER]);

$redisSlave = new RedisService('cache', 7000);
$redisSlave->setServerRoles([ServerRole::APPLICATION]);
$redisSlave->setMasterServer('production123.hipex.io');

The Redis instances on the servers with role ServerRole::APPLICATION will be configured as slaves to the redis instance on production123.hipex.io.

Persistence

If you use Redis for your sessions (and you should) then saving the sessions to disk from time to time is not a bad idea. Again with a simple call on your Redis configuration is enough.

$redis = new RedisService('cache', 7000);
$redis->setSnapshotSaveFrequency(60);

This will result in saving the Redis database to disk every 60 seconds.