Dedup

createDedup() is an in-process key-set with fixed-window TTL. Use to drop duplicates of the same logical request inside a window - webhook delivery, idempotency keys, retry storms.

Setup

// src/lib/server/dedup.js
import { createDedup } from 'svelte-adapter-uws/plugins/dedup';

export const seen = createDedup({ ttlMs: 60_000 });

Usage

import { seen } from '$lib/server/dedup';

if (await seen.acquire(requestId)) {
  // First time we have seen this id within the window.
  await processWebhook(payload);
} else {
  // Duplicate - drop silently or return 200 OK to the sender.
}

API

MethodDescription
acquire(key)true if first time within the window; false for duplicates.
has(key)Membership check without acquiring.
delete(key)Force-evict a key.
clear()Drop all entries.

Options

OptionDefaultDescription
ttlMs60000Window size in ms.
maxKeys1_000_000Capacity cap. Past the cap, oldest entries evict.
maxIdLength256Reject acquire(key) / has(key) / delete(key) calls where key.length > maxIdLength synchronously. Pass Infinity to disable.

The 256-char maxIdLength is a defense-in-depth bound. Pre-cap, a 1 MB wire-supplied key would anchor a 1 MB heap entry until the TTL elapsed. The cap throws synchronously with the actual length, so callers can log the offender.

See also

Was this page helpful?