Effect-first Redis integration for Bun.
The library exposes two layers of abstraction:
Redis: an Effect service with Redis operations wrapped inEffectKeyValueStore: a higher-level store built on top of that service
This package is intentionally scoped to Bun's native Redis client.
- Bun >= 1.2.22
- Redis Server >= 7.2 or Valkey
- Effect >= 3.1.2
@effect/platform>= 0.52.3
bun add @vortexdd/effect-redis-bunThe Redis service exposes Effect-wrapped operations for the minimum low-level surface:
connectclosesendgetgetBuffersetdelscan
The KeyValueStore adapter is built from the Redis service and supports:
getgetUint8Arraysetremoveclearsize
import { KeyValueStore } from "@effect/platform";
import { Effect } from "effect";
import { makeLayer } from "@vortexdd/effect-redis-bun";
const RedisStoreLive = makeLayer({
host: "localhost",
port: 6379,
database: 0,
scanBatchSize: 200,
});
const program = Effect.gen(function* () {
const store = yield* KeyValueStore.KeyValueStore;
yield* store.set("user:123", JSON.stringify({ name: "Alice" }));
const value = yield* store.get("user:123");
yield* store.remove("user:123");
return value;
});
await Effect.runPromise(program.pipe(Effect.provide(RedisStoreLive)));import { Effect } from "effect";
import { makeRedisLayer, Redis } from "@vortexdd/effect-redis-bun";
const RedisLive = makeRedisLayer({
host: "localhost",
port: 6379,
});
const program = Effect.gen(function* () {
const redis = yield* Redis;
yield* redis.connect;
yield* redis.set("raw:key", "value");
const value = yield* redis.get("raw:key");
const scan = yield* redis.scan("0", "MATCH", "raw:*", "COUNT", 50);
yield* redis.del("raw:key");
yield* redis.close;
return { value, scan };
});
await Effect.runPromise(program.pipe(Effect.provide(RedisLive)));import { RedisClient } from "bun";
import { fromClient, fromClientService } from "@vortexdd/effect-redis-bun";
const client = new RedisClient("redis://localhost:6379/0", {
enableOfflineQueue: false,
});
const redis = fromClientService(client);
const store = fromClient(client, {
scanBatchSize: 100,
});import { KeyValueStore } from "@effect/platform";
import { Effect, Layer } from "effect";
import {
fromClientService,
layerFromRedis,
Redis,
} from "@vortexdd/effect-redis-bun";
const program = Effect.gen(function* () {
const store = yield* KeyValueStore.KeyValueStore;
yield* store.set("cache:key", "value");
return yield* store.get("cache:key");
}).pipe(
Effect.provide(
Layer.provide(
layerFromRedis({ scanBatchSize: 50 }),
Layer.succeed(Redis, fromClientService(client))
)
)
);Effect service tag for the low-level Redis API.
Builds a Redis URL from ConnectionConfig.
Creates a Bun RedisClient instance.
Builds a Redis service from an existing Bun client.
Builds a KeyValueStore from an existing Redis service.
Builds a KeyValueStore directly from a Bun client.
Creates a scoped layer that provides Redis.
Creates a KeyValueStore layer from an already-provided Redis service.
Creates a scoped layer that provides KeyValueStore.
Alias of makeKeyValueStoreLayer(config).
interface ConnectionConfig {
readonly host: string;
readonly port: number;
readonly username?: string;
readonly password?: string;
readonly database?: number;
readonly tls?: boolean;
readonly connectTimeoutMs?: number;
readonly scanBatchSize?: number;
}Every fallible Redis operation fails with a typed RedisError:
RedisCommandError— the server rejected the command with an error reply. Carriescommand, the parsed replycode("ERR","WRONGTYPE","NOAUTH","MOVED", ...), the replymessage, and the original error ascause.RedisConnectionError— the command never got a server reply (connection refused/closed, timeout, authentication, client-side, or transport failure). Carriescommand, the underlying clientcodewhen available,message, andcause.connectalways uses this error type.
import { Effect } from "effect";
import { Redis } from "@vortexdd/effect-redis-bun";
const readString = (key: string) =>
Effect.gen(function* () {
const redis = yield* Redis;
return yield* redis.get(key);
}).pipe(
Effect.catchTag("RedisCommandError", (error) =>
error.code === "WRONGTYPE" ? Effect.succeed(null) : Effect.fail(error),
),
);The KeyValueStore adapter and its layer keep their platform contract and fail with PlatformError; the underlying RedisError is preserved as cause.
makeRedisLayercreates a ready-to-use service and closes the client when the layer scope ends.clearusesSCANin batches and avoidsKEYS *.sizeusesDBSIZEinstead of scanning the full keyspace.scanfalls back tosend("SCAN", ...)if the Bun client version does not exposescandirectly.
Version 3.x replaces PlatformError with typed errors on the Redis service:
- Redis commands fail with
RedisError(RedisCommandError | RedisConnectionError) instead ofPlatformError.PlatformError;connectandmakeRedisLayerfail more specifically withRedisConnectionError. KeyValueStoreandmakeKeyValueStoreLayerare unaffected — they still fail withPlatformError, now carrying theRedisErrorascause.- If you matched on
SystemErroror dug througherror.causeto read server error messages, switch toEffect.catchTag("RedisCommandError", ...)and thecodefield.
Version 2.x removes the public service that exposed the raw Bun client directly.
If you were using the old raw-client layer, migrate to one of these:
makeRedisLayer(config)if you want low-level Redis commands asEffectmakeLayer(config)ormakeKeyValueStoreLayer(config)if you wantKeyValueStorefromClientService(client)if you already own the Bun client instance