Integrating Redis
Redis is an optional add-on for Node.js environments on the WordPress VIP Platform. The primary purpose of Redis is to act as an in-memory datastore that is shared across application containers, but it has a rich feature set including “pub/sub” messaging.
To enable Redis for a Node.js environment, create a VIP Support request.
Connecting to Redis
Once VIP Support has enabled Redis, additional configurations are needed in order for the Node.js environment to interact with it.
Redis environment variables that are defined by the VIP Platform:
VIP_REDIS_PRIMARY
: The Redis endpoint, represented by an IP address and port separated by a colon. Example:"127.0.0.1:1234"
.VIP_REDIS_PASSWORD
: The password required to connect (AUTH
) to the Redis endpoint. This value might be defined as an empty string.- A username is not required.
The ioredis
package is a library commonly used for interacting with Redis, and the following code example code shows how to use the VIP_REDIS_PRIMARY
and VIP_REDIS_PASSWORD
environment variables to form a connection string and create a Redis client:
const Redis = require( 'ioredis' );
const endpoint = process.env.VIP_REDIS_PRIMARY;
const password = process.env.VIP_REDIS_PASSWORD;
const redisClient = new Redis( `redis://:${ password }@${ endpoint }` );
Alternatively, the VIP_REDIS_PRIMARY
environment variable can be parsed to extract the host and port:
const Redis = require( 'ioredis' );
const [ host, port ] = process.env.VIP_REDIS_PRIMARY.split( ':' );
const password = process.env.VIP_REDIS_PASSWORD;
const redisClient = new Redis( { host, port, password } );
Caution
Redis cannot be accessed from outside the VIP Platform or from a different VIP Platform environment. For example, the Redis instance provisioned for one Node.js environment is not accessible from a different Node.js environment or from any WordPress environment.
Configuration
The Redis operations EVAL
, EVALSHA
, and SCRIPT
can cause severe performance issues and are disabled by default. If these operations are absolutely necessary, it is possible for them to be enabled by creating a VIP Support request. The default eviction strategy for stored keys is allkeys-lru
.
Data ephemerality
Data stored in Redis is not guaranteed to persist and Redis should be considered an ephemeral cache store. Configuration or security updates are infrequent but can result in cold starts. Data stored in Redis is not backed up and cannot not be restored to a previous state.
Therefore, do not design applications whose operation or performance depends on data being preloaded in Redis. If an application’s performance could suffer during a Redis cold start, consider writing and maintaining a script that populates the cache, and develop a strategy for running it against the application.
Error handling
Configure applications to catch connection errors and to fall back gracefully if Redis is unavailable. Redis is very reliable, but there may be brief moments when it is unavailable if configuration or security updates are performed by VIP.
Last updated: August 08, 2024