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.

fix(error-tracking): hash-tag per-issue rate limiter redis keys (#60838)

Aleksander Błaszkiewicz2409c481b9ef
diff --git a/nodejs/src/ingestion/error-tracking/error-tracking-consumer.test.ts b/nodejs/src/ingestion/error-tracking/error-tracking-consumer.test.ts
index 8db575f2f603..f4dcfcd55dd6 100644
--- a/nodejs/src/ingestion/error-tracking/error-tracking-consumer.test.ts
+++ b/nodejs/src/ingestion/error-tracking/error-tracking-consumer.test.ts
@@ -462,9 +462,13 @@ describe('ErrorTrackingConsumer', () => {
consumer = await createConsumer(hub)
await consumer['rateLimiterRedis']!.useClient({ name: 'test-flush' }, async (client) => {
- const keys = await client.keys(
- `@posthog-test/error-tracking-rate-limiter/tokens/${team.id}:exceptions:*`
- )
+ // Per-issue buckets are hash-tagged (`tokens/{teamId}/…`); the team-global
+ // bucket is keyed straight off the id (`tokens/teamId:exceptions:global`).
+ const tokens = `@posthog-test/error-tracking-rate-limiter/tokens`
+ const keys = [
+ ...(await client.keys(`${tokens}/{${team.id}}/*`)),
+ ...(await client.keys(`${tokens}/${team.id}:*`)),
+ ]
if (keys.length > 0) {
await client.del(...keys)
}
diff --git a/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.test.ts b/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.test.ts
index a1556d2ee6e0..bdd29687e4bb 100644
--- a/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.test.ts
+++ b/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.test.ts
@@ -118,6 +118,21 @@ describe('PerIssueGuardedRateLimiterService', () => {
expect(await cooldownIsSet(limiter.cooldownKey(42))).toBe(false)
})
+ it('colocates cooldown, counter, and bucket keys on one Redis Cluster slot per team', () => {
+ const limiter = build('slot-tag')
+ const nowSeconds = Math.round(now / 1000)
+ const keys = [
+ limiter.cooldownKey(42),
+ limiter.counterKey(42, nowSeconds),
+ limiter.bucketKey(42, '42:exceptions:issue:abc'),
+ ]
+ // Redis hashes only the substring inside the first {…}; identical tags ⇒ same slot ⇒ no CROSSSLOT.
+ const hashTag = (key: string) => key.slice(key.indexOf('{') + 1, key.indexOf('}'))
+ expect(keys.map(hashTag)).toEqual(['42', '42', '42'])
+ // Different teams must not collapse onto the same tag.
+ expect(hashTag(limiter.cooldownKey(99))).toBe('99')
+ })
+
it('skips counter increment for an already-seen scopeKey', async () => {
const limiter = build('repeat-scope')
await cleanLimiter(limiter)
@@ -146,7 +161,7 @@ describe('PerIssueGuardedRateLimiterService', () => {
// Bucket for 'b' was never written.
const bExists = await redis.useClient({ name: 'check' }, async (client) =>
- client.exists(`${limiter.getKeyPrefix()}/42:exceptions:issue:b`)
+ client.exists(limiter.bucketKey(42, '42:exceptions:issue:b'))
)
expect(bExists).toBe(0)
@@ -266,14 +281,14 @@ describe('PerIssueGuardedRateLimiterService', () => {
])
// Team 100 was already in cooldown → new scopeKeys never minted buckets.
- expect(await bucketExists(`${limiter.getKeyPrefix()}/100:exceptions:issue:cooldown-1`)).toBe(0)
- expect(await bucketExists(`${limiter.getKeyPrefix()}/100:exceptions:issue:cooldown-2`)).toBe(0)
+ expect(await bucketExists(limiter.bucketKey(100, '100:exceptions:issue:cooldown-1'))).toBe(0)
+ expect(await bucketExists(limiter.bucketKey(100, '100:exceptions:issue:cooldown-2'))).toBe(0)
// Team 200 tripped this batch — first two buckets minted, third (tripC) NOT.
expect(await cooldownIsSet(limiter.cooldownKey(200))).toBe(true)
- expect(await bucketExists(`${limiter.getKeyPrefix()}/200:exceptions:issue:tripA`)).toBe(1)
- expect(await bucketExists(`${limiter.getKeyPrefix()}/200:exceptions:issue:tripB`)).toBe(1)
- expect(await bucketExists(`${limiter.getKeyPrefix()}/200:exceptions:issue:tripC`)).toBe(0)
+ expect(await bucketExists(limiter.bucketKey(200, '200:exceptions:issue:tripA'))).toBe(1)
+ expect(await bucketExists(limiter.bucketKey(200, '200:exceptions:issue:tripB'))).toBe(1)
+ expect(await bucketExists(limiter.bucketKey(200, '200:exceptions:issue:tripC'))).toBe(0)
// Team 300 never tripped (only 1 distinct scopeKey).
expect(await cooldownIsSet(limiter.cooldownKey(300))).toBe(false)
diff --git a/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.ts b/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.ts
index 974a315d8ee1..af552b1e9ce0 100644
--- a/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.ts
+++ b/nodejs/src/ingestion/error-tracking/per-issue-guarded-rate-limiter.service.ts
@@ -65,13 +65,20 @@ export class PerIssueGuardedRateLimiterService implements KeyedRateLimiter {
return this.prefixes.cooldown
}
+ // All three keys of a single guarded call carry a `{teamId}` hash tag so Redis Cluster
+ // colocates them on one slot — the Lua script touches all three at once and would otherwise
+ // fail with CROSSSLOT. Tagging on teamId keeps different teams spread across slots.
public cooldownKey(teamId: number): string {
- return `${this.prefixes.cooldown}/${teamId}`
+ return `${this.prefixes.cooldown}/{${teamId}}`
}
public counterKey(teamId: number, nowSeconds: number): string {
const window = Math.floor(nowSeconds / this.config.windowTtlSeconds)
- return `${this.prefixes.counter}/${teamId}/${window}`
+ return `${this.prefixes.counter}/{${teamId}}/${window}`
+ }
+
+ public bucketKey(teamId: number, id: string): string {
+ return `${this.prefixes.bucket}/{${teamId}}/${id}`
}
/** Tripped/cooldown pass through as `isRateLimited: false`; the 4-state status only surfaces via the Prom counter. */
@@ -117,7 +124,7 @@ export class PerIssueGuardedRateLimiterService implements KeyedRateLimiter {
pipeline.checkGuardedRateLimit(
this.cooldownKey(req.teamId),
this.counterKey(req.teamId, now),
- `${this.prefixes.bucket}/${req.id}`,
+ this.bucketKey(req.teamId, req.id),
now,
req.cost,
req.bucketSize,