Skip to content

Mini in-memory redis server useful for client agnostic mocks

License

Notifications You must be signed in to change notification settings

fatal10110/js-redis-server

Repository files navigation

js-redis-server

CI npm version npm downloads License: MIT Node.js Version

In-memory Redis-compatible server implementation in JavaScript. Useful for testing and development without requiring a real Redis instance.

Features

  • Redis-compatible protocol - Works with any Redis client (ioredis, node-redis, etc.)
  • Standalone and Cluster modes - Run a single server or a full cluster
  • Lua scripting support - Execute Redis Lua scripts via WebAssembly
  • No external dependencies - Pure JavaScript, no Redis installation needed
  • Dual package - Supports both ESM and CommonJS

Installation

npm install js-redis-server

Quick Start

As a Library

import { createCustomCommander, Resp2Transport } from 'js-redis-server'

const logger = {
  info: console.log,
  error: console.error,
}

const factory = await createCustomCommander(logger)
const transport = new Resp2Transport(logger, factory.createCommander())

await transport.listen(6379)
console.log('Redis server listening on port 6379')

// Cleanup
await transport.close()
await factory.shutdown()

As a CLI

# Run a single server on default port 6379
npx js-redis-server

# Run on a specific port
npx js-redis-server --port 6380

# Run a cluster with 3 masters
npx js-redis-server --cluster --masters 3

# Run a cluster with replicas
npx js-redis-server --cluster --masters 3 --slaves 1

CLI Options

Modes:
  --single               Run a single Redis server (default)
  --cluster              Run a Redis cluster
  --mode <single|cluster>

Single server options:
  --port <number>        Port to listen on (default 6379)

Cluster options:
  --masters <number>     Number of masters (default 3)
  --slaves <number>      Number of replicas per master (default 0)
  --base-port <number>   Starting port for cluster nodes (default 30000)

General:
  -h, --help             Show help

Cluster Mode

import { ClusterNetwork } from 'js-redis-server'

const logger = { info: console.log, error: console.error }
const cluster = new ClusterNetwork(logger)

await cluster.init({
  masters: 3,
  slaves: 1,
  basePort: 30000,
})

// Get all node addresses
const nodes = cluster.getAll()
console.log(nodes.map(n => `${n.host}:${n.port}`))

// Cleanup
await cluster.shutdown()

Supported Commands

Strings

GET, SET, MGET, MSET, MSETNX, APPEND, STRLEN, GETSET, INCR, INCRBY, INCRBYFLOAT, DECR, DECRBY

Hashes

HGET, HSET, HMGET, HMSET, HDEL, HEXISTS, HGETALL, HINCRBY, HINCRBYFLOAT, HKEYS, HLEN, HVALS

Lists

LPUSH, RPUSH, LPOP, RPOP, LLEN, LRANGE, LINDEX, LSET, LREM, LTRIM

Sets

SADD, SREM, SMEMBERS, SISMEMBER, SCARD, SPOP, SRANDMEMBER, SMOVE, SDIFF, SINTER, SUNION

Sorted Sets

ZADD, ZREM, ZSCORE, ZRANK, ZREVRANK, ZCARD, ZRANGE, ZREVRANGE, ZRANGEBYSCORE, ZREMRANGEBYSCORE, ZINCRBY

Keys

DEL, EXISTS, EXPIRE, EXPIREAT, TTL, PTTL, TYPE

Server

PING, QUIT, INFO, DBSIZE, FLUSHDB, FLUSHALL

Cluster

CLUSTER INFO, CLUSTER NODES, CLUSTER SLOTS, CLUSTER SHARDS

Scripting

EVAL, EVALSHA, SCRIPT LOAD

Client

CLIENT SETNAME

Requirements

  • Node.js >= 22

Development

# Install dependencies
npm install

# Build
npm run build

# Run tests
npm test

# Run integration tests (mock backend)
npm run test:integration:mock

# Run integration tests (real Redis)
npm run test:integration:real

# Run all tests
npm run test:all

Contributing

Contributions are welcome! Please read the contributing guidelines before submitting a pull request.

License

MIT - see LICENSE for details.

About

Mini in-memory redis server useful for client agnostic mocks

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 2

  •  
  •