secondsky avatar

Idempotency Handling

This is a solid reference for preventing duplicate operations when requests get retried. Shows two approaches: Redis for simple caching and Postgres for more robust tracking with status management. The database pattern is what you want for payments or orders where you need to handle concurrent requests trying to process the same idempotency key. Includes the crucial detail of hashing request bodies to catch key reuse with different payloads. The cleanup job examples are helpful since expired keys will bloat your table if you ignore them. Good practical code you can actually drop into an Express app or payment handler without much modification.

skills.sh installs
415
GitHub stars
248
last updated
Aug 21, 2026

Install

$ npx -y skills add secondsky/claude-skills --skill idempotency-handling --agent claude-code

Installs into .claude/skills of the current project.

Files

Idempotency Handling

Ensure operations produce identical results regardless of execution count.

Idempotency Key Pattern

const redis = require('redis');
const client = redis.createClient();

async function idempotencyMiddleware(req, res, next) {
  const key = req.headers['idempotency-key'];
  if (!key) return next();

  const cached = await client.get(`idempotency:${key}`);
  if (cached) {
    const { status, body } = JSON.parse(cached);
    return res.status(status).json(body);
  }

  // Store original send
  const originalSend = res.json.bind(res);
  res.json = async (body) => {
    await client.setEx(
      `idempotency:${key}`,
      86400, // 24 hours
      JSON.stringify({ status: res.statusCode, body })
    );
    return originalSend(body);
  };

  next();
}

Database-Backed Idempotency

CREATE TABLE idempotency_keys (
  key VARCHAR(255) PRIMARY KEY,
  request_hash VARCHAR(64) NOT NULL,
  response JSONB,
  status VARCHAR(20) DEFAULT 'processing',
  created_at TIMESTAMP DEFAULT NOW(),
  expires_at TIMESTAMP DEFAULT NOW() + INTERVAL '24 hours'
);

CREATE INDEX idx_idempotency_expires ON idempotency_keys(expires_at);

Sponsors

Details

Repository
github.com/secondsky/claude-skills
Categories
Backend & APIs, Finance & Trading, Databases
First seen
Jun 3, 2026

More from secondsky/claude-skills

All 146
1davila7 avatarshopify-developmentdavila7/claude-code-templatesBuild Shopify apps, extensions, themes using GraphQL Admin API, Shopify CLI, Polaris UI, and Liquid. TRIGGER: "shopify", "shopify app", "checkout extension", "a411skills.sh installs
2jezweb avatarcloudflare-d1jezweb/claude-skillsBuild with D1 serverless SQLite database on Cloudflare's edge. Use when: creating databases, writing SQL migrations, querying D1 from Workers, handling relation408skills.sh installs
3timescale avatarfind-hypertable-candidatestimescale/pg-aiguideUse this skill to analyze an existing PostgreSQL database and identify which tables should be converted to Timescale/TimescaleDB hypertables. **Trigger when use408skills.sh installs
4secondsky avatarcloudflare-managersecondsky/claude-skillsComprehensive Cloudflare account management for deploying Workers, KV Storage, R2, Pages, DNS, and Routes. Use when deploying cloudflare services, managing work407skills.sh installs
5jezweb avatarfastmcpjezweb/claude-skillsBuild MCP servers in Python with FastMCP to expose tools, resources, and prompts to LLMs. Supports storage backends, middleware, OAuth Proxy, OpenAPI integratio406skills.sh installs
6bobmatnyc avatardigitalocean-managed-databasesbobmatnyc/claude-mpm-skillsDigitalOcean Managed Databases for PostgreSQL, MySQL, Redis, MongoDB, Kafka, OpenSearch, and Valkey. Use when provisioning, scaling, or operating managed databa404skills.sh installs