cache-boy

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT

README

cache-boy

A Redis-compatible in-memory data store written in Go. It speaks the Redis protocol (RESP2 and RESP3), so existing Redis clients and tools work with it. It adds features that usually need extra libraries or services:

Problem What you usually do cache-boy
Invalidate every cache entry for a user Track keys yourself SET k v TAGS user:1 + TAG.INVALIDATE user:1
Cache stampede when a hot key expires Hand-written locks CACHE.GET (one client computes, the others wait)
Rate limiting Lua scripts RATELIMIT key 100 1m
Distributed locks Redlock library LOCK name 10s with fencing tokens
Background jobs BullMQ QPUSH / QPOP / QACK with retries, delay, dead-letter
Reacting to key changes Keyspace notifications config KEYWATCH user:*, WEBHOOK.ADD
Calling it from serverless / edge Paid HTTP proxies Built-in REST + WebSocket API
Seeing what's in the cache Third-party GUIs Built-in web dashboard
JSON, time series, vectors Redis Stack modules Built in
Multi-tenant apps Key prefixes by convention NAMESPACE USE tenant42
Reads hammering the network Nothing in ioredis Client-side caching (RESP3 tracking) in the TS SDK
Paying for the same LLM answer twice Separate vector DB + glue code Semantic cache: SCACHE.GET hits on similar prompts

It also uses every CPU core: the keyspace is split into shards with their own locks, while Redis runs commands on a single thread.

Install

Windows (PowerShell, no admin needed):

irm https://raw.githubusercontent.com/shamim3696/cache-boy/main/scripts/install.ps1 | iex

Linux / macOS:

curl -fsSL https://raw.githubusercontent.com/shamim3696/cache-boy/main/scripts/install.sh | sh

Docker:

docker run -d -p 6380:6380 -p 6381:6381 -v cache-boy-data:/data ghcr.io/shamim3696/cache-boy

Go:

go install github.com/shamim3696/cache-boy/cmd/...@latest

Or download an archive for your platform from Releases. Each one holds cache-boy, cache-boy-cli and cache-boy-bench. Both install scripts check the SHA-256 checksum before installing; set CACHE_BOY_VERSION=v0.1.0 to pin a version.

Node.js SDK:

npm install cache-boy-client

Then start the server with cache-boy and open http://127.0.0.1:6381 for the dashboard.

Quick start

From source:

go build -o bin/ ./cmd/...

./bin/cache-boy                          # 127.0.0.1:6380, dashboard on http://127.0.0.1:6381
./bin/cache-boy configs/cache-boy.conf   # with a config file
./bin/cache-boy --port 7000 --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes

./bin/cache-boy-cli                      # interactive shell (use cmd/PowerShell on Windows, or winpty in Git Bash)
./bin/cache-boy-cli SET greeting hello EX 60
./bin/cache-boy-bench -t set,get -n 100000 -P 16

Docker: docker compose -f deployments/docker/docker-compose.yml up starts a leader and a replica.

Using it

From any Redis client (redis-cli, ioredis, go-redis, redis-py): connect to port 6380.

From Node.js, with the TypeScript SDK in sdk/typescript:

import { CacheBoy } from "cache-boy-client";
const cache = new CacheBoy({ url: "redis://127.0.0.1:6380", clientCache: { maxEntries: 10_000 } });

const user = await cache.wrap(`user:${id}`, () => db.users.find(id), { ttl: 60, tags: [`user:${id}`] });
await cache.tags.invalidate(`user:${id}`);
const { allowed } = await cache.rateLimit(`api:${ip}`, 100, "1m");

From Go: import "github.com/shamim3696/cache-boy/pkg/client". It includes helpers like Wrap, Lock, RateLimit and QPush.

Over HTTP (serverless, edge, any language):

curl -X POST localhost:6381/v1/command -d '["SET","a","1"]'      # {"result":"OK"}
curl localhost:6381/v1/keys/a                                     # typed JSON
curl -X POST localhost:6381/v1/pipeline -d '[["INCR","n"],["GET","n"]]'

If requirepass is set, add Authorization: Bearer <password>. Prometheus metrics are at /metrics and the WebSocket API at /v1/ws.

Every non-Redis command is documented in docs/features.md.

What's supported

  • Data types: strings, lists, hashes, sets, sorted sets (skip list), JSON documents, time series (Gorilla-compressed), vector sets (HNSW index), Bloom filters, HyperLogLog
  • Compact encodings: small hashes and sets are stored as listpacks and intsets, and integer strings as int64, as in Redis
  • Keys: TTLs (lazy and active expiry), SCAN, RENAME, COPY, OBJECT
  • Memory: maxmemory with LRU, LFU, random, TTL and W-TinyLFU eviction (allkeys-tinylfu: a Count-Min Sketch plus a doorkeeper Bloom filter, which resists scans), an eviction pool, timing-wheel active expiry, and optional compression of large values
  • Transactions: MULTI / EXEC / WATCH, plus atomic server-side JavaScript (EVAL)
  • AI: semantic cache for LLM responses, vector similarity search
  • Pub/Sub: channels and patterns, key-change streams, webhooks
  • Persistence: binary snapshots (SAVE, BGSAVE, save rules) and an append-only file with fsync policies and rewrite
  • Replication: leader → replicas with full sync, partial resync from a backlog after short disconnects, WAIT, read-only replicas, masterauth
  • High availability: Raft leader election for automatic failover
  • Cluster: 16384 hash slots with a static slot map, MOVED / CROSSSLOT redirects; the TS SDK's CacheBoyCluster routes by slot and follows redirects
  • Security: requirepass, ACL users with command, category and key-pattern rules
  • Observability: INFO, SLOWLOG, MONITOR, per-command latency histograms, Prometheus, a dashboard

Project layout

cmd/cache-boy            server
cmd/cache-boy-cli        CLI (like redis-cli)
cmd/cache-boy-bench      benchmark (like redis-benchmark)
internal/protocol/resp   RESP2/RESP3 reader and writer
internal/server          TCP server: one goroutine per client, plus a writer goroutine
internal/command         command table and dispatcher, one file per command group
internal/store           sharded keyspace, entries, expiry sampling
internal/datatype        deque, skiplist, jsondoc, timeseries, vector
internal/expiry, eviction, compression, glob
internal/persistence     snapshot (binary format) and AOF
internal/replication     leader/follower, Raft failover
internal/consensus/raft  leader election
internal/cluster         hash slots and routing
internal/auth            users, passwords, ACL
internal/pubsub, transaction, blocking, tracking
internal/feature/*       tags, ratelimit, lock, queue, watch, singleflight, history, scripting, namespace
internal/api             REST and WebSocket API
internal/observability   metrics, slowlog, monitor
web/dashboard            embedded web UI
pkg/client               Go client
sdk/typescript           Node.js / TypeScript SDK

How a command runs

  1. server/conn.go reads a RESP array from the socket (resp.Reader).
  2. command.Engine.Execute looks up the command, then checks arity, authentication and ACL, MULTI queuing and pub/sub mode.
  3. run adds the namespace prefix to key arguments and checks cluster slots, read-only replicas and maxmemory. It then locks only the shards that hold the command's keys, which is why commands on different keys run in parallel.
  4. The handler runs. Write commands then emit key events (WATCH, tracking, BLPOP wake-ups, key watchers, history and tags all listen to these) and are propagated to the AOF and replicas, still under the lock, so the order on disk matches the order in memory.
  5. The reply goes to the client's writer goroutine, which batches pipelined replies into one network write.

Testing

go test ./...                                    # Go unit and integration tests
go test -fuzz FuzzReadCommand ./internal/protocol/resp   # fuzz the protocol parser
cd test/compat/ioredis && npm install && npm test -- 6380  # real ioredis against a running server
cd sdk/typescript && npm install && npm test     # TypeScript SDK
CACHEBOY_URL=redis://127.0.0.1:6380 npm test     # SDK against a running server

Performance

With many keys and no pipelining, cache-boy is about 2x faster than Redis 7 on the same 4 CPUs. With pipelining the two are roughly even. With one extremely hot key under heavy pipelining, Redis is faster. See docs/benchmarks.md.

The test suite passes under the race detector (go test -race ./...).

Known limitations

  • After a long disconnect (more writes than repl-backlog-size), a replica falls back to a full resync. A replica that restarts always does a full resync, because the replication ID is not persisted.
  • Failover can lose writes the new leader hadn't received yet, because replication is asynchronous (as in Redis).
  • Cluster slots are a static map from the config; there's no resharding or gossip.
  • Snapshots and AOF rewrites briefly lock all shards and need a temporary in-memory copy of the dataset, because Go can't fork() like Redis.
  • Locks, rate limits and compute locks live only on the node that handles them; they aren't replicated.
  • HSCAN / SSCAN / ZSCAN return all elements in one page.
  • Vector search is exact (brute force), which is fine up to about 100k vectors per key.

Directories

Path Synopsis
cmd
cache-boy command
Command cache-boy starts the server.
Command cache-boy starts the server.
cache-boy-bench command
Command cache-boy-bench measures throughput and latency, like redis-benchmark.
Command cache-boy-bench measures throughput and latency, like redis-benchmark.
cache-boy-cli command
Command cache-boy-cli is an interactive client, similar to redis-cli.
Command cache-boy-cli is an interactive client, similar to redis-cli.
internal
api/apitest
Package apitest provides minimal stand-ins for data commands (GET, SET, TYPE...) so the API, WebSocket and client packages can be tested before the real implementations are merged.
Package apitest provides minimal stand-ins for data commands (GET, SET, TYPE...) so the API, WebSocket and client packages can be tested before the real implementations are merged.
api/apiutil
Package apiutil holds what the HTTP API and the WebSocket endpoint share: RESP → JSON conversion, JSON → command parsing, and credential checks.
Package apiutil holds what the HTTP API and the WebSocket endpoint share: RESP → JSON conversion, JSON → command parsing, and credential checks.
api/httpapi
Package httpapi is the REST API: JSON commands, pipelines and transactions, a key browser, server stats, Prometheus metrics, the WebSocket endpoint (/v1/ws) and the dashboard (/).
Package httpapi is the REST API: JSON commands, pipelines and transactions, a key browser, server stats, Prometheus metrics, the WebSocket endpoint (/v1/ws) and the dashboard (/).
api/ws
Package ws serves commands over WebSocket (mounted at /v1/ws).
Package ws serves commands over WebSocket (mounted at /v1/ws).
auth
Package auth manages users and passwords (AUTH, requirepass) and per-user permissions (ACL).
Package auth manages users and passwords (AUTH, requirepass) and per-user permissions (ACL).
blocking
Package blocking lets commands like BLPOP wait until one of their keys is written.
Package blocking lets commands like BLPOP wait until one of their keys is written.
cluster
Package cluster implements Redis Cluster style key routing with a static slot map: the keyspace is split into 16384 hash slots, each node serves a set of slot ranges, and a node answers MOVED for keys it doesn't own so cluster-aware clients learn where to send them.
Package cluster implements Redis Cluster style key routing with a static slot map: the keyspace is split into 16384 hash slots, each node serves a set of slot ranges, and a node answers MOVED for keys it doesn't own so cluster-aware clients learn where to send them.
command
Package command holds the command table and the dispatcher (Engine.Execute).
Package command holds the command table and the dispatcher (Engine.Execute).
compression
Package compression compresses large string values in memory (DEFLATE, fastest level) so big cached payloads like HTML or JSON take less RAM.
Package compression compresses large string values in memory (DEFLATE, fastest level) so big cached payloads like HTML or JSON take less RAM.
config
Package config holds server settings.
Package config holds server settings.
consensus/raft
Package raft implements Raft leader election (the Raft paper, §5.2) to pick which cache-boy node is the replication leader.
Package raft implements Raft leader election (the Raft paper, §5.2) to pick which cache-boy node is the replication leader.
datatype/bloom
Package bloom implements a scalable Bloom filter (Almeida et al., 2007): when the newest sub-filter reaches its capacity a larger one is added with a tighter error rate, so the compound false-positive rate stays bounded by the requested one however many items are inserted.
Package bloom implements a scalable Bloom filter (Almeida et al., 2007): when the newest sub-filter reaches its capacity a larger one is added with a tighter error rate, so the compound false-positive rate stays bounded by the requested one however many items are inserted.
datatype/deque
Package deque is a growable ring buffer of strings used for Redis lists.
Package deque is a growable ring buffer of strings used for Redis lists.
datatype/hll
Package hll implements HyperLogLog cardinality estimation the way Redis does: 16384 6-bit registers indexed by a MurmurHash64A of the element, and Ertl's improved estimator ("New cardinality estimation algorithms for HyperLogLog sketches", 2017), whose standard error is about 0.81%.
Package hll implements HyperLogLog cardinality estimation the way Redis does: 16384 6-bit registers indexed by a MurmurHash64A of the element, and Ertl's improved estimator ("New cardinality estimation algorithms for HyperLogLog sketches", 2017), whose standard error is about 0.81%.
datatype/jsondoc
Package jsondoc stores a JSON document and supports a JSONPath subset:
Package jsondoc stores a JSON document and supports a JSONPath subset:
datatype/skiplist
Package skiplist implements sorted sets the same way Redis does: a skip list ordered by (score, member) for range queries, plus a hash map member→score for O(1) lookups.
Package skiplist implements sorted sets the same way Redis does: a skip list ordered by (score, member) for range queries, plus a hash map member→score for O(1) lookups.
datatype/timeseries
Package timeseries stores (timestamp, value) samples sorted by time, with optional retention and range queries with downsampling.
Package timeseries stores (timestamp, value) samples sorted by time, with optional retention and range queries with downsampling.
datatype/vector
Package vector stores embeddings by ID and answers nearest-neighbour queries.
Package vector stores embeddings by ID and answers nearest-neighbour queries.
eviction
Package eviction frees memory when maxmemory is reached.
Package eviction frees memory when maxmemory is reached.
expiry
Package expiry runs active expiration.
Package expiry runs active expiration.
feature/history
Package history keeps the last N versions of every string key (CONFIG SET history-depth N), including deletions, so old values can be read back or restored.
Package history keeps the last N versions of every string key (CONFIG SET history-depth N), including deletions, so old values can be read back or restored.
feature/lock
Package lock implements named distributed locks with fencing tokens.
Package lock implements named distributed locks with fencing tokens.
feature/namespace
Package namespace isolates keys of different apps/tenants sharing one server.
Package namespace isolates keys of different apps/tenants sharing one server.
feature/queue
Package queue implements job queues with delayed jobs, a visibility timeout, retries and a dead-letter list.
Package queue implements job queues with delayed jobs, a visibility timeout, retries and a dead-letter list.
feature/ratelimit
Package ratelimit implements GCRA (Generic Cell Rate Algorithm) rate limiting.
Package ratelimit implements GCRA (Generic Cell Rate Algorithm) rate limiting.
feature/scripting
Package scripting runs server-side JavaScript (EVAL/EVALSHA) with goja.
Package scripting runs server-side JavaScript (EVAL/EVALSHA) with goja.
feature/semcache
Package semcache is a semantic cache for LLM apps: it stores responses by the embedding of the prompt, and a lookup returns the cached response of the most similar earlier prompt if its cosine similarity is above a threshold.
Package semcache is a semantic cache for LLM apps: it stores responses by the embedding of the prompt, and a lookup returns the cached response of the most similar earlier prompt if its cosine similarity is above a threshold.
feature/singleflight
Package singleflight holds the compute locks behind CACHE.GET: when a cached key is missing, only one client gets to recompute it while the others wait for the key to be written.
Package singleflight holds the compute locks behind CACHE.GET: when a cached key is missing, only one client gets to recompute it while the others wait for the key to be written.
feature/tags
Package tags keeps a two-way index between keys and tags, so every key carrying a tag can be found (and deleted) in one call.
Package tags keeps a two-way index between keys and tags, so every key carrying a tag can be found (and deleted) in one call.
feature/watch
Package watch streams key changes to clients (KEYWATCH) and to HTTP endpoints (webhooks).
Package watch streams key changes to clients (KEYWATCH) and to HTTP endpoints (webhooks).
glob
Package glob implements Redis-style glob matching: * ? [abc] [^a] [a-z] and \ escapes.
Package glob implements Redis-style glob matching: * ? [abc] [^a] [a-z] and \ escapes.
observability
Package observability collects metrics (per-command calls and latency), the slow log, and MONITOR streaming (tracing.go).
Package observability collects metrics (per-command calls and latency), the slow log, and MONITOR streaming (tracing.go).
persistence
Package persistence ties snapshots and the AOF to the engine: it loads data at startup, applies the "save" rules, runs BGSAVE / BGREWRITEAOF and flushes everything at shutdown.
Package persistence ties snapshots and the AOF to the engine: it loads data at startup, applies the "save" rules, runs BGSAVE / BGREWRITEAOF and flushes everything at shutdown.
persistence/aof
Package aof implements the append-only file: every write command is appended in RESP form (the same bytes a client sends), and replaying the file through the dispatcher rebuilds the dataset.
Package aof implements the append-only file: every write command is appended in RESP form (the same bytes a client sends), and replaying the file through the dispatcher rebuilds the dataset.
persistence/snapshot
Package snapshot implements cache-boy's binary point-in-time dump (the equivalent of Redis' RDB file).
Package snapshot implements cache-boy's binary point-in-time dump (the equivalent of Redis' RDB file).
protocol/resp
Package resp implements the Redis serialization protocol, versions 2 and 3.
Package resp implements the Redis serialization protocol, versions 2 and 3.
pubsub
Package pubsub implements PUBLISH/SUBSCRIBE with exact channels and glob patterns.
Package pubsub implements PUBLISH/SUBSCRIBE with exact channels and glob patterns.
replication
Package replication implements leader → follower replication in the style of Redis' SYNC protocol:
Package replication implements leader → follower replication in the style of Redis' SYNC protocol:
server
Package server accepts TCP connections and runs one goroutine per client.
Package server accepts TCP connections and runs one goroutine per client.
store
Package store is the keyspace: a map of key → Entry split into shards, each with its own mutex, so commands on different keys run in parallel on all CPU cores.
Package store is the keyspace: a map of key → Entry split into shards, each with its own mutex, so commands on different keys run in parallel on all CPU cores.
tracking
Package tracking implements server-assisted client-side caching (CLIENT TRACKING).
Package tracking implements server-assisted client-side caching (CLIENT TRACKING).
transaction
Package transaction implements WATCH: optimistic locking for MULTI/EXEC.
Package transaction implements WATCH: optimistic locking for MULTI/EXEC.
pkg
client
Package client is the Go client for cache-boy (and any RESP server).
Package client is the Go client for cache-boy (and any RESP server).
web
dashboard
Package dashboard serves the built-in web dashboard: plain HTML, CSS and JavaScript embedded in the binary, with no build step and no external requests.
Package dashboard serves the built-in web dashboard: plain HTML, CSS and JavaScript embedded in the binary, with no build step and no external requests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL