Files
README.md

redis #

redis client for zig implementing RESP (REdis Serialization Protocol).

usage #

const redis = @import("redis");

var client = try redis.Client.connect(allocator, "localhost", 6379);
defer client.close();

// strings
var str = client.strings();
try str.set("key", "value");
if (try str.get("key")) |val| { ... }

// hashes
var hash = client.hashes();
_ = try hash.hset("user:1", &.{
    .{ "name", "alice" },
    .{ "email", "alice@example.com" },
});
const all = try hash.hgetAll("user:1");

// lists
var list = client.lists();
_ = try list.rpush("queue", &.{ "a", "b", "c" });
if (try list.lpop("queue")) |item| { ... }

// sets
var set = client.sets();
_ = try set.sadd("tags", &.{ "redis", "fast" });
const common = try set.sinter(&.{ "set1", "set2" });

// sorted sets
var zset = client.sortedSets();
_ = try zset.zadd("scores", &.{
    .{ .score = 100, .member = "alice" },
    .{ .score = 85, .member = "bob" },
}, .{});
const top = try zset.zrangeWithScores("scores", 0, 2, .{ .rev = true });

// streams
var stream = client.streams();
const id = try stream.xadd("events", .auto, &.{.{ "type", "login" }});
try stream.xgroupCreate("events", "workers", "0", true);
const entries = try stream.xreadgroup("workers", "worker-1", &.{"events"}, &.{">"}, .{});

data types #

strings #

get, set, setNx, setEx, incr, incrBy, decr, decrBy, mget, mset, append, strlen

hashes #

hset, hget, hmget, hdel, hexists, hlen, hincrBy, hgetAll, hkeys, hvals, hscan

lists #

lpush, rpush, lpop, rpop, llen, lrange, lindex, lset, lrem, ltrim, blpop, brpop, lmove, blmove

sets #

sadd, srem, sismember, smismember, smembers, scard, spop, srandmember, sunion, sunionstore, sinter, sinterstore, sintercard, sdiff, sdiffstore, smove, sscan

sorted sets #

zadd, zrem, zscore, zrank, zrevrank, zcard, zcount, zincrBy, zrange, zrangeWithScores, zpopmin, zpopmax, bzpopmin, bzpopmax, zscan

streams #

xadd, xlen, xrange, xrevrange, xread, xgroupCreate, xgroupDestroy, xgroupCreateConsumer, xgroupDelConsumer, xreadgroup, xack, xpending, xpendingRange, xautoclaim, xclaim, xtrim, xdel, xinfo

keys #

del, unlink, exists, expire, expireAt, ttl, pttl, persist, rename, renameNx, type, scan, keys

design #

  • tagged unions: Value = union(enum) for all RESP types
  • distinct errors: ConnectionError, ProtocolError, CommandError
  • buffer ownership: client owns read buffer; slices valid until next command
  • modular commands: each data type in its own file

error handling #

const result = client.strings().get("key") catch |err| switch (err) {
    error.ConnectionRefused => { ... },
    error.Timeout => { ... },
    error.WrongType => { ... },
    else => return err,
};

building #

zig build test    # run tests
zig build run     # run example (requires redis on localhost:6379)

integration #

zig fetch --save https://tangled.sh/@zzstoatzz.io/redis/archive/main

then in build.zig:

const redis_dep = b.dependency("redis", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("redis", redis_dep.module("redis"));

deadlines and cancellation #

read_timeout_ms bounds one complete response, starting after command transmission. It does not restart when another fragment arrives. Native bulk header and body reads share that deadline, including time spent by the caller between them. write_timeout_ms bounds the complete encoded command's transmission. Encoding and connection setup are outside these budgets. Non-positive values disable the corresponding deadline.

Finite deadlines require Io.concurrent; unavailable concurrency returns ConcurrencyUnavailable instead of falling back to unbounded I/O. All waits use the supplied Io. Timed phases join their I/O task before returning, including on cancellation. This uses a task per timed phase (a fiber under zio, a worker under Threaded).

Interrupted transmission, incomplete responses, and protocol failures close and poison the transport. isPoisoned() reports this state; subsequent commands return NotAllowed. Call close() once to release the remaining client storage. Pre-transmission encoding allocation failures leave the client usable. A complete Redis error response from sendCommand also leaves it usable.

A timeout or cancellation does not mean Redis did not execute the command. Reconnect for a subsequent operation, but do not blindly replay mutations or stream claims after an ambiguous failure. Clients require exclusive ownership for the whole command, including both halves of a native bulk response.

Run the shared contract suite against both Threaded and zio with zig build test-contract. Add -Dredis-test-port=<isolated-port> to include real Redis checks; zig build test-redis uses the same port option.