Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

perf(dashboard): share analytics responses across users and workers (#1212)

Goldflaga404e376e502
diff --git a/docker-compose.yml b/docker-compose.yml
index e1f668659..92ce80c2d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -133,6 +133,7 @@ services:
- CLUSTER_WORKERS=${CLUSTER_WORKERS:-4}
- DEPLOYMENT=${DEPLOYMENT}
- LITE_DASHBOARD=${LITE_DASHBOARD}
+ - DASHBOARD_CACHE_TTL_SECONDS=${DASHBOARD_CACHE_TTL_SECONDS:-30}
- OPENROUTER_API_KEY=${OPENROUTER_API_KEY}
- OPENROUTER_MODEL=${OPENROUTER_MODEL}
depends_on:
diff --git a/server/.env.example b/server/.env.example
index 25db15c51..1d24cc812 100644
--- a/server/.env.example
+++ b/server/.env.example
@@ -18,6 +18,10 @@ REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
+# Shared dashboard cache lifetime, in seconds (0 disables; maximum 30).
+# Live counts, sessions, user details and custom SQL are not cached.
+DASHBOARD_CACHE_TTL_SECONDS=30
+
# Authentication
BETTER_AUTH_SECRET=your-secret-key-here
diff --git a/server/README.md b/server/README.md
index 8d6ecdcd0..8cc53e1ac 100644
--- a/server/README.md
+++ b/server/README.md
@@ -2,6 +2,41 @@
Self-hosted analytics backend using ClickHouse.
+## Dashboard response cache
+
+Dashboard aggregates share a Redis cache across users and backend workers. This
+applies to both standard queries and the lite endpoints backed by materialized
+views. No ClickHouse schema changes are required.
+
+`DASHBOARD_CACHE_TTL_SECONDS` defaults to `30`, is capped at 30 seconds, and can be
+set to `0` to disable caching and request coalescing. Successful JSON responses up
+to 256 KiB expire based on when their query started; reading a cached response
+does not extend its lifetime. Identical requests arriving while a query is running
+share that query, including across workers. Redis failures fall back to the
+original handler.
+
+Cached endpoints under `/api/sites/:siteId/`:
+
+- `overview`, `overview/time-series`, `overview-lite`, `overview-bucketed-lite`
+- `metric`, `metric-lite`, `page-titles`
+- `events/names`, `events/autocapture`, `events/outbound`
+
+Live user counts, sessions, user details, custom SQL, and all other endpoints
+remain uncached. Every request still runs access checks, time validation, and
+saved-segment expansion before looking up a cached result. Cache keys include the
+ClickHouse host/database, resolved Site, endpoint, and complete expanded query,
+so different time ranges, filters, timezones, and pagination cannot share results.
+
+For diagnostics, inspect the `X-Rybbit-Cache` response header: `MISS` ran the
+handler, `HIT` reused a response, and `COALESCED` shared work with an earlier
+request. Send `Cache-Control: no-cache` to bypass both caching and coalescing for
+a fresh read. Errors are only shared with requests already waiting for that
+query; later requests can retry immediately.
+
+This improves repeat loads and reduces database contention from simultaneous
+viewers. The first request for a new selection still pays the underlying query
+cost, so measure cold and warm loads separately when evaluating performance.
+
## Development
### Prerequisites
diff --git a/server/src/api/analytics/utils/dashboardCache.test.ts b/server/src/api/analytics/utils/dashboardCache.test.ts
new file mode 100644
index 000000000..53dfa0f58
--- /dev/null
+++ b/server/src/api/analytics/utils/dashboardCache.test.ts
@@ -0,0 +1,296 @@
+import Fastify, { FastifyInstance } from "fastify";
+import RedisMock from "ioredis-mock";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { createDashboardCache } from "./dashboardCache.js";
+
+const apps: FastifyInstance[] = [];
+const clients: InstanceType<typeof RedisMock>[] = [];
+let port = 16379;
+function mockRedis() {
+ const redis = new RedisMock({ port: port++ });
+ Object.defineProperty(redis, "status", { value: "ready", writable: true });
+ clients.push(redis);
+ return redis;
+}
+afterEach(async () => {
+ await Promise.all(apps.splice(0).map(app => app.close()));
+ clients.splice(0).forEach(redis => redis.disconnect());
+ vi.restoreAllMocks();
+});
+
+describe("dashboard response cache", () => {
+ it("shares one query across concurrent viewers on separate backend workers", async () => {
+ const redis = mockRedis();
+ const query = vi.fn(async () => {
+ await new Promise(resolve => setTimeout(resolve, 75));
+ return { data: { users: 42, sessions: 51 } };
+ });
+ const authorize = vi.fn(async () => {});
+ // Separate factories and Fastify instances: no shared in-process promises.
+ const workers = Array.from({ length: 2 }, () => {
+ const app = Fastify();
+ const cached = createDashboardCache({ redis, namespace: "test" });
+ app.get("/sites/:siteId/overview-lite", cached({ preHandler: [authorize] }), async () => query());
+ apps.push(app);
+ return app;
+ });
+ const results = await Promise.all(
+ Array.from({ length: 20 }, (_, i) =>
+ workers[i % 2].inject("/sites/1/overview-lite?past_minutes_start=1440&past_minutes_end=0")
+ )
+ );
+ expect(results.every(response => response.statusCode === 200)).toBe(true);
+ expect(results.map(response => response.json())).toEqual(Array(20).fill({ data: { users: 42, sessions: 51 } }));
+ expect(authorize).toHaveBeenCalledTimes(20);
+ expect(query).toHaveBeenCalledTimes(1);
+ });
+
+ it("checks authorization on every cache hit and uses expanded segment filters", async () => {
+ const redis = mockRedis();
+ const cached = createDashboardCache({ redis, namespace: "test" });
+ const app = Fastify();
+ apps.push(app);
+ let allowed = true;
+ let country = "KR";
+ const query = vi.fn(async () => ({ data: country }));
+ app.get(
+ "/sites/:siteId/overview",
+ cached({
+ preHandler: [
+ async (_request, reply) => {
+ if (!allowed) return reply.code(403).send({ error: "Forbidden" });
+ },
+ async request => {
+ (request.query as Record<string, unknown>).filters = [{ country }];
+ },
+ ],
+ }),
+ query
+ );
+ const url = "/sites/1/overview?segment=saved-segment";
+ expect((await app.inject(url)).json()).toEqual({ data: "KR" });
+ expect((await app.inject(url)).headers["x-rybbit-cache"]).toBe("HIT");
+ allowed = false;
+ expect((await app.inject(url)).statusCode).toBe(403);
+ expect(query).toHaveBeenCalledTimes(1);
+ allowed = true;
+ country = "US";
+ expect((await app.inject(url)).json()).toEqual({ data: "US" });
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("separates Sites, endpoints, filters, both time bounds, timezone, bucket and pagination", async () => {
+ const redis = mockRedis();
+ const cached = createDashboardCache({ redis, namespace: "test" });
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async request => ({
+ data: { site: request.params, query: request.query, call: query.mock.calls.length },
+ }));
+ app.get("/sites/:siteId/overview", cached({}), query);
+ app.get("/sites/:siteId/overview-lite", cached({}), query);
+ const base =
+ "/sites/1/overview?past_minutes_start=1440&past_minutes_end=0&time_zone=UTC&bucket=hour&page=1&parameter=country";
+ const changes = [
+ base.replace("/1/", "/2/"),
+ base.replace("overview?", "overview-lite?"),
+ base.replace("1440", "2880"),
+ base.replace("end=0", "end=60"),
+ base.replace("UTC", "Asia%2FKolkata"),
+ base.replace("hour", "day"),
+ base.replace("page=1", "page=2"),
+ base.replace("country", "device_type"),
+ base + "&filters=KR",
+ ];
+ const first = await app.inject(base);
+ expect((await app.inject(base)).payload).toBe(first.payload);
+ for (const url of changes) expect((await app.inject(url)).payload).not.toBe(first.payload);
+ expect(query).toHaveBeenCalledTimes(changes.length + 1);
+ });
+
+ it("shares equivalent query-string orders, but never caches across deployments", async () => {
+ const redis = mockRedis();
+ const query = vi.fn(async () => ({ data: 42 }));
+ for (const namespace of ["database-a", "database-b"]) {
+ const app = Fastify();
+ apps.push(app);
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace })({}), query);
+ await app.inject("/sites/1/overview?start_date=2026-09-19&end_date=2026-09-20");
+ expect(
+ (await app.inject("/sites/1/overview?end_date=2026-09-20&start_date=2026-09-19")).headers["x-rybbit-cache"]
+ ).toBe("HIT");
+ }
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("bounds freshness from query start and does not extend expiry on hits", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ let now = 1_000_000;
+ vi.spyOn(Date, "now").mockImplementation(() => now);
+ const query = vi.fn(async () => {
+ now += 400;
+ return { data: 42 };
+ });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test", ttlMs: 1000 })({}), query);
+ const url = "/sites/1/overview";
+ await app.inject(url);
+ const key = (await redis.keys("dashboard:v1:*")).find(key => !key.includes(":done:") && !key.endsWith(":lock"))!;
+ expect(await redis.pttl(key)).toBe(600);
+ now += 500;
+ expect((await app.inject(url)).headers["x-rybbit-cache"]).toBe("HIT");
+ expect(await redis.pttl(key)).toBe(100);
+ now += 101;
+ await app.inject(url);
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("does not cache a query that outlives the freshness budget", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ let now = 1_000_000;
+ vi.spyOn(Date, "now").mockImplementation(() => now);
+ const query = vi.fn(async () => {
+ now += 2000;
+ return { data: 42 };
+ });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test", ttlMs: 1000 })({}), query);
+ await app.inject("/sites/1/overview");
+ await app.inject("/sites/1/overview");
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("does not cache errors, and releases the lease so retries can succeed", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn().mockRejectedValueOnce(new Error("ClickHouse down")).mockResolvedValue({ data: 42 });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test" })({}), query);
+ expect((await app.inject("/sites/1/overview")).statusCode).toBe(500);
+ expect((await app.inject("/sites/1/overview")).json()).toEqual({ data: 42 });
+ expect((await app.inject("/sites/1/overview")).headers["x-rybbit-cache"]).toBe("HIT");
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("shares an in-flight failure with waiters without caching it for later requests", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async () => {
+ await new Promise(resolve => setTimeout(resolve, 75));
+ throw new Error("ClickHouse unavailable");
+ });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test" })({}), query);
+ const results = await Promise.all(Array.from({ length: 10 }, () => app.inject("/sites/1/overview")));
+ expect(results.every(response => response.statusCode === 500)).toBe(true);
+ expect(query).toHaveBeenCalledTimes(1);
+ await app.inject("/sites/1/overview");
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it.each(["disabled", "disconnected", "redis-error", "no-cache", "no-store"])(
+ "serves live results when %s",
+ async mode => {
+ const redis = mockRedis();
+ if (mode === "disconnected") Object.defineProperty(redis, "status", { value: "reconnecting" });
+ if (mode === "redis-error") vi.spyOn(redis, "eval").mockRejectedValue(new Error("Redis down"));
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async () => ({ data: 42 }));
+ app.get(
+ "/sites/:siteId/overview",
+ createDashboardCache({ redis, namespace: "test", ttlMs: mode === "disabled" ? 0 : 30_000 })({}),
+ query
+ );
+ for (let i = 0; i < 2; i++)
+ expect(
+ (
+ await app.inject({
+ url: "/sites/1/overview",
+ headers: mode.startsWith("no-") ? { "cache-control": mode } : {},
+ })
+ ).json()
+ ).toEqual({ data: 42 });
+ expect(query).toHaveBeenCalledTimes(2);
+ }
+ );
+
+ it("does not cache oversized responses", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async () => ({ data: "x".repeat(100) }));
+ app.get(
+ "/sites/:siteId/overview",
+ createDashboardCache({ redis, namespace: "test", maxResponseBytes: 50 })({}),
+ query
+ );
+ await app.inject("/sites/1/overview");
+ await app.inject("/sites/1/overview");
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("still serves a successful query when Redis fails during publication", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async () => ({ data: 42 }));
+ query.mockImplementationOnce(async () => {
+ vi.spyOn(redis, "eval").mockRejectedValueOnce(new Error("Redis write failed"));
+ return { data: 42 };
+ });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test" })({}), query);
+ const url = "/sites/1/overview";
+ expect((await app.inject(url)).json()).toEqual({ data: 42 });
+ expect((await app.inject(url)).json()).toEqual({ data: 42 });
+ expect((await app.inject(url)).headers["x-rybbit-cache"]).toBe("HIT");
+ expect(query).toHaveBeenCalledTimes(2);
+ });
+
+ it("prevents an expired lock owner from overwriting a newer result", async () => {
+ const redis = mockRedis();
+ let now = 1_000_000;
+ vi.spyOn(Date, "now").mockImplementation(() => now);
+ let finishFirst!: () => void;
+ const blocked = new Promise<void>(resolve => {
+ finishFirst = resolve;
+ });
+ const query = vi.fn(async () => {
+ if (query.mock.calls.length === 1) {
+ await blocked;
+ return { data: "old" };
+ }
+ return { data: "new" };
+ });
+ const app = Fastify();
+ apps.push(app);
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test", lockMs: 50 })({}), query);
+ const first = app.inject("/sites/1/overview").then(response => response.json());
+ await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(1));
+ // Simulate a paused/crashed worker whose heartbeat did not run in time.
+ now += 51;
+ expect((await app.inject("/sites/1/overview")).json()).toEqual({ data: "new" });
+ finishFirst();
+ expect(await first).toEqual({ data: "old" });
+ expect((await app.inject("/sites/1/overview")).json()).toEqual({ data: "new" });
+ });
+
+ it("renews the lease while a slow query is still running", async () => {
+ const redis = mockRedis();
+ const app = Fastify();
+ apps.push(app);
+ const query = vi.fn(async () => {
+ await new Promise(resolve => setTimeout(resolve, 180));
+ return { data: 42 };
+ });
+ app.get("/sites/:siteId/overview", createDashboardCache({ redis, namespace: "test", lockMs: 90 })({}), query);
+ const first = app.inject("/sites/1/overview").then(response => response.json());
+ await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(1));
+ await new Promise(resolve => setTimeout(resolve, 100));
+ const second = await app.inject("/sites/1/overview");
+ expect(await first).toEqual(second.json());
+ expect(query).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/server/src/api/analytics/utils/dashboardCache.ts b/server/src/api/analytics/utils/dashboardCache.ts
new file mode 100644
index 000000000..a15bafc24
--- /dev/null
+++ b/server/src/api/analytics/utils/dashboardCache.ts
@@ -0,0 +1,213 @@
+import { createHash, randomUUID } from "node:crypto";
+import { setTimeout as delay } from "node:timers/promises";
+import {
+ FastifyRequest,
+ onResponseHookHandler,
+ onSendHookHandler,
+ preHandlerHookHandler,
+ RouteShorthandOptions,
+} from "fastify";
+import { Redis } from "ioredis";
+
+export interface DashboardCacheOptions {
+ redis: Pick<Redis, "eval" | "status">;
+ namespace: string;
+ ttlMs?: number;
+ lockMs?: number;
+ maxResponseBytes?: number;
+}
+
+// The read/claim and publish/release operations are atomic across backend
+// workers. An expired owner must never overwrite a newer owner's response.
+const READ_OR_CLAIM = `
+ if ARGV[3] ~= '' then
+ local completed = redis.call('GET', KEYS[3])
+ if completed then return {'completed', completed} end
+ end
+ local value = redis.call('GET', KEYS[1])
+ if value then return {'hit', value} end
+ if redis.call('SET', KEYS[2], ARGV[1], 'NX', 'PX', ARGV[2]) then return {'owner'} end
+ return {'wait', redis.call('GET', KEYS[2])}
+`;
+const PUBLISH = `
+ if redis.call('GET', KEYS[2]) ~= ARGV[1] then return 0 end
+ if tonumber(ARGV[3]) > 0 then redis.call('SET', KEYS[1], ARGV[2], 'PX', ARGV[3]) end
+ redis.call('SET', KEYS[3], ARGV[4], 'PX', 5000)
+ redis.call('DEL', KEYS[2])
+ return 1
+`;
+const RELEASE = `
+ if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end
+ return 0
+`;
+const RENEW = `
+ if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('PEXPIRE', KEYS[1], ARGV[2]) end
+ return 0
+`;
+
+function canonical(value: unknown): unknown {
+ if (Array.isArray(value)) return value.map(canonical);
+ if (value && typeof value === "object") {
+ return Object.fromEntries(
+ Object.entries(value)
+ .sort(([a], [b]) => a.localeCompare(b))
+ .map(([key, item]) => [key, canonical(item)])
+ );
+ }
+ return value;
+}
+
+const hooks = <T>(hook: T | T[] | undefined): T[] => (hook === undefined ? [] : Array.isArray(hook) ? hook : [hook]);
+
+export function createDashboardCache({
+ redis,
+ namespace,
+ ttlMs = 30_000,
+ lockMs = 5_000,
+ maxResponseBytes = 256 * 1024,
+}: DashboardCacheOptions) {
+ const owners = new WeakMap<
+ FastifyRequest,
+ { key: string; lock: string; token: string; startedAt: number; heartbeat: NodeJS.Timeout }
+ >();
+
+ const release: onResponseHookHandler = async request => {
+ const owner = owners.get(request);
+ if (!owner) return;
+ clearInterval(owner.heartbeat);
+ owners.delete(request);
+ try {
+ await redis.eval(RELEASE, 1, owner.lock, owner.token);
+ } catch {
+ // The lease expires even if Redis is unavailable during cleanup.
+ }
+ };
+
+ const read: preHandlerHookHandler = async (request, reply) => {
+ // This hook is appended AFTER access checks, time validation and segment
+ // expansion. Cache identity uses the resolved Site and full expanded query.
+ if (
+ ttlMs <= 0 ||
+ request.method !== "GET" ||
+ redis.status !== "ready" ||
+ /(?:no-cache|no-store|max-age=0)/i.test(String(request.headers["cache-control"] || ""))
+ )
+ return;
+
+ const digest = createHash("sha256")
+ .update(
+ JSON.stringify(
+ canonical({
+ route: request.routeOptions.url,
+ params: request.params,
+ query: request.query,
+ })
+ )
+ )
+ .digest("hex");
+ const key = `dashboard:v1:{${namespace}:${digest}}`;
+ const lock = `${key}:lock`;
+ const token = randomUUID();
+ const deadline = Date.now() + 70_000;
+ let waited = false;
+ let waitingFor = "";
+
+ try {
+ while (!reply.raw.destroyed && !request.raw.aborted && Date.now() < deadline) {
+ const [result, payload] = (await redis.eval(
+ READ_OR_CLAIM,
+ 3,
+ key,
+ lock,
+ `${key}:done:${waitingFor}`,
+ token,
+ lockMs,
+ waitingFor
+ )) as string[];
+ if (result === "completed") {
+ const completion = JSON.parse(payload);
+ if (completion.retry) return;
+ reply.header("X-Rybbit-Cache", "COALESCED");
+ return reply.code(completion.status).type(completion.type).send(completion.payload);
+ }
+ if (result === "hit") {
+ reply.header("X-Rybbit-Cache", waited ? "COALESCED" : "HIT");
+ return reply.type("application/json; charset=utf-8").send(payload);
+ }
+ if (result === "owner") {
+ // A live slow query renews its short lease. A crashed worker leaves
+ // followers waiting at most one lease, rather than the query timeout.
+ let renewing = false;
+ const heartbeat = setInterval(
+ async () => {
+ if (renewing) return;
+ renewing = true;
+ try {
+ if (reply.raw.destroyed || request.raw.aborted || !(await redis.eval(RENEW, 1, lock, token, lockMs)))
+ clearInterval(heartbeat);
+ } catch {
+ // An unavailable Redis cannot prevent the original query from
+ // completing. Publication still verifies ownership afterwards.
+ } finally {
+ renewing = false;
+ }
+ },
+ Math.max(10, Math.floor(lockMs / 3))
+ );
+ heartbeat.unref();
+ owners.set(request, { key, lock, token, startedAt: Date.now(), heartbeat });
+ reply.header("X-Rybbit-Cache", "MISS");
+ return;
+ }
+ waited = true;
+ waitingFor = payload;
+ await delay(50);
+ }
+ } catch (error) {
+ request.log.debug({ err: error }, "Dashboard cache unavailable; querying directly");
+ }
+ // Cache availability never decides whether analytics can be served.
+ };
+
+ const write: onSendHookHandler = async (request, reply, payload) => {
+ const owner = owners.get(request);
+ if (!owner) return payload;
+ clearInterval(owner.heartbeat);
+ // Count freshness from query start, not completion: a slow query must not
+ // add its execution time on top of the promised cache lifetime.
+ const remaining = owner.startedAt + ttlMs - Date.now();
+ const type = String(reply.getHeader("content-type") || "");
+ const shareable =
+ type.startsWith("application/json") &&
+ typeof payload === "string" &&
+ Buffer.byteLength(payload) <= maxResponseBytes;
+ const cacheTtl = shareable && reply.statusCode === 200 ? Math.max(Math.floor(remaining), 0) : 0;
+ // Only callers already waiting for this owner's token can receive an error
+ // completion. New requests retry normally; failures never enter the cache.
+ const completion = JSON.stringify(shareable ? { status: reply.statusCode, type, payload } : { retry: true });
+ try {
+ await redis.eval(
+ PUBLISH,
+ 3,
+ owner.key,
+ owner.lock,
+ `${owner.key}:done:${owner.token}`,
+ owner.token,
+ typeof payload === "string" ? payload : "",
+ cacheTtl,
+ completion
+ );
+ owners.delete(request);
+ } catch (error) {
+ request.log.debug({ err: error }, "Could not cache dashboard response");
+ }
+ return payload;
+ };
+
+ return <T extends RouteShorthandOptions>(options: T): T => ({
+ ...options,
+ preHandler: [...hooks(options.preHandler), read],
+ onSend: [...hooks(options.onSend), write],
+ onResponse: [...hooks(options.onResponse), release],
+ });
+}
diff --git a/server/src/db/redis/redis.ts b/server/src/db/redis/redis.ts
index 1d16044f0..9d45f6825 100644
--- a/server/src/db/redis/redis.ts
+++ b/server/src/db/redis/redis.ts
@@ -45,6 +45,9 @@ function createRedisClient(label: string): Redis {
// general use.
export const redis = createRedisClient("main");
+// Dashboard JSON responses must not queue behind tracking/session commands.
+export const dashboardCacheRedis = createRedisClient("dashboard-cache");
+
// Dedicated connection for per-event session resolution. Isolated from `redis`
// so a session GET/SET is never head-of-line blocked behind the anomaly scorer's
// Lua call — that blocking timed sessions out, and a timed-out session forced the
diff --git a/server/src/index.ts b/server/src/index.ts
index a2f6ea002..08f71ef1f 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -1,4 +1,5 @@
import cluster from "node:cluster";
+import { createHash } from "node:crypto";
import cors from "@fastify/cors";
import rateLimit from "@fastify/rate-limit";
import fastifyStatic from "@fastify/static";
@@ -180,10 +181,11 @@ import {
unsubscribeMarketing,
updateAccountSettings,
} from "./api/user/index.js";
+import { createDashboardCache } from "./api/analytics/utils/dashboardCache.js";
import { validateHttpTimeParams } from "./api/analytics/utils/query-validation.js";
import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";
import { unclaimedSiteRouteOptions } from "./api/sites/createUnclaimedSite.js";
-import { apiRateLimitRedis } from "./db/redis/redis.js";
+import { apiRateLimitRedis, dashboardCacheRedis } from "./db/redis/redis.js";
import { initPostgres } from "./db/postgres/initPostgres.js";
import {
allowPublicSiteAccess,
@@ -267,8 +269,18 @@ const authOnlyScoped = (resource: ScopeResource, action: ScopeAction) => ({
// Reused scoped chains
const publicAnalyticsRead = publicSiteScoped("analytics", "read");
+const dashboardCacheSeconds = Number(process.env.DASHBOARD_CACHE_TTL_SECONDS ?? 30);
+const cacheDashboard = createDashboardCache({
+ redis: dashboardCacheRedis,
+ namespace: createHash("sha256")
+ .update(JSON.stringify([process.env.CLICKHOUSE_HOST, process.env.CLICKHOUSE_DB]))
+ .digest("hex"),
+ ttlMs: (Number.isFinite(dashboardCacheSeconds) ? Math.max(0, Math.min(30, dashboardCacheSeconds)) : 30) * 1000,
+});
+const cachedAnalyticsRead = cacheDashboard(publicAnalyticsRead);
const publicSessionsRead = publicSiteScoped("sessions", "read");
const publicEventsRead = publicSiteScoped("events", "read");
+const cachedEventsRead = cacheDashboard(publicEventsRead);
const publicUsersRead = publicSiteScoped("users", "read");
const publicFunnelsRead = publicSiteScoped("funnels", "read");
const publicGoalsRead = publicSiteScoped("goals", "read");
@@ -425,13 +437,13 @@ async function analyticsRoutes(fastify: FastifyInstance) {
// This endpoint gets called a lot so we don't want to log it
fastify.get("/sites/:siteId/live-user-count", { logLevel: "silent", ...publicAnalyticsRead }, getLiveUsercount);
- fastify.get("/sites/:siteId/overview", publicAnalyticsRead, getOverview);
- fastify.get("/sites/:siteId/overview/time-series", publicAnalyticsRead, getOverviewBucketed);
- fastify.get("/sites/:siteId/overview-lite", publicAnalyticsRead, getOverviewLite);
- fastify.get("/sites/:siteId/overview-bucketed-lite", publicAnalyticsRead, getOverviewBucketedLite);
- fastify.get("/sites/:siteId/metric-lite", publicAnalyticsRead, getMetricLite);
- fastify.get("/sites/:siteId/metric", publicAnalyticsRead, getMetric);
- fastify.get("/sites/:siteId/page-titles", publicAnalyticsRead, getPageTitles);
+ fastify.get("/sites/:siteId/overview", cachedAnalyticsRead, getOverview);
+ fastify.get("/sites/:siteId/overview/time-series", cachedAnalyticsRead, getOverviewBucketed);
+ fastify.get("/sites/:siteId/overview-lite", cachedAnalyticsRead, getOverviewLite);
+ fastify.get("/sites/:siteId/overview-bucketed-lite", cachedAnalyticsRead, getOverviewBucketedLite);
+ fastify.get("/sites/:siteId/metric-lite", cachedAnalyticsRead, getMetricLite);
+ fastify.get("/sites/:siteId/metric", cachedAnalyticsRead, getMetric);
+ fastify.get("/sites/:siteId/page-titles", cachedAnalyticsRead, getPageTitles);
fastify.get("/sites/:siteId/errors/names", publicAnalyticsRead, getErrorNames);
fastify.get("/sites/:siteId/errors/events", publicAnalyticsRead, getErrorEvents);
fastify.get("/sites/:siteId/errors/time-series", publicAnalyticsRead, getErrorBucketed);
@@ -501,11 +513,11 @@ async function analyticsRoutes(fastify: FastifyInstance) {
fastify.put("/sites/:siteId/experiments/:experimentId", adminExperimentsWrite, updateExperiment);
fastify.delete("/sites/:siteId/experiments/:experimentId", adminExperimentsWrite, deleteExperiment);
fastify.get("/sites/:siteId/experiments/:experimentId/results", authExperimentsRead, getExperimentResults);
- fastify.get("/sites/:siteId/events/names", publicEventsRead, getEventNames);
+ fastify.get("/sites/:siteId/events/names", cachedEventsRead, getEventNames);
fastify.get("/sites/:siteId/events/properties", publicEventsRead, getEventProperties);
- fastify.get("/sites/:siteId/events/autocapture", publicEventsRead, getAutocaptureEvents);
+ fastify.get("/sites/:siteId/events/autocapture", cachedEventsRead, getAutocaptureEvents);
fastify.get("/sites/:siteId/events/autocapture-values", publicEventsRead, getAutocaptureValues);
- fastify.get("/sites/:siteId/events/outbound", publicEventsRead, getOutboundLinks);
+ fastify.get("/sites/:siteId/events/outbound", cachedEventsRead, getOutboundLinks);
fastify.get("/org-event-count/:organizationId", orgAnalyticsRead, getOrgEventCount);
fastify.post(
"/organizations/:organizationId/analytics/query",