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.

Fallback to vercelCache when redis timeouts (#3566)

Steven Teybe4905069297
diff --git a/apps/web/app/(ee)/api/track/click/route.ts b/apps/web/app/(ee)/api/track/click/route.ts
index e71a8f9b1d..1b75001afe 100644
--- a/apps/web/app/(ee)/api/track/click/route.ts
+++ b/apps/web/app/(ee)/api/track/click/route.ts
@@ -14,7 +14,7 @@ import { getWorkspaceViaEdge } from "@/lib/planetscale";
import { getLinkWithPartner } from "@/lib/planetscale/get-link-with-partner";
import { recordClick } from "@/lib/tinybird";
import { RedisLinkProps } from "@/lib/types";
-import { formatRedisLink, redis, redisGlobal } from "@/lib/upstash";
+import { formatRedisLink, redis, redisGlobalWithTimeout } from "@/lib/upstash";
import { DiscountSchema } from "@/lib/zod/schemas/discount";
import { PartnerSchema } from "@/lib/zod/schemas/partners";
import { isValidUrl, nanoid } from "@dub/utils";
@@ -60,18 +60,17 @@ export const POST = withAxiom(async (req) => {
const identityHash = await getIdentityHash(req);
- const [cachedClickId, mgetResults] = await Promise.all([
- redisGlobal.get<string>(
- recordClickCache._createKey({ domain, key, identityHash }),
- ),
+ let [redisGlobalResults, cachedAllowedHostnames] = await Promise.all([
+ redisGlobalWithTimeout
+ .mget<
+ [string | null, RedisLinkProps | null]
+ >([recordClickCache._createKey({ domain, key, identityHash }), linkCache._createKey({ domain, key })])
+ .catch(() => [null, null] as [string | null, RedisLinkProps | null]),
- redis.mget<[RedisLinkProps, string[]]>([
- linkCache._createKey({ domain, key }),
- allowedHostnamesCache._createKey({ domain }),
- ]),
+ redis.get<string[]>(allowedHostnamesCache._createKey({ domain })),
]);
- let [cachedLink, cachedAllowedHostnames] = mgetResults;
+ let [cachedClickId, cachedLink] = redisGlobalResults;
// assign a new clickId if there's no cached clickId
// else, reuse the cached clickId
diff --git a/apps/web/app/(ee)/api/track/open/route.ts b/apps/web/app/(ee)/api/track/open/route.ts
index c8425f1c6d..f3305e9cb8 100644
--- a/apps/web/app/(ee)/api/track/open/route.ts
+++ b/apps/web/app/(ee)/api/track/open/route.ts
@@ -9,7 +9,7 @@ import { getIdentityHash } from "@/lib/middleware/utils/get-identity-hash";
import { getLinkViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { RedisLinkProps } from "@/lib/types";
-import { formatRedisLink, redis, redisGlobal } from "@/lib/upstash";
+import { formatRedisLink, redis, redisGlobalWithTimeout } from "@/lib/upstash";
import {
trackOpenRequestSchema,
trackOpenResponseSchema,
@@ -71,10 +71,12 @@ export const POST = withAxiom(async (req) => {
const key = deepLink.pathname.slice(1) || "_root"; // Remove leading slash, default to _root if empty
let [cachedClickId, cachedLink] = await Promise.all([
- redisGlobal.get<string>(
- recordClickCache._createKey({ domain, key, identityHash }),
- ),
- redis.get<RedisLinkProps>(linkCache._createKey({ domain, key })),
+ redisGlobalWithTimeout
+ .get<string>(recordClickCache._createKey({ domain, key, identityHash }))
+ .catch(() => null),
+ redisGlobalWithTimeout
+ .get<RedisLinkProps>(linkCache._createKey({ domain, key }))
+ .catch(() => null),
]);
// assign a new clickId if there's no cached clickId
diff --git a/apps/web/app/(ee)/api/track/visit/route.ts b/apps/web/app/(ee)/api/track/visit/route.ts
index e221a58a2e..eb398a4e67 100644
--- a/apps/web/app/(ee)/api/track/visit/route.ts
+++ b/apps/web/app/(ee)/api/track/visit/route.ts
@@ -9,7 +9,7 @@ import { getIdentityHash } from "@/lib/middleware/utils/get-identity-hash";
import { getLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { RedisLinkProps } from "@/lib/types";
-import { formatRedisLink, redis, redisGlobal } from "@/lib/upstash";
+import { formatRedisLink, redisGlobalWithTimeout } from "@/lib/upstash";
import { isValidUrl, nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -36,10 +36,12 @@ export const POST = withAxiom(async (req) => {
const identityHash = await getIdentityHash(req);
let [clickId, cachedLink] = await Promise.all([
- redisGlobal.get<string>(
- recordClickCache._createKey({ domain, key, identityHash }),
- ),
- redis.get<RedisLinkProps>(linkCache._createKey({ domain, key })),
+ redisGlobalWithTimeout
+ .get<string>(recordClickCache._createKey({ domain, key, identityHash }))
+ .catch(() => null),
+ redisGlobalWithTimeout
+ .get<RedisLinkProps>(linkCache._createKey({ domain, key }))
+ .catch(() => null),
]);
// if the clickId is already cached in Redis, return it
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 48039e8e48..aa0e7f8be6 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -4,6 +4,7 @@ import {
redisGlobal,
redisGlobalWithTimeout,
} from "@/lib/upstash";
+import { getCache, waitUntil } from "@vercel/functions";
import { LRUCache } from "lru-cache";
import { decodeKey, isCaseSensitiveDomain } from "./case-sensitivity";
import { ExpandedLink } from "./utils/transform-link";
@@ -16,11 +17,15 @@ const linkLRUCache = new LRUCache<string, RedisLinkProps>({
max: 10000, // max 10,000 entries
ttl: 5000, // 5 seconds
});
-
/*
- * Link cache expiration is set to 24 hours by default for all links.
+ * When traffic spikes, new Fluid instances are spun up.
+ * Since LRUCache is not shared between Fluid instances,
+ * we fallback to Vercel cache if both LRUCache/Redis are not available
*/
-const CACHE_EXPIRATION = 60 * 60 * 24;
+export const vercelCache = getCache();
+
+const VERCEL_CACHE_EXPIRATION = 60 * 5; // 5 minutes
+const REDIS_CACHE_EXPIRATION = 60 * 60 * 24;
class LinkCache {
async mset(links: ExpandedLink[]) {
@@ -33,20 +38,30 @@ class LinkCache {
links.forEach((link) => {
const redisLink = formatRedisLink(link);
const cacheKey = this._createKey({ domain: link.domain, key: link.key });
- pipeline.set(cacheKey, redisLink, { ex: CACHE_EXPIRATION });
+ pipeline.set(cacheKey, redisLink, { ex: REDIS_CACHE_EXPIRATION });
});
return await pipeline.exec();
}
- async set(link: ExpandedLink) {
+ async set(
+ link: ExpandedLink,
+ { setVercelCache }: { setVercelCache?: boolean } = {},
+ ) {
const redisLink = formatRedisLink(link);
const cacheKey = this._createKey({ domain: link.domain, key: link.key });
// Update LRU cache immediately to prevent stale reads
linkLRUCache.set(cacheKey, redisLink);
- return await redisGlobal.set(cacheKey, redisLink, { ex: CACHE_EXPIRATION });
+ await Promise.all([
+ redisGlobal.set(cacheKey, redisLink, {
+ ex: REDIS_CACHE_EXPIRATION,
+ }),
+ setVercelCache
+ ? vercelCache.set(cacheKey, redisLink, { ttl: VERCEL_CACHE_EXPIRATION })
+ : this._invalidateVercelCache(cacheKey),
+ ]);
}
async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
@@ -82,12 +97,23 @@ class LinkCache {
}
} catch (error) {
console.error(`[Redis Cache Error] ${cacheKey} - ${error}`);
- throw new Error(`[Redis Cache Error] ${cacheKey} - ${error}`);
+
+ cachedLink = (await vercelCache.get(cacheKey)) as RedisLinkProps | null;
+ if (cachedLink) {
+ console.log(`[Vercel Cache HIT] ${cacheKey}`);
+ linkLRUCache.set(cacheKey, cachedLink);
+ return cachedLink;
+ }
+
+ console.log(`[Vercel Cache MISS] ${cacheKey} - Falling back to MySQL...`);
+ return null;
}
}
async delete({ domain, key }: Pick<LinkProps, "domain" | "key">) {
- return await redisGlobal.del(this._createKey({ domain, key }));
+ const cacheKey = this._createKey({ domain, key });
+ waitUntil(this._invalidateVercelCache(cacheKey));
+ return await redisGlobal.del(cacheKey);
}
async deleteMany(links: Pick<LinkProps, "domain" | "key">[]) {
@@ -126,6 +152,16 @@ class LinkCache {
return caseSensitive ? cacheKey : cacheKey.toLowerCase();
}
+
+ // Vercel cache reads are 10x cheaper than writes, so to invalidate the cache
+ // we check if the value is cached first before deleting it.
+ async _invalidateVercelCache(cacheKey: string) {
+ return vercelCache
+ .get(cacheKey)
+ .then((cachedLink) =>
+ cachedLink ? vercelCache.delete(cacheKey) : undefined,
+ );
+ }
}
export const linkCache = new LinkCache();
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 648f7d1e0e..476bab5481 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -99,7 +99,7 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
ev.waitUntil(
(async () => {
if (!isPartnerLink) {
- await linkCache.set(linkData as any);
+ await linkCache.set(linkData as any, { setVercelCache: true });
return;
}
@@ -109,11 +109,14 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
});
// we'll use this data on /track/click
- await linkCache.set({
- ...(linkData as any),
- ...(partner && { partner }),
- ...(discount && { discount }),
- });
+ await linkCache.set(
+ {
+ ...(linkData as any),
+ ...(partner && { partner }),
+ ...(discount && { discount }),
+ },
+ { setVercelCache: true },
+ );
})(),
);
}
diff --git a/apps/web/lib/upstash/redis.ts b/apps/web/lib/upstash/redis.ts
index d449f88275..c8a85010fb 100644
--- a/apps/web/lib/upstash/redis.ts
+++ b/apps/web/lib/upstash/redis.ts
@@ -26,5 +26,5 @@ export const redisGlobal = new Redis(redisConfig);
export const redisGlobalWithTimeout = new Redis({
...redisConfig,
- signal: () => AbortSignal.timeout(1000),
+ signal: () => AbortSignal.timeout(1500),
});