This document explains how to use the IdempotencyStore class in the frontend. This class is designed to help manage and persist idempotency keys and responses locally (e.g., in IndexedDB) to avoid duplicate submissions or calls.
Idempotency means that repeating the same action multiple times will always result in the same outcome. In frontend applications, this ensures that retrying a request (like submitting a form) won’t cause duplication.
This utility is useful for cases like:
- Payment or checkout submissions
- Form submissions
- Offline caching of server responses
- Avoiding duplicate requests
- Ensuring idempotent responses
- Optimizing performance
- Enhancing user experience
This library is compatible with the backend Idempotency Middleware, which ensures that the same request is processed only once and returns the same result. Read the Backend Guide
npm install idempotency-clientUUID()- generates a unique identifier
Creates a new idempotency key for a request, with optional TTL.
Returns the stored key for the given request if it's not expired.
Saves the response data associated with the requestId.
Retrieves cached response data if it exists.
Deletes the stored key and response for the given requestId.
Exports all saved data as JSON (useful for backup).
Restores data from a JSON string.
Removes expired entries automatically (default is 7 days).
import IdempotencyStore from 'idempotency-client';
async function handleFormSubmit() {
const requestId = 'submit::formA';
const key = await IdempotencyStore.CreateKey(requestId, 60000);
const existing = await IdempotencyStore.GetResponse(requestId);
if (existing) {
renderFromCache(existing);
return;
}
const response = await fetch('/api/submit', {
method: 'POST',
headers: { 'Idempotency-Key': key },
body: JSON.stringify(data)
});
const result = await response.json();
await IdempotencyStore.SaveResponse(requestId, result);
renderResponse(result);
}- Ensure the server supports idempotency via headers.
requestIdshould uniquely represent the logical request.- Run
AutoCleanup()periodically to prevent storage bloat.
This store is a reliable, frontend-safe mechanism for caching request outcomes and safely retrying operations without duplication.