惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
宝玉的分享
宝玉的分享
小众软件
小众软件
罗磊的独立博客
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Handling Duplicate Shopify Webhook Events (And Why You Must)
Muhammad Mas · 2026-05-26 · via DEV Community

You built a Shopify integration. It works great in dev. Then in production,
a customer gets charged twice. Or an order ships twice. Or your inventory
goes negative overnight.

The culprit almost always? Duplicate webhook events.

Shopify guarantees at-least-once delivery, not exactly-once. Your endpoint
will receive the same event more than once. Here is how to handle it properly.


Why Duplicates Happen

Shopify retries a webhook if your server does not respond with 2xx within
5 seconds. It retries up to 19 times over 48 hours.

Duplicates hit you when:

  • Your server is slow to respond
  • A network timeout occurs mid-request
  • Your server restarts while processing
  • A queue consumer crashes and re-pulls the job

Step 1: Respond Immediately, Process Later

Never do heavy work inside the webhook handler. Respond fast, queue the work.

app.post('/webhooks/orders-paid', async (req, res) => {
  res.status(200).send('OK'); // Shopify gets its response immediately
  await queue.push({ topic: 'orders/paid', payload: req.body });
});

Enter fullscreen mode Exit fullscreen mode


Step 2: Build a Dedup Key

Do NOT use X-Shopify-Webhook-Id as your dedup key. That header changes
on every retry attempt. Use the resource ID from the payload instead.

const dedupKey = `orders/paid:${payload.id}`;

Enter fullscreen mode Exit fullscreen mode

This stays the same across all retries for the same event.


Step 3: Check Redis Before Processing

const alreadySeen = await redis.get(dedupKey);

if (alreadySeen) {
  console.log('Duplicate detected, skipping:', dedupKey);
  return;
}

await redis.setex(dedupKey, 86400, '1'); // TTL: 24 hours

Enter fullscreen mode Exit fullscreen mode


Step 4: Add a Database Safety Net

Redis can go down. Your database should be the last line of defense.

CREATE TABLE processed_webhook_events (
  dedup_key VARCHAR(255) UNIQUE NOT NULL,
  processed_at TIMESTAMP DEFAULT NOW()
);

Enter fullscreen mode Exit fullscreen mode

const result = await db.raw(`
  INSERT INTO processed_webhook_events (dedup_key)
  VALUES (?)
  ON CONFLICT (dedup_key) DO NOTHING
  RETURNING id
`, [dedupKey]);

if (result.rows.length === 0) return; // Already processed

Enter fullscreen mode Exit fullscreen mode

The ON CONFLICT DO NOTHING is atomic. Even 10 concurrent requests for
the same event will only insert once.


Step 5: Make Your Handler Idempotent

Dedup catches most duplicates. Idempotent logic catches the rest.

For inventory, always set absolute values, never increment or decrement:

// BAD - breaks on duplicate
await db.inventory.decrement({ quantity: 5 });

// GOOD - safe to run multiple times
await db.inventory.update({ quantity: newAbsoluteValue });

Enter fullscreen mode Exit fullscreen mode


High-Risk Events to Watch

Event Risk Fix
orders/paid Double fulfillment DB unique constraint
inventory_levels/update Wrong stock count Use absolute values
refunds/create Double refund Check refund ID first
customers/create Duplicate accounts Check email uniqueness

Quick Checklist Before You Ship

  • [ ] Webhook responds in under 5 seconds
  • [ ] Processing is async
  • [ ] Dedup key = topic + resource ID
  • [ ] Redis check at entry point
  • [ ] DB unique constraint as fallback
  • [ ] Inventory uses absolute values
  • [ ] Load tested with concurrent duplicate requests

That's the full pattern. Two layers of protection: Redis for speed,
database for correctness. Your handlers stay idempotent as a safety net.

Full guide with queue-level dedup (SQS + BullMQ) and monitoring setup
on our blog: kolachitech.com