Redis Memory Server
redis-memory-server is a utility that programmatically starts a real Redis server instance from Node.js, primarily designed for testing and development mocking. As of version 0.16.0, it provides an isolated, in-memory Redis environment for integration tests, allowing multiple instances to run concurrently on different ports. The package automatically handles downloading and compiling the `redis-server` binary, caching it for subsequent runs to improve performance. It is inspired by `mongodb-memory-server` and differentiates itself by providing a genuine Redis instance rather than a mock, ensuring high fidelity for integration testing. It supports Node.js environments version 18 and above, ships with TypeScript types, and abstracts away the complexities of managing Redis processes for testing workflows. Its release cadence appears active, with consistent updates for features and bug fixes.
Common errors
-
Command failed with exit code 2: make
cause The `make` utility is required for compiling the Redis binary but is missing from the system's PATH.fixInstall `make` on your operating system. For Debian/Ubuntu: `sudo apt-get install build-essential`. For macOS: `xcode-select --install`. -
Error: Redis binary not found
cause The Redis binary could not be found or successfully downloaded/compiled, or the cache was invalidated without a rebuild.fixRun `REDISMS_IGNORE_DOWNLOAD_CACHE=true npm rebuild redis-memory-server` to force a re-download and compilation. Ensure stable internet access and sufficient disk space. -
connect ECONNREFUSED
cause Your Redis client tried to connect to the RedisMemoryServer before it was fully started or after it was stopped, or with incorrect host/port details.fixAlways `await redisServer.getHost()` and `await redisServer.getPort()` to ensure the server is ready and you have the correct connection details. Ensure `redisServer.stop()` is called in a `finally` block or test teardown.
Warnings
- gotcha The initial installation or first run of `redis-memory-server` can be significantly slower than subsequent runs because it automatically downloads and compiles the `redis-server` binary. This process requires `make` to be available on your system.
- gotcha By default, `redis-memory-server` downloads the 'stable' version of the Redis binary. After the first download, this binary is cached and will not be automatically updated, leading to less deterministic environments and potentially outdated or vulnerable Redis versions across different machines or builds.
- gotcha On Windows, this library uses Memurai instead of Redis. It is currently not possible to specify a particular version of Memurai or Redis when running on Windows.
- gotcha Each `RedisMemoryServer` instance starts a full Redis server process, consuming approximately 4MB of memory. While suitable for isolated tests, running a very large number of parallel instances (e.g., hundreds) without careful resource management can lead to high memory consumption.
Install
-
npm install redis-memory-server -
yarn add redis-memory-server -
pnpm add redis-memory-server
Imports
- RedisMemoryServer
const RedisMemoryServer = require('redis-memory-server')import { RedisMemoryServer } from 'redis-memory-server' - RedisMemoryServer
import RedisMemoryServer from 'redis-memory-server'
import { RedisMemoryServer } from 'redis-memory-server' - RedisMemoryServerOpts
import { RedisMemoryServerOpts } from 'redis-memory-server'import type { RedisMemoryServerOpts } from 'redis-memory-server'
Quickstart
import { RedisMemoryServer } from 'redis-memory-server';
import { Redis } from 'ioredis'; // ioredis is a common client, install it separately
let redisServer: RedisMemoryServer | undefined;
let redisClient: Redis | undefined;
async function runRedisTest() {
redisServer = new RedisMemoryServer({
binary: {
version: '6.2.6', // Specify a fixed version for deterministic builds
},
});
try {
const host = await redisServer.getHost();
const port = await redisServer.getPort();
console.log(`Redis server started on ${host}:${port}`);
redisClient = new Redis({
host,
port,
});
await redisClient.set('mykey', 'myvalue');
const value = await redisClient.get('mykey');
console.log(`Retrieved value: ${value}`); // Expected: 'myvalue'
await redisClient.del('mykey');
const deleted = await redisClient.get('mykey');
console.log(`Value after deletion: ${deleted}`); // Expected: null
} catch (error) {
console.error('An error occurred during the test:', error);
} finally {
if (redisClient) {
await redisClient.quit();
console.log('Redis client disconnected.');
}
if (redisServer) {
await redisServer.stop();
console.log('Redis memory server stopped.');
}
}
}
runRedisTest();