Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/compressed-redis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import test from "node:test";
import assert from "node:assert/strict";
import Redis from "ioredis";

import { RedisCacheStore, CompressedRedisCacheStore } from ".";
import { testCache } from "./storetest";

const { REDIS_HOST = "localhost" } = process.env;

test("CompressedRedisCacheStore", async (t) => {
const redisClient = new Redis(REDIS_HOST);
t.after(() => redisClient.quit());

const cache = new CompressedRedisCacheStore(redisClient);

await testCache(t, cache);

const uncompressedCache = new RedisCacheStore(redisClient);
uncompressedCache.setKeyTemplate("-- test --");

await t.test(
"CompressedRedisCacheStore can read RedisCacheStore entries",
async () => {
const key = "object";
const value = { a: 1, b: "c" };

const expiresAt = Date.now() + 5000;
await uncompressedCache.set(key, { value, expiresAt });

assert.deepEqual(await cache.get(key), { value, expiresAt });

await cache.delete(key);
},
);
await t.test(
"RedisCacheStore treats CachedRedisCacheStore entries as misses",
async () => {
const key = "object";
const value = { a: 1, b: "c" };

const expiresAt = Date.now() + 5000;
await cache.set(key, { value, expiresAt });

assert.deepEqual(await uncompressedCache.get(key), undefined);

await cache.delete(key);
},
);
});
106 changes: 106 additions & 0 deletions src/compressed-redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { exponentialBuckets, Histogram, linearBuckets } from "prom-client";
import type {
Redis as IORedisClient,
Cluster as IORedisCluster,
} from "ioredis";

import type { CacheStore, StoreEntity } from "./store";
import { metrics } from "./metrics";
import { brotliCompress, brotliDecompress, constants } from "node:zlib";
import { promisify } from "node:util";

const brotliCompressAsync = promisify(brotliCompress);
const brotliDecompressAsync = promisify(brotliDecompress);

const cacheValueSize = new Histogram({
name: "cache_compressed_redis_value_size_bytes",
help: "Size of bytes sent to Redis after compression",
labelNames: ["key"],
buckets: exponentialBuckets(100, 10, 5),
registers: [],
});

const cacheCompressionRatio = new Histogram({
name: "cache_compressed_ratio",
help: "Ratio of uncompressed:compressed data",
labelNames: ["key"],
buckets: linearBuckets(0, 0.1, 11),
registers: [],
});

metrics.push(cacheValueSize, cacheCompressionRatio);

type Client = Pick<IORedisClient | IORedisCluster, "getBuffer" | "set" | "del">;

// Brotli looks like all the interesting options are on the compressor,
// which wouldn't change the format of the stored data. But version the magic bytes just in case.
// Br 26-03
const brotli2603MagicBytes = Buffer.from([0x42, 0x72, 0x26, 0x03]);
Copy link
Copy Markdown
Member

@smithamax smithamax Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these well known magic bytes or just something you came up with, fine either, if it's well known just note it

Just clocked that 26-03 is the year and month 🤦

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised to learn there's no magic bytes in the Brotli compression format itself. I guess it reduces compression ratios.


export class CompressedRedisCacheStore<T> implements CacheStore<T> {
private keyTemplate: string | null = null;

constructor(private client: Client) {}

setKeyTemplate(keyTemplate: string) {
if (this.keyTemplate !== null) {
throw new Error("Cannot change key template");
}

this.keyTemplate = keyTemplate;
}

async get(key: string): Promise<StoreEntity<T> | undefined> {
const rawData = await this.client.getBuffer(key);

if (rawData === null) return undefined;

if (
rawData
.subarray(0, brotli2603MagicBytes.length)
.equals(brotli2603MagicBytes)
) {
return JSON.parse(
(
await brotliDecompressAsync(
rawData.subarray(brotli2603MagicBytes.length),
)
).toString("utf8"),
);
}

try {
return JSON.parse(rawData.toString("utf8"));
} catch {
return undefined;
}
}

async set(key: string, record: StoreEntity<T>): Promise<void> {
const ttl = record.expiresAt - Date.now();
if (ttl <= 0) return;

const jsonBuffer = Buffer.from(JSON.stringify(record), "utf8");
const buf = Buffer.concat([
brotli2603MagicBytes,
await brotliCompressAsync(jsonBuffer, {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,
[constants.BROTLI_PARAM_SIZE_HINT]: jsonBuffer.length,
}),
]);

if (this.keyTemplate !== null) {
cacheValueSize.observe({ key: this.keyTemplate }, buf.length);
cacheCompressionRatio.observe(
{ key: this.keyTemplate },
buf.length / jsonBuffer.length,
);
}

await this.client.set(key, buf, "PX", ttl);
}

async delete(key: string): Promise<void> {
await this.client.del(key);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./store";
export * from "./cache";
export * from "./lru";
export * from "./redis";
export * from "./compressed-redis";
export { registerMetrics } from "./metrics";
Loading