Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Handle `redisFailOver`

Steven Teyb0e236b1c4a2
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 8d1521ea8b..d3050cc778 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -68,7 +68,10 @@ class LinkCache {
]);
}
- async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
+ async get({ domain, key }: Pick<LinkProps, "domain" | "key">): Promise<{
+ cachedLink: RedisLinkProps | null;
+ redisFailOver: boolean;
+ }> {
// here we use linkcache:${domain}:${key} instead of this._createKey({ domain, key })
// because the key can either be cached as case-sensitive or case-insensitive depending on the domain
// so we should get the original key from the cache
@@ -80,7 +83,7 @@ class LinkCache {
if (cachedLink) {
console.log(`[LRU Cache HIT] ${cacheKey}`);
linkLRUCache.set(cacheKey, cachedLink); // refresh the LRU cache
- return cachedLink;
+ return { cachedLink, redisFailOver: false };
}
console.log(`[LRU Cache MISS] ${cacheKey} - Checking redisGlobal...`);
@@ -92,12 +95,12 @@ class LinkCache {
if (cachedLink) {
console.log(`[Redis Cache HIT] ${cacheKey} - Populating LRU Cache...`);
linkLRUCache.set(cacheKey, cachedLink); // persist to LRU cache
- return cachedLink;
+ return { cachedLink, redisFailOver: false };
} else {
console.log(
`[Redis Cache MISS] ${cacheKey} - Not found in LRU or Redis, falling back to MySQL...`,
);
- return null;
+ return { cachedLink: null, redisFailOver: false };
}
} catch (error) {
console.error(`[Redis Cache Error] ${cacheKey} - ${error}`);
@@ -106,7 +109,7 @@ class LinkCache {
if (cachedLink) {
console.log(`[Vercel Cache HIT] ${cacheKey}`);
linkLRUCache.set(cacheKey, cachedLink);
- return cachedLink;
+ return { cachedLink, redisFailOver: true };
}
console.log(`[Vercel Cache MISS] ${cacheKey} - Falling back to MySQL...`);
@@ -128,7 +131,7 @@ class LinkCache {
ttl: VERCEL_CACHE_EXPIRATION,
}),
);
- return cachedLink;
+ return { cachedLink, redisFailOver: true };
}
}
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 990a7c7e0f..41dac39d01 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -1,4 +1,4 @@
-import { recordClick } from "@/lib/tinybird";
+import { recordClick as recordClickJob } from "@/lib/tinybird";
import { formatRedisLink } from "@/lib/upstash";
import {
APP_DOMAIN,
@@ -86,9 +86,17 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
});
}
- let cachedLink = await linkCache.get({ domain, key });
+ let { cachedLink, redisFailOver } = await linkCache.get({ domain, key });
let isPartnerLink = Boolean(cachedLink?.programId && cachedLink?.partnerId);
+ // skip click tracking during Redis failover to avoid timeouts
+ const recordClick = redisFailOver
+ ? async () => {
+ console.log("Redis failover detected, skipping click tracking...");
+ return;
+ }
+ : recordClickJob;
+
if (!cachedLink) {
let linkData = await getLinkViaEdge({
domain,
@@ -182,8 +190,8 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
const cookieStore = await cookies();
let clickId = cookieStore.get(dubIdCookieName)?.value;
if (!clickId) {
- // if we need to pass the clickId, check if clickId is cached in Redis
- if (shouldCacheClickId) {
+ // if we need to cache the clickId, check if clickId is cached in Redis (assuming no redisFailOver)
+ if (shouldCacheClickId && !redisFailOver) {
const identityHash = await getIdentityHash(req);
clickId =
(await recordClickCache