: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): dequeue email queue by priority class behind a flag (#86241)
Meikel Ratz4c639463a694
diff --git a/nodejs/src/cdp/config.ts b/nodejs/src/cdp/config.tsindex 39ae582b5418..5d92c041b40c 100644--- a/nodejs/src/cdp/config.ts+++ b/nodejs/src/cdp/config.ts@@ -91,6 +91,10 @@ export type CdpConfig = ClickhouseConfig & {CDP_SES_RATE_LIMIT_REFILL_PER_SECOND: numberCDP_SES_RATE_LIMIT_CAPACITY: numberCDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS: number+ // When enabled, the email queue dequeues by priority class before the+ // per-team fair ordering, so transactional-class sends aren't stuck behind+ // a broadcast backlog. Requires idx_cyclotron_jobs_email_priority_fair_dequeue.+ CDP_EMAIL_PRIORITY_DEQUEUE_ENABLED: booleanCDP_EVENT_PROCESSOR_EXECUTE_FIRST_STEP: booleanCDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN: string@@ -254,6 +258,7 @@ export function getDefaultCdpConfig(): CdpConfig {CDP_SES_RATE_LIMIT_REFILL_PER_SECOND: 100,CDP_SES_RATE_LIMIT_CAPACITY: 50,CDP_SES_RATE_LIMIT_THROTTLED_POLL_DELAY_MS: 250,+ CDP_EMAIL_PRIORITY_DEQUEUE_ENABLED: false,CDP_EVENT_PROCESSOR_EXECUTE_FIRST_STEP: true,CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN: '',diff --git a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.tsindex bd093a5933ea..6f1b48ba92d8 100644--- a/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts+++ b/nodejs/src/cdp/services/cyclotron-v2/cyclotron-v2.test.ts@@ -1745,6 +1745,69 @@ describe('Cyclotron V2', () => {const createFairWorker = (overrides?: Record<string, unknown>): CyclotronV2Worker =>createWorker(EMAIL_QUEUE, overrides)+ it('dequeues the fast class before an earlier-enqueued bulk backlog when priority dequeue is enabled', async () => {+ // A bulk broadcast (priority 1) is already queued when two fast-class+ // sends (priority 0) arrive, one from the same team. Without priority+ // ordering, the same-team fast job waits behind the whole backlog.+ const teamA = 100+ const teamB = 200+ await manager.bulkCreateJobs([+ ...Array.from({ length: 5 }, () => ({ teamId: teamA, queueName: EMAIL_QUEUE, priority: 1 })),+ { teamId: teamA, queueName: EMAIL_QUEUE, priority: 0 },+ { teamId: teamB, queueName: EMAIL_QUEUE, priority: 0 },+ ])++ const worker = createFairWorker({ batchMaxSize: 2, priorityDequeue: true })+ const jobs = await dequeueOneBatch(worker)++ expect(jobs.map((j) => j.priority)).toEqual([0, 0])+ expect(new Set(jobs.map((j) => j.teamId))).toEqual(new Set([teamA, teamB]))+ })++ it('keeps the per-team interleave within a priority class', async () => {+ const teamA = 100+ const teamB = 200+ await manager.bulkCreateJobs([+ ...Array.from({ length: 2 }, () => ({ teamId: teamA, queueName: EMAIL_QUEUE, priority: 1 })),+ ...Array.from({ length: 2 }, () => ({ teamId: teamB, queueName: EMAIL_QUEUE, priority: 1 })),+ { teamId: teamA, queueName: EMAIL_QUEUE, priority: 0 },+ ])++ const drained: Array<[number, number]> = []+ for (let i = 0; i < 5; i++) {+ const worker = createFairWorker({ batchMaxSize: 1, priorityDequeue: true })+ const batch = await dequeueOneBatch(worker)+ expect(batch).toHaveLength(1)+ drained.push([batch[0].priority, batch[0].teamId])+ await batch[0].ack()+ }++ expect(drained).toEqual([+ [0, teamA],+ [1, teamA],+ [1, teamB],+ [1, teamA],+ [1, teamB],+ ])+ })++ it('ignores priority when priority dequeue is disabled', async () => {+ const teamA = 100+ const teamB = 200+ await manager.bulkCreateJobs([+ ...Array.from({ length: 5 }, () => ({ teamId: teamA, queueName: EMAIL_QUEUE, priority: 1 })),+ { teamId: teamA, queueName: EMAIL_QUEUE, priority: 0 },+ { teamId: teamB, queueName: EMAIL_QUEUE, priority: 0 },+ ])++ const worker = createFairWorker({ batchMaxSize: 2 })+ const jobs = await dequeueOneBatch(worker)++ // Legacy ordering is dequeue_seq only: the first fair round is team A's+ // first bulk job and team B's fast job, so priorities stay mixed.+ expect(jobs.map((j) => j.priority).sort((a, b) => a - b)).toEqual([0, 1])+ })+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,// team B enqueues 1. With strict FIFO, B's 1 sits behind A's 5.diff --git a/nodejs/src/cdp/services/cyclotron-v2/types.ts b/nodejs/src/cdp/services/cyclotron-v2/types.tsindex 4035d66edf0e..826da748c607 100644--- a/nodejs/src/cdp/services/cyclotron-v2/types.ts+++ b/nodejs/src/cdp/services/cyclotron-v2/types.ts@@ -204,6 +204,9 @@ export type CyclotronV2WorkerConfig = {pollDelayMs?: numberheartbeatTimeoutMs?: numberincludeEmptyBatches?: boolean+ // Orders the email queue's fair dequeue by priority class before dequeue_seq.+ // Only meaningful on the email queue; other queues already order by priority.+ priorityDequeue?: boolean}export type CyclotronV2JanitorConfig = {diff --git a/nodejs/src/cdp/services/cyclotron-v2/worker.ts b/nodejs/src/cdp/services/cyclotron-v2/worker.tsindex 8b0b0c7b1106..4a08088cff5d 100644--- a/nodejs/src/cdp/services/cyclotron-v2/worker.ts+++ b/nodejs/src/cdp/services/cyclotron-v2/worker.ts@@ -213,6 +213,7 @@ export class CyclotronV2Worker {private readonly heartbeatTimeoutMs: numberprotected readonly includeEmptyBatches: booleanprotected readonly fairDequeue: boolean+ private readonly priorityDequeue: booleanconstructor(private config: CyclotronV2WorkerConfig) {this.pool = new Pool({@@ -229,6 +230,7 @@ export class CyclotronV2Worker {// 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'+ this.priorityDequeue = config.priorityDequeue ?? false}async connect(processBatch: (jobs: CyclotronV2DequeuedJob[]) => Promise<void>): Promise<void> {@@ -356,6 +358,13 @@ export class CyclotronV2Worker {*/protected async fairDequeueJobs(limit: number = this.batchMaxSize): Promise<RawJobRow[]> {const lockId = uuidv7()+ // With priorityDequeue, priority class leads the sort so transactional-class+ // sends aren't stuck behind a broadcast backlog; the per-team interleave+ // still orders jobs within each class. Served by+ // idx_cyclotron_jobs_email_priority_fair_dequeue.+ const orderBy = this.priorityDequeue+ ? 'priority ASC, dequeue_seq ASC NULLS FIRST'+ : 'dequeue_seq ASC NULLS FIRST'const result = await this.pool.query<RawJobRow>(`WITH available AS (SELECT id@@ -363,7 +372,7 @@ export class CyclotronV2Worker {WHERE status = 'available'AND queue_name = $1AND scheduled <= NOW()- ORDER BY dequeue_seq ASC NULLS FIRST+ ORDER BY ${orderBy}LIMIT $2FOR UPDATE SKIP LOCKED)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.tsindex eb215a386d5b..0b392b1801c9 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,6 +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'+ | 'CDP_EMAIL_PRIORITY_DEQUEUE_ENABLED'>) {this.sanitizer = createInvocationSanitizer(config)@@ -92,6 +93,7 @@ export class CyclotronJobQueuePostgresV2 implements JobQueue {batchMaxSize: this.consumerBatchSize,pollDelayMs: this.config.CDP_CYCLOTRON_BATCH_DELAY_MS,includeEmptyBatches: true,+ priorityDequeue: this.config.CDP_EMAIL_PRIORITY_DEQUEUE_ENABLED,})await this.worker.connect(async (jobs) => {