idempotency-handling▌
aj-geddes/useful-ai-prompts · updated Apr 8, 2026
Implement idempotency to ensure operations produce the same result regardless of how many times they're executed.
Idempotency Handling
Table of Contents
Overview
Implement idempotency to ensure operations produce the same result regardless of how many times they're executed.
When to Use
- Payment processing
- API endpoints with retries
- Webhooks and callbacks
- Message queue consumers
- Distributed transactions
- Bank transfers
- Order creation
- Email sending
- Resource creation
Quick Start
Minimal working example:
import express from "express";
import Redis from "ioredis";
import crypto from "crypto";
interface IdempotentRequest {
key: string;
status: "processing" | "completed" | "failed";
response?: any;
error?: string;
createdAt: number;
completedAt?: number;
}
class IdempotencyService {
private redis: Redis;
private ttl = 86400; // 24 hours
constructor(redisUrl: string) {
this.redis = new Redis(redisUrl);
}
async getRequest(key: string): Promise<IdempotentRequest | null> {
const data = await this.redis.get(`idempotency:${key}`);
return data ? JSON.parse(data) : null;
}
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Express Idempotency Middleware | Express Idempotency Middleware |
| Database-Based Idempotency | Database-Based Idempotency |
| Stripe-Style Idempotency | Stripe-Style Idempotency |
| Message Queue Idempotency | Message Queue Idempotency |
Best Practices
✅ DO
- Require idempotency keys for mutations
- Store request and response together
- Set appropriate TTL for idempotency records
- Validate request body matches stored request
- Handle concurrent requests gracefully
- Return same response for duplicate requests
- Clean up old idempotency records
- Use database constraints for atomicity
❌ DON'T
- Apply idempotency to GET requests
- Store idempotency data forever
- Skip validation of request body
- Use non-unique idempotency keys
- Process same request concurrently
- Change response for duplicate requests
Discussion
Product Hunt–style comments (not star reviews)- No comments yet — start the thread.
Ratings
4.7★★★★★64 reviews- ★★★★★Zaid Gill· Dec 28, 2024
idempotency-handling reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Arjun Yang· Dec 24, 2024
I recommend idempotency-handling for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Shikha Mishra· Dec 20, 2024
idempotency-handling reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Min Agarwal· Dec 8, 2024
Useful defaults in idempotency-handling — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.
- ★★★★★Zaid Ghosh· Nov 27, 2024
Registry listing for idempotency-handling matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Zaid Bansal· Nov 19, 2024
I recommend idempotency-handling for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Luis Taylor· Nov 15, 2024
idempotency-handling reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Rahul Santra· Nov 11, 2024
I recommend idempotency-handling for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Valentina Perez· Nov 7, 2024
Keeps context tight: idempotency-handling is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Valentina Mensah· Oct 26, 2024
idempotency-handling is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.
showing 1-10 of 64