Skip to content

Add configurable redis TTL on realtime streams #1725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
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
5 changes: 5 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ const EnvironmentSchema = z.object({
MAX_BATCH_AND_WAIT_V2_TRIGGER_ITEMS: z.coerce.number().int().default(500),

REALTIME_STREAM_VERSION: z.enum(["v1", "v2"]).default("v1"),
REALTIME_STREAM_MAX_LENGTH: z.coerce.number().int().default(1000),
REALTIME_STREAM_TTL: z.coerce
.number()
.int()
.default(60 * 60 * 24), // 1 day in seconds
BATCH_METADATA_OPERATIONS_FLUSH_INTERVAL_MS: z.coerce.number().int().default(1000),
BATCH_METADATA_OPERATIONS_FLUSH_ENABLED: z.string().default("1"),
BATCH_METADATA_OPERATIONS_FLUSH_LOGGING_ENABLED: z.string().default("1"),
Expand Down
25 changes: 23 additions & 2 deletions apps/webapp/app/services/realtime/redisRealtimeStreams.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthenticatedEnvironment } from "../apiAuth.server";
import { logger } from "../logger.server";
import { StreamIngestor, StreamResponder } from "./types";
import { LineTransformStream } from "./utils.server";
import { env } from "~/env.server";

export type RealtimeStreamsOptions = {
redis: RedisOptions | undefined;
Expand Down Expand Up @@ -152,10 +153,30 @@ export class RedisRealtimeStreams implements StreamIngestor, StreamResponder {
value,
});

await redis.xadd(streamKey, "MAXLEN", "~", "1000", "*", "data", value);
await redis.xadd(
streamKey,
"MAXLEN",
"~",
String(env.REALTIME_STREAM_MAX_LENGTH),
"*",
"data",
value
);
}

await redis.xadd(streamKey, "MAXLEN", "~", "1000", "*", "data", END_SENTINEL);
// Send the END_SENTINEL and set TTL with a pipeline.
const pipeline = redis.pipeline();
pipeline.xadd(
streamKey,
"MAXLEN",
"~",
String(env.REALTIME_STREAM_MAX_LENGTH),
"*",
"data",
END_SENTINEL
);
pipeline.expire(streamKey, env.REALTIME_STREAM_TTL);
await pipeline.exec();

return new Response(null, { status: 200 });
} catch (error) {
Expand Down
Loading