Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: analytics ingest race condition

Blue Mouse52b4db5ddede
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 3121a5703..66fbcfe79 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -1150,11 +1150,31 @@ export class AnalyticsService {
userAgent: string,
ip: string,
): Promise<[boolean, string]> {
- const { exists, psid } = await this.getSessionId(pid, userAgent, ip)
+ const salt = await this.saltService.getSaltForSession()
+ const psid = this.derivePsidFromInputs(pid, userAgent, ip, salt)
const sessionKey = this.getSessionKey(psid)
- await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)
- return [!exists, psid]
+ // Use SET with NX (only set if not exists) for atomic "new session" detection
+ // This prevents race conditions where multiple workers could both see the key
+ // doesn't exist and both think they're creating a "new session"
+ const result = await redis.set(
+ sessionKey,
+ '1',
+ 'EX',
+ UNIQUE_SESSION_LIFE_TIME,
+ 'NX',
+ )
+
+ // If SET NX succeeded (returned 'OK'), this is a new session
+ // If it returned null, the session already existed
+ const isNew = result === 'OK'
+
+ if (!isNew) {
+ // Session already exists, extend TTL
+ await this.extendSessionTTL(psid)
+ }
+
+ return [isNew, psid]
}
async extendSessionTTL(psid: string): Promise<void> {
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index bcf2d482f..837d110da 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -1159,11 +1159,31 @@ export class AnalyticsService {
userAgent: string,
ip: string,
): Promise<[boolean, string]> {
- const { exists, psid } = await this.getSessionId(pid, userAgent, ip)
+ const salt = await this.saltService.getSaltForProject(pid)
+ const psid = this.derivePsidFromInputs(pid, userAgent, ip, salt)
const sessionKey = this.getSessionKey(psid)
- await redis.set(sessionKey, '1', 'EX', UNIQUE_SESSION_LIFE_TIME)
- return [!exists, psid]
+ // Use SET with NX (only set if not exists) for atomic "new session" detection
+ // This prevents race conditions where multiple workers could both see the key
+ // doesn't exist and both think they're creating a "new session"
+ const result = await redis.set(
+ sessionKey,
+ '1',
+ 'EX',
+ UNIQUE_SESSION_LIFE_TIME,
+ 'NX',
+ )
+
+ // If SET NX succeeded (returned 'OK'), this is a new session
+ // If it returned null, the session already existed
+ const isNew = result === 'OK'
+
+ if (!isNew) {
+ // Session already exists, extend TTL
+ await this.extendSessionTTL(psid)
+ }
+
+ return [isNew, psid]
}
async extendSessionTTL(psid: string): Promise<void> {