Skip to content

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

fix(reminders): give each reminder firing a random event id

Calvin8fe394f32711
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 97327c689b..e9af202a0d 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -814,10 +814,11 @@ await someDAL.transaction(async (tx) => {
- `emit` writes a batch in one go (eg one event per secret in a bulk write), in chunks of 1000 rows to
stay under Postgres's bind parameter limit.
- Every event carries its UUID `id` from the moment it is built, and every consumer's row for it stores
- it as `eventId`. A consumer reports its results by that id. `(consumer, eventId)` is unique, so an
- emitter that needs a retry dropped derives the id instead of generating it (the secret reminder uses
- `uuid` v5 over the reminder and the date). `eventId` is nullable only in the table, because a pod on
- the previous release writes rows without it during a rolling deploy; `claimBatch` gives those rows one.
+ it as `eventId`. A consumer reports its results by that id, so `(consumer, eventId)` is unique and a
+ repeated insert is dropped. Generate ids with `crypto.randomUUID()` (`defineEvent`'s `build` does):
+ deriving one from the entity and a date silently drops a legitimate repeat (eg a reminder moved back
+ onto a date it already fired on). `eventId` is nullable only in the table, because a pod on the
+ previous release writes rows without it during a rolling deploy; `claimBatch` gives those rows one.
**How delivery works:**
@@ -862,8 +863,7 @@ await someDAL.transaction(async (tx) => {
don't add a partition key back until a consumer needs one: no consumer today depends on the order two
events for one resource arrive in, and each event is idempotent on its own through alert history.
- **Delivery is at-least-once.** `commitResults` is retried in-process, since by then the consumer has
- already sent; what's left is narrowed by the consumer's own delivery records and by an emitter that
- derives its event id.
+ already sent; what's left is narrowed by the consumer's own delivery records.
**Watch `infisical.event_outbox.oldest_pending_age`.** It catches a dead relay, a wedged consumer and a
stuck claim alike. `lag` and `exhausted.count` are recorded by the outbox, labelled by consumer, so a
diff --git a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
index 4120781d10..fbaa19ebdc 100644
--- a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
+++ b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
@@ -4,9 +4,8 @@ import { TableName } from "../schemas";
const EVENT_ID_INDEX = `${TableName.EventOutbox}_event_id_idx`;
-// The CloudEvents `id`, shared by every consumer's row for one event. It replaces `idempotencyKey`: an
-// emitter that needs a retry deduplicated derives the id instead of passing a key. Both stay for one
-// release, because a pod still on the previous release inserts into `idempotencyKey` and names its
+// The CloudEvents `id`, shared by every consumer's row for one event. It replaces `idempotencyKey`. Both
+// stay for one release, because a pod still on the previous release inserts into `idempotencyKey` and names its
// unique index in ON CONFLICT during a rolling deploy; dropping either would fail that insert along with
// the write it belongs to. For the same reason `eventId` is nullable with no default: `claimBatch` gives
// those rows one.
diff --git a/backend/src/services/event-outbox/event-outbox-dal.test.ts b/backend/src/services/event-outbox/event-outbox-dal.test.ts
index 30b28cfc45..cc793b3cf8 100644
--- a/backend/src/services/event-outbox/event-outbox-dal.test.ts
+++ b/backend/src/services/event-outbox/event-outbox-dal.test.ts
@@ -109,7 +109,7 @@ const buildDAL = (opts?: { returning?: unknown[]; updated?: number; selected?: u
};
describe("event outbox dal", () => {
- // An emitter that derives its event id (eg a reminder occurrence) relies on this to drop a retry.
+ // Results are keyed by event id, so a consumer must never hold two rows for one event.
test("insertEvents ignores an event the consumer already has", async () => {
const { dal, calls, tx } = buildDAL();
diff --git a/backend/src/services/event-outbox/event-outbox-types.ts b/backend/src/services/event-outbox/event-outbox-types.ts
index e42c044230..877c64e2d6 100644
--- a/backend/src/services/event-outbox/event-outbox-types.ts
+++ b/backend/src/services/event-outbox/event-outbox-types.ts
@@ -46,8 +46,8 @@ const keySchema = z
"Must be lowercase and dot-namespaced, e.g. 'pki.certificate.issued'"
);
-// `id` is the CloudEvents id. It is fixed when the event is built rather than at insert, so an emitter
-// that needs a retry to be recognised as the same event can derive it (eg a reminder occurrence).
+// `id` is the CloudEvents id. It is set when the event is built, so every consumer's row for the event
+// carries the same one.
export const EventInputSchema = z.object({
id: z.string().uuid(),
type: keySchema,
diff --git a/backend/src/services/reminder/reminder-events.ts b/backend/src/services/reminder/reminder-events.ts
index 6344b97ce3..eb897acce3 100644
--- a/backend/src/services/reminder/reminder-events.ts
+++ b/backend/src/services/reminder/reminder-events.ts
@@ -1,6 +1,7 @@
+import crypto from "node:crypto";
+
import { Knex } from "knex";
import RE2 from "re2";
-import { v5 as uuidv5 } from "uuid";
import { z } from "zod";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
@@ -8,9 +9,6 @@ import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
export const SECRET_REMINDER_RESOURCE_TYPE = "secret.reminder";
export const SECRET_REMINDER_DUE_EVENT = "secret.reminder.due";
-// Namespaces the event ids derived below, so they can't collide with ids derived the same way elsewhere.
-const SECRET_REMINDER_DUE_EVENT_ID_NAMESPACE = "18460d3b-7d67-44a8-a6fb-1f84f78f29c4";
-
export const SecretReminderDuePayloadSchema = z.object({
note: z.string().max(1024).nullish(),
repeatDays: z.number().int().min(1).nullish(),
@@ -36,10 +34,10 @@ export const emitSecretReminderDue = (
eventEmitter.emit(
[
{
- // Derived rather than random, so a cron retry after a partial run is the same event and the outbox
- // drops it. Derived from the reminder rather than the secret, so a reminder deleted and set again
- // for a date that already fired still sends.
- id: uuidv5(`${reminderId}:${occurrenceDate}`, SECRET_REMINDER_DUE_EVENT_ID_NAMESPACE),
+ // Random, not derived from the reminder and date. The dispatch transaction already stops one run from
+ // firing an occurrence twice, and a derived id would make the outbox drop a reminder the user moved
+ // back onto a date it already fired on.
+ id: crypto.randomUUID(),
type: SECRET_REMINDER_DUE_EVENT,
data: {
orgId,
diff --git a/backend/src/services/reminder/reminder-service.test.ts b/backend/src/services/reminder/reminder-service.test.ts
index 482f00e9f6..8ff2a1786a 100644
--- a/backend/src/services/reminder/reminder-service.test.ts
+++ b/backend/src/services/reminder/reminder-service.test.ts
@@ -121,22 +121,17 @@ describe("reminder dispatch", () => {
});
// A cron retry after a partial run must be the same event, so the outbox drops it.
- test("gives one occurrence of a reminder the same event id every time it is emitted", async () => {
+ // The outbox drops an event id a consumer already has, so a reminder moved back onto a date it already
+ // fired on would never send again if its id were derived from the reminder and the date.
+ test("gives every firing its own event id, even for a date the reminder already fired on", async () => {
const first = buildService({ due: [reminder()] });
- const retry = buildService({ due: [reminder()] });
- const otherDate = buildService({ due: [reminder({ nextReminderDate: new Date("2026-10-09T00:00:00.000Z") })] });
- const otherReminder = buildService({ due: [reminder({ id: "rem-2" })] });
+ const movedBack = buildService({ due: [reminder()] });
await first.service.dispatchDueReminders({ now });
- await retry.service.dispatchDueReminders({ now });
- await otherDate.service.dispatchDueReminders({ now });
- await otherReminder.service.dispatchDueReminders({ now });
-
- expect(first.emitted[0].id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/);
- expect(retry.emitted[0].id).toBe(first.emitted[0].id);
- expect(otherDate.emitted[0].data.occurrenceDate).toBe("2026-10-09");
- expect(otherDate.emitted[0].id).not.toBe(first.emitted[0].id);
- expect(otherReminder.emitted[0].id).not.toBe(first.emitted[0].id);
+ await movedBack.service.dispatchDueReminders({ now });
+
+ expect(first.emitted[0].id).toMatch(/^[0-9a-f-]{36}$/);
+ expect(movedBack.emitted[0].id).not.toBe(first.emitted[0].id);
});
test("deletes a one-time reminder once its event is emitted", async () => {