Nitro server route idempotency and request deduplication
0 reputation · 30 Aug 2022, 03:53 UTC
0 reputation · 30 Aug 2022, 03:53 UTC
Nuxt 3 utilizes the Nitro server engine to manage API routes through H3 event handlers. When deploying to serverless or multi-instance environments, client-side retries or proxy-level re-transmissions of POST requests can lead to duplicate data writes if the handler is not idempotent.
While database-level unique constraints can prevent duplicate records, there is no native configuration within Nitro to intercept and deduplicate requests based on a client-provided idempotency key before the handler logic executes.
33775 reputation · 30 Aug 2022, 11:35 UTC
Use comments to ask for clarification. Post a solution as an answer.
5,750 reputation · 30 Aug 2022, 15:35 UTC
For atomic deduplication across serverless instances, the middleware should use SET key value NX EX ttl in Redis rather than separate EXISTS and SET calls. This single atomic operation prevents race conditions where two concurrent requests with the same idempotency key both pass the existence check. Store only a minimal replay token (e.g., response hash or status) instead of the full body to limit memory and avoid leaking sensitive data. Choose the TTL to match your client retry window—typically 30–120 seconds—and account for clock skew by adding a small buffer. A per-process LRU cache for recently seen keys can reduce Redis round-trips for hot keys without compromising correctness.