Redis In-Memory Server
This package spins up a real Redis server programmatically from Node.js, for testing or mocking during development.
It holds the data in memory. Each redis-server process takes about 4Mb of memory.
The server will allow you to connect your favorite client library to the Redis server and run integration tests isolated from each other.
It is inspired heavily by mongodb-memory-server.
On install, it downloads the Redis source,
compiles the redis-server binary, and saves it to a cache folder.
On starting a new instance of the in-memory server, if the binary cannot be found,
it will be downloaded and compiled; thus, the first run may take some time.
All further runs will be fast because they will use the downloaded binaries.
NOTE: If no version is specified (or version is set to stable),
the binary will not be updated after the first run.
This package automatically downloads source code from https://download.redis.io/.
Every RedisMemoryServer instance starts a fresh Redis server on a free port.
You may start up several redis-server processes simultaneously.
When you terminate your script or call stop(), the Redis server(s) will be automatically shut down.
It works in Travis CI without additional services or addons in .travis.yml.
Installation
yarn add redis-memory-server --dev
npm install redis-memory-server --save-dev
On install, this package auto-downloads and compiles version stable of the redis-server binary to node_modules/.cache/redis-binaries.
Requirements
- NodeJS: 16+
- Typescript: 3.8+ (if used)
make
Windows
This library uses the latest version of Memurai on Windows.
Currently, it is not possible to specify a particular version of Memurai or Redis on Windows.
Configuring which redis-server binary to use
The default behavior is that version stable will be downloaded.
You can set configurations via environment variables
or via package.json.
Using the stable version of the binary
After the first install of the stable version on a machine,
the binary will not be automatically updated on that machine.
This may be a concern because:
- the install is less deterministic
- some machines could have vulnerable versions
If this is a concern, either:
- specify a
version other than stable
- forcibly update the
stable binary on demand using a command like:
REDISMS_IGNORE_DOWNLOAD_CACHE=1 yarn rebuild redis-memory-server
REDISMS_IGNORE_DOWNLOAD_CACHE=1 npm rebuild redis-memory-server
- specify the
ignoreDownloadCache option
(NOTE: this will make installs/runs slower)
Usage
Simple server start
import { RedisMemoryServer } from 'redis-memory-server';
const redisServer = new RedisMemoryServer();
const host = await redisServer.getHost();
const port = await redisServer.getPort();
redisServer.getInstanceInfo();
await redisServer.stop();
redisServer.getInstanceInfo();
Start server via npx
npx redis-memory-server
REDISMS_PORT=6379 npx redis-memory-server
Available options for RedisMemoryServer
All settings are optional.
const redisServer = new RedisMemoryServer({
instance: {
port: number,
ip: string,
args: [],
},
binary: {
version: string,
downloadDir: string,
ignoreDownloadCache: boolean,
systemBinary: string,
},
autoStart: boolean,
});
Options which can be set via environment variables
REDISMS_DOWNLOAD_DIR=/path/to/redis/binaries
REDISMS_VERSION=6.0.10
REDISMS_IGNORE_DOWNLOAD_CACHE=1
REDISMS_DEBUG=1
REDISMS_DOWNLOAD_MIRROR=host
REDISMS_DOWNLOAD_URL=url
REDISMS_DISABLE_POSTINSTALL=1
REDISMS_SYSTEM_BINARY=/usr/local/bin/redis-server
Options which can be set via package.json
You can also use package.json to configure the installation process.
It will search up the hierarchy looking for package.json files and combine all configurations, where closer package.json files take precedence.
Environment variables have higher priority than contents of package.json files.
{
"redisMemoryServer": {
"downloadDir": "/path/to/redis/binaries",
"version": "6.0.10",
"ignoreDownloadCache": "1",
"debug": "1",
"downloadMirror": "url",
"disablePostinstall": "1",
"systemBinary": "/usr/local/bin/redis-server"
}
}
By default, it starts looking for package.json files at process.cwd().
To change this:
import { findPackageJson } from 'redis-memory-server/lib/util/resolve-config';
findPackageJson('/custom/path');
Simple test with ioredis
Take a look at this test file.
Debug mode
Debug mode can be enabled with an environment variable or in package.json:
REDISMS_DEBUG=1
or
{
"redisMemoryServer": {
"debug": "1"
}
}
Credits
This package is inspired heavily by mongodb-memory-server.
License
MIT