Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

reduce retention window

Thiago Araujo da Silva64fe85ba3f85
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
index 0aabf65508..48cbd368af 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
@@ -4,6 +4,7 @@ import { TDbClient } from "@app/db";
import { TableName, TAuditLogs } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { chunkArray } from "@app/lib/fn";
+import { logger } from "@app/lib/logger";
import {
AuditLogStreamOutboxStatus,
@@ -280,6 +281,11 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
total += deleted;
if (deleted < batchSize) break;
+ if (i === maxBatches - 1) {
+ logger.warn(
+ `audit-log-stream-outbox: deleteDeliveredOlderThan hit the batch ceiling [maxBatches=${maxBatches}] [batchSize=${batchSize}] [deleted=${total}] — eligible rows remain; cleanup is not keeping up with delivered-row inflow`
+ );
+ }
}
return total;
} catch (error) {
@@ -303,6 +309,11 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
total += deleted;
if (deleted < batchSize) break;
+ if (i === maxBatches - 1) {
+ logger.warn(
+ `audit-log-stream-outbox: deleteDlqOlderThan hit the batch ceiling [maxBatches=${maxBatches}] [batchSize=${batchSize}] [deleted=${total}] — expired DLQ entries remain unpruned`
+ );
+ }
}
return total;
} catch (error) {
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-queue.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-queue.ts
index fe58a2db5d..7f1ee314cb 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-queue.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-queue.ts
@@ -10,10 +10,15 @@ import { TAuditLogStreamOutboxServiceFactory } from "./audit-log-stream-outbox-s
const STALE_CLAIM_SWEEPER_CRON = "*/5 * * * *";
const STALE_CLAIM_SWEEPER_RUN_HASH_TTL_S = 60 * 60;
-// Cleanup cadence. Runs hourly to prune both 'delivered' outbox rows and DLQ
-// entries past their respective retention windows
-const CLEANUP_CRON = "0 * * * *";
-const CLEANUP_RUN_HASH_TTL_S = 24 * 60 * 60;
+// Cleanup cadence. Runs every 15 min to prune both 'delivered' outbox rows and DLQ
+// entries past their respective retention windows. Tighter than the old hourly cadence
+// so deletes come in smaller, more frequent bursts (less autovacuum/bloat pressure) and
+// the outbox table tracks the now-shorter delivered retention closely.
+const CLEANUP_CRON = "*/15 * * * *";
+// Per-fire hash dedup window — only needs to outlive a single run + retries, so 1h is
+// ample for a 15-min cron (the hash id is keyed per scheduled fire, so this never gates
+// future fires).
+const CLEANUP_RUN_HASH_TTL_S = 60 * 60;
export type TAuditLogStreamOutboxQueueDep = {
queueService: TQueueServiceFactory;
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
index f79e188501..0284d2edb1 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
@@ -405,9 +405,10 @@ describe("audit-log-stream-outbox-service pruneDeliveredRows", () => {
expect(auditLogStreamOutboxDAL.deleteDeliveredOlderThan).toHaveBeenCalledTimes(1);
const [retentionMs] = auditLogStreamOutboxDAL.deleteDeliveredOlderThan.mock.calls[0];
- // Retention must be positive and at least an hour — guards against accidental
- // tightening that would defeat the dedup-window purpose of keeping delivered rows.
- expect(retentionMs).toBeGreaterThanOrEqual(60 * 60_000);
+ // Retention must comfortably outlive the ~5s ingest-stream trim window so the
+ // delivered rows still serve their dedup-guard purpose — guards against accidental
+ // tightening down toward that window.
+ expect(retentionMs).toBeGreaterThanOrEqual(5 * 60_000);
});
});
@@ -420,7 +421,8 @@ describe("audit-log-stream-outbox-service pruneDlqEntries", () => {
expect(auditLogStreamOutboxDAL.deleteDlqOlderThan).toHaveBeenCalledTimes(1);
const [retentionMs] = auditLogStreamOutboxDAL.deleteDlqOlderThan.mock.calls[0];
- // DLQ retention should give operators at least a day to triage.
- expect(retentionMs).toBeGreaterThanOrEqual(24 * 60 * 60_000);
+ // DLQ retention should give operators a meaningful window (at least several hours)
+ // to notice and triage a wedged provider.
+ expect(retentionMs).toBeGreaterThanOrEqual(12 * 60 * 60_000);
});
});
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
index bb63da5047..9296c38d89 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
@@ -30,9 +30,10 @@ const MAX_ATTEMPTS = 5;
// POSTs. Dispatch them in parallel waves of this size instead — bounded so we don't
// open BATCH_SIZE concurrent connections to a legacy webhook receiver at once.
const SINGLE_MODE_SEND_CONCURRENCY = 5;
-// Exponential backoff with jitter, capped — keeps a wedged provider from hot-looping.
+// Exponential backoff with jitter — keeps a wedged provider from hot-looping. The
+// exponent is clamped (see computeBackoffMs) so the base can't run away if MAX_ATTEMPTS
+// is ever raised; with the current MAX_ATTEMPTS the schedule tops out at ~240s.
const BACKOFF_BASE_MS = 30_000;
-const BACKOFF_MAX_MS = 15 * 60_000;
// Stale-claim sweeper threshold: a single flush job's worst-case legitimate
// hold time is MAX_BATCHES_PER_JOB × AUDIT_LOG_STREAM_BATCH_TIMEOUT ≈ 5 min.
@@ -42,14 +43,16 @@ export const STALE_CLAIM_THRESHOLD_MS = 10 * 60_000;
// Retention for 'delivered' outbox rows. The row is the dedup guard against a
// re-fanout from the ingest consumer (e.g. after a Redis streamTrim failure),
// so the retention only needs to outlive how long an ingest-stream entry can
-// stay un-trimmed in practice. 1h is comfortably above that for any realistic
-// Redis/DB blip; the cleanup cron runs hourly so rows live ~1–2h in steady state.
-export const DELIVERED_RETENTION_MS = 60 * 60_000;
+// stay un-trimmed in practice. The ingest stream trims every ~5s, so 15m is
+// already 100×+ that window; the unique (streamId, auditLogId) constraint is the
+// hard dedup guarantee, this is just the belt-and-suspenders margin. Kept short
+// so the 'delivered' rows (the table's hottest section) don't pile up.
+export const DELIVERED_RETENTION_MS = 15 * 60_000;
// Retention for DLQ entries. Long enough for an operator to notice + triage a
// wedged provider via dashboard, short enough that the table doesn't grow
// without bound. Same cron prunes these alongside delivered rows.
-export const DLQ_RETENTION_MS = 24 * 60 * 60_000;
+export const DLQ_RETENTION_MS = 12 * 60 * 60_000;
const PROVIDER_QUEUE_MAP: Record<LogProvider, QueueName> = {
[LogProvider.Azure]: QueueName.AuditLogStreamAzure,
@@ -61,7 +64,7 @@ const PROVIDER_QUEUE_MAP: Record<LogProvider, QueueName> = {
const computeBackoffMs = (attemptsAfterIncrement: number): number => {
const exponent = Math.min(attemptsAfterIncrement - 1, 10);
- const base = Math.min(BACKOFF_BASE_MS * 2 ** exponent, BACKOFF_MAX_MS);
+ const base = BACKOFF_BASE_MS * 2 ** exponent;
const jitter = Math.floor(Math.random() * Math.min(base / 2, 30_000));
return base + jitter;
};