From a78cedf50c17c46e55bcd5b48a621516ba1b75b9 Mon Sep 17 00:00:00 2001 From: Rui Costa Date: Thu, 16 Oct 2025 15:46:45 -0400 Subject: [PATCH] fix: resolve Redis Cluster CROSSSLOT error in cache deletions --- src/lib/cache.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/lib/cache.ts b/src/lib/cache.ts index cb84cde..2cd83fe 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -114,7 +114,17 @@ export async function deleteCache(...keys: string[]): Promise { const startTime = Date.now(); try { - await cache.del(...keys); + // In cluster mode, keys may hash to different slots + // Use pipeline to delete individually (more efficient than separate awaits) + if (keys.length === 1) { + await cache.del(keys[0]); + } else { + const pipeline = cache.pipeline(); + for (const key of keys) { + pipeline.del(key); + } + await pipeline.exec(); + } const duration = Date.now() - startTime; // Log cache operation with metrics (use first key as representative) @@ -149,11 +159,15 @@ export async function deleteCachePattern(pattern: string): Promise { } if (keys.length > 0) { - // Delete in batches to avoid overwhelming the cluster + // Delete in batches using pipeline (cluster mode compatible) const batchSize = 100; for (let i = 0; i < keys.length; i += batchSize) { const batch = keys.slice(i, i + batchSize); - await cache.del(...batch); + const pipeline = cache.pipeline(); + for (const key of batch) { + pipeline.del(key); + } + await pipeline.exec(); } logger.info(`Deleted cache keys matching pattern`, {