Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(cdp): always fair-dequeue the email queue (#65976)

Meikel Ratz526058016bfc
diff --git a/nodejs/src/cdp/config.ts b/nodejs/src/cdp/config.ts
index 6f7b2b4085a5..23bff73cc42c 100644
--- a/nodejs/src/cdp/config.ts
+++ b/nodejs/src/cdp/config.ts
@@ -91,12 +91,6 @@ export type CdpConfig = ClickhouseConfig & {
CDP_SES_RATE_LIMIT_CAPACITY: number
CDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS: number
- // When true, the email worker dequeues ordered by `dequeue_seq` (per-team
- // round-robin) instead of FIFO. `dequeue_seq` is always assigned at insert
- // (cheap), so flipping this on/off is purely a worker-side decision —
- // rollback is a single env-var change with no SQL revert needed.
- CDP_CYCLOTRON_EMAIL_FAIR_DEQUEUE: boolean
-
CDP_EVENT_PROCESSOR_EXECUTE_FIRST_STEP: boolean
CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN: string
CDP_FETCH_RETRIES: number
@@ -215,8 +209,6 @@ export function getDefaultCdpConfig(): CdpConfig {
CDP_SES_RATE_LIMIT_CAPACITY: 50,
CDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS: 250,
- CDP_CYCLOTRON_EMAIL_FAIR_DEQUEUE: false,
-
CDP_EVENT_PROCESSOR_EXECUTE_FIRST_STEP: true,
CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN: '',
CDP_FETCH_RETRIES: 3,
diff --git a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
index 8b510c3ad784..ffdf4b498a02 100644
--- a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
+++ b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts
@@ -996,8 +996,10 @@ describe('Cyclotron V2', () => {
})
describe('Worker: fairDequeue ordering', () => {
+ // The email queue is intrinsically fair-dequeued — the worker derives
+ // it from the queue name, so an EMAIL_QUEUE worker is already fair.
const createFairWorker = (overrides?: Record<string, unknown>): CyclotronV2Worker =>
- createWorker(EMAIL_QUEUE, { fairDequeue: true, ...overrides })
+ createWorker(EMAIL_QUEUE, overrides)
it('picks small-tenant jobs into the same batch as big-tenant jobs', async () => {
// The 2M-vs-1 scenario at a smaller scale: team A enqueues 5,
@@ -1153,21 +1155,19 @@ describe('Cyclotron V2', () => {
])
})
- it('preserves FIFO when fairDequeue is false (default)', async () => {
- // Same scenario, but with the flag off, all team-A jobs come first.
+ it('keeps non-email queues on FIFO (priority, scheduled) ordering', async () => {
+ // Fair dequeue is intrinsic to the email queue; a non-email
+ // queue worker stays strict FIFO. Team A enqueues 5 then team B
+ // enqueues 1 on the default (hog) queue — A's 5 come first.
const teamA = 100
const teamB = 200
- await manager.bulkCreateJobs(
- Array.from({ length: 5 }, () => ({ teamId: teamA, queueName: EMAIL_QUEUE }))
- )
- await manager.bulkCreateJobs([{ teamId: teamB, queueName: EMAIL_QUEUE }])
+ await manager.bulkCreateJobs(Array.from({ length: 5 }, () => ({ teamId: teamA, queueName: QUEUE })))
+ await manager.bulkCreateJobs([{ teamId: teamB, queueName: QUEUE }])
- const worker = createWorker(EMAIL_QUEUE, { batchMaxSize: 2 })
+ const worker = createWorker(QUEUE, { batchMaxSize: 2 })
const jobs = await dequeueOneBatch(worker)
expect(jobs).toHaveLength(2)
- // FIFO: both jobs are team A (their dequeue_seq is ignored by the
- // ordering, scheduled-time wins, A came first).
expect(jobs.every((j) => j.teamId === teamA)).toBe(true)
})
diff --git a/nodejs/src/cdp/services/cyclotron-v2/types.ts b/nodejs/src/cdp/services/cyclotron-v2/types.ts
index 59e678726049..c2a2642579e4 100644
--- a/nodejs/src/cdp/services/cyclotron-v2/types.ts
+++ b/nodejs/src/cdp/services/cyclotron-v2/types.ts
@@ -85,13 +85,6 @@ export type CyclotronV2WorkerConfig = {
pollDelayMs?: number
heartbeatTimeoutMs?: number
includeEmptyBatches?: boolean
- /**
- * When true, dequeue orders by `dequeue_seq ASC NULLS FIRST` (per-team
- * round-robin via the sort key assigned at insert time) instead of the
- * default `priority, scheduled` FIFO. Intended for the email queue
- * specifically; non-email queues should leave this off.
- */
- fairDequeue?: boolean
}
export type CyclotronV2JanitorConfig = {
diff --git a/nodejs/src/cdp/services/cyclotron-v2/worker.ts b/nodejs/src/cdp/services/cyclotron-v2/worker.ts
index 82dcc0e0a8cd..ef3795252692 100644
--- a/nodejs/src/cdp/services/cyclotron-v2/worker.ts
+++ b/nodejs/src/cdp/services/cyclotron-v2/worker.ts
@@ -55,7 +55,11 @@ export class CyclotronV2Worker {
this.pollDelayMs = config.pollDelayMs ?? 50
this.heartbeatTimeoutMs = config.heartbeatTimeoutMs ?? 30000
this.includeEmptyBatches = config.includeEmptyBatches ?? false
- this.fairDequeue = config.fairDequeue ?? false
+ // Fair (per-team round-robin) dequeue is the email queue's ordering.
+ // `dequeue_seq` is only ever assigned for email jobs (see
+ // CyclotronV2Manager), so deriving this from the queue name keeps the
+ // worker's ORDER BY in lockstep with where the sort key actually exists.
+ this.fairDequeue = config.queueName === 'email'
}
async connect(processBatch: (jobs: CyclotronV2DequeuedJob[]) => Promise<void>): Promise<void> {
diff --git a/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts b/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts
index 88967bdf88ae..f1216b1d8605 100644
--- a/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts
+++ b/nodejs/src/cdp/services/job-queue/job-queue-postgres-v2.ts
@@ -47,11 +47,7 @@ export class CyclotronJobQueuePostgresV2 implements JobQueue {
| 'CDP_CYCLOTRON_INSERT_MAX_BATCH_SIZE'
| 'CDP_CYCLOTRON_INSERT_PARALLEL_BATCHES'
| 'CDP_CYCLOTRON_STRIP_PERSON_FROM_STATE_TEAMS'
- >,
- // Worker-level options the queue passes through to the worker on
- // construction. Currently just fair dequeue; other per-worker tweaks
- // could go here without bloating the constructor signature further.
- private workerOptions: { fairDequeue?: boolean } = {}
+ >
) {
this.sanitizer = createInvocationSanitizer(config)
}
@@ -96,7 +92,6 @@ export class CyclotronJobQueuePostgresV2 implements JobQueue {
batchMaxSize: this.consumerBatchSize,
pollDelayMs: this.config.CDP_CYCLOTRON_BATCH_DELAY_MS,
includeEmptyBatches: true,
- fairDequeue: this.workerOptions.fairDequeue,
})
await this.worker.connect(async (jobs) => {
diff --git a/nodejs/src/cdp/services/job-queue/job-queue-rate-limited-postgres-v2.ts b/nodejs/src/cdp/services/job-queue/job-queue-rate-limited-postgres-v2.ts
index b2feace7b93d..024ea32e0178 100644
--- a/nodejs/src/cdp/services/job-queue/job-queue-rate-limited-postgres-v2.ts
+++ b/nodejs/src/cdp/services/job-queue/job-queue-rate-limited-postgres-v2.ts
@@ -28,10 +28,9 @@ export class CyclotronJobQueueRateLimitedPostgresV2 extends CyclotronJobQueuePos
constructor(
consumerBatchSize: number,
config: ConstructorParameters<typeof CyclotronJobQueuePostgresV2>[1],
- private rateLimit: RateLimitedQueueOptions,
- workerOptions: ConstructorParameters<typeof CyclotronJobQueuePostgresV2>[2] = {}
+ private rateLimit: RateLimitedQueueOptions
) {
- super(consumerBatchSize, config, workerOptions)
+ super(consumerBatchSize, config)
}
public override async startAsConsumer(
diff --git a/nodejs/src/server.ts b/nodejs/src/server.ts
index 2c027ca151a8..448f2d58b6d1 100644
--- a/nodejs/src/server.ts
+++ b/nodejs/src/server.ts
@@ -298,25 +298,19 @@ export class PluginServer implements NodeServer {
// the env var (typical for local dev outside k8s) we fall back to the
// plain queue and dequeue is unthrottled.
//
- // Fair dequeue (per-team round-robin) is independent — it's wired
- // into the worker via workerOptions and applies regardless of whether
- // rate limiting is on.
+ // Fair dequeue (per-team round-robin) is intrinsic to the email queue —
+ // the worker derives it from its queue name — and applies regardless of
+ // whether rate limiting is on.
const sesValkey = createSesRateLimiterValkeyPool(this.config)
- const workerOptions = { fairDequeue: this.config.CDP_CYCLOTRON_EMAIL_FAIR_DEQUEUE }
const queue = sesValkey
- ? new CyclotronJobQueueRateLimitedPostgresV2(
- this.config.CONSUMER_BATCH_SIZE,
- this.config,
- {
- limiter: new RateLimiterService(sesValkey, { name: 'ses' }),
- key: '@posthog/ses/global',
- capacity: this.config.CDP_SES_RATE_LIMIT_CAPACITY,
- refillPerSecond: this.config.CDP_SES_RATE_LIMIT_REFILL_PER_SECOND,
- throttledPollDelayMs: this.config.CDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS,
- },
- workerOptions
- )
- : new CyclotronJobQueuePostgresV2(this.config.CONSUMER_BATCH_SIZE, this.config, workerOptions)
+ ? new CyclotronJobQueueRateLimitedPostgresV2(this.config.CONSUMER_BATCH_SIZE, this.config, {
+ limiter: new RateLimiterService(sesValkey, { name: 'ses' }),
+ key: '@posthog/ses/global',
+ capacity: this.config.CDP_SES_RATE_LIMIT_CAPACITY,
+ refillPerSecond: this.config.CDP_SES_RATE_LIMIT_REFILL_PER_SECOND,
+ throttledPollDelayMs: this.config.CDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS,
+ })
+ : new CyclotronJobQueuePostgresV2(this.config.CONSUMER_BATCH_SIZE, this.config)
const worker = new CdpCyclotronWorkerEmail(this.config, cdpDeps!, queue)
await worker.start()
return worker.service