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
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: aj-geddes-useful-ai-prompts-idempotency-handling3description: Idempotency Handling4---56# Idempotency Handling78## Table of Contents910- [Overview](#overview)11- [When to Use](#when-to-use)12- [Quick Start](#quick-start)13- [Reference Guides](#reference-guides)14- [Best Practices](#best-practices)1516## Overview1718Implement idempotency to ensure operations produce the same result regardless of how many times they're executed.1920## When to Use2122- Payment processing23- API endpoints with retries24- Webhooks and callbacks25- Message queue consumers26- Distributed transactions27- Bank transfers28- Order creation29- Email sending30- Resource creation3132## Quick Start3334Minimal working example:3536```typescript37import express from "express";38import Redis from "ioredis";39import crypto from "crypto";4041interface IdempotentRequest {42 key: string;43 status: "processing" | "completed" | "failed";44 response?: any;45 error?: string;46 createdAt: number;47 completedAt?: number;48}4950class IdempotencyService {51 private redis: Redis;52 private ttl = 86400; // 24 hours5354 constructor(redisUrl: string) {55 this.redis = new Redis(redisUrl);56 }5758 async getRequest(key: string): Promise<IdempotentRequest | null> {59 const data = await this.redis.get(`idempotency:${key}`);60 return data ? JSON.parse(data) : null;61 }62// ... (see reference guides for full implementation)63```6465## Reference Guides6667Detailed implementations in the `references/` directory:6869| Guide | Contents |70|---|---|71| [Express Idempotency Middleware](references/express-idempotency-middleware.md) | Express Idempotency Middleware |72| [Database-Based Idempotency](references/database-based-idempotency.md) | Database-Based Idempotency |73| [Stripe-Style Idempotency](references/stripe-style-idempotency.md) | Stripe-Style Idempotency |74| [Message Queue Idempotency](references/message-queue-idempotency.md) | Message Queue Idempotency |7576## Best Practices7778### ✅ DO7980- Require idempotency keys for mutations81- Store request and response together82- Set appropriate TTL for idempotency records83- Validate request body matches stored request84- Handle concurrent requests gracefully85- Return same response for duplicate requests86- Clean up old idempotency records87- Use database constraints for atomicity8889### ❌ DON'T9091- Apply idempotency to GET requests92- Store idempotency data forever93- Skip validation of request body94- Use non-unique idempotency keys95- Process same request concurrently96- Change response for duplicate requests9798---99> Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions.100<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-idempotency-handling in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Idempotency Handling It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.