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

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
T
Tailwind CSS Blog
G
Google Developers Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
爱范儿
爱范儿
博客园 - 【当耐特】
腾讯CDC
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
博客园_首页
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

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
The Hidden Trap in Backend Tutorials: Why Your Webhooks A...
Anubhav Gupta · 2026-06-20 · via DEV Community

We've all been there. You're sitting at your desk late at night, watching a backend tutorial, fueled by coffee and determination. The instructor shows you how to integrate a payment gateway or a webhook, and it looks incredibly simple:

  1. Receive the webhook event.
  2. Update the database.
  3. Return a 200 OK.

You write the code, test it locally, and it works perfectly. You push it to production feeling like a senior engineer.

But a few days later, you check your database and notice something terrifying: Duplicate records. Users are being credited twice, or identical data entries are piling up.

Welcome to the real world of backend engineering, where the "Happy Path" is a myth, and network reliability is a lie.

Here is what went wrong, and the crucial concept of Idempotency that tutorials often skip.

The Problem : The Unreliable Network
In a perfect world, when an external service (like Stripe, GitHub, or Razorpay) sends a webhook to your server, your server processes it instantly and fires back a 200 OK to say, "Got it, thanks!"

But networks are inherently flaky. Sometimes, your server takes too long to process the data. Other times, a DNS hiccup drops your 200 OK response before it reaches the external service.

When the external service doesn't get your confirmation in time, it assumes the event was lost. So, what does it do? It retries. It fires the exact same webhook event again a few seconds (or minutes) later.

If your code blindly accepts data and inserts it into the database, you've just processed the same event twice.

The "Aha!" Moment: Enter Idempotency
To fix this, we need to design our API to be Idempotent.

Idempotency is a fancy mathematical term that simply means: Making multiple identical requests has the same effect as making a single request.

Think of it like an elevator button. Pressing the "Floor 5" button once tells the elevator to go to floor 5. Smashing the "Floor 5" button ten times in a row doesn't make the elevator go to floor 50. The end result is exactly the same.

Your webhook endpoint needs to act like that elevator button.

How to Implement an Idempotent Webhook
To stop duplicate data, you need to turn your server into a bouncer. Before letting any data in, it needs to check the guest list.

Here is the step-by-step logic to fix the issue:

1. Find the Unique Identifier
Every well-designed webhook payload contains a unique ID for that specific event (e.g., event_id or stripe_signature). This is your golden ticket.

2. Check the Database Before Processing
When the webhook hits your server, do not process the business logic immediately. First, query your database to see if you have already processed this event_id.

3. Handle the Duplicate Gracefully
If the event_id exists, it means this is a network retry. Your server should safely ignore the payload and immediately return a 200 OK to satisfy the external service.

If the event_id does not exist, process the data, save the event_id to your database, and return the 200 OK.

The Code Example (Node.js / Express)
Here is what that mental shift looks like in code:

app.post('/webhook', async (req, res) => {
  const eventId = req.body.event_id;
  const payloadData = req.body.data;

  try {
    // 1. Check if we've already processed this event
    const existingEvent = await db.processedEvents.findUnique({
      where: { id: eventId }
    });

    // 2. If it exists, it's a retry! Ignore it, but send a 200 OK.
    if (existingEvent) {
      console.log(`Duplicate event ${eventId} blocked.`);
      return res.status(200).send('Event already processed');
    }

    // 3. If it's new, process the business logic safely
    await updateUserData(payloadData);

    // 4. Save the event_id so we remember it for the future
    await db.processedEvents.create({
      data: { id: eventId }
    });

    // 5. Finally, send the success response
    return res.status(200).send('Webhook received and processed');

  } catch (error) {
    console.error("Webhook processing failed", error);
    return res.status(500).send('Internal Server Error');
  }
});

The Takeaway
Tutorials are amazing for learning the syntax and the basic flow of a framework. But the transition from a "learner" to a "builder" happens the moment you start dealing with real-world edge cases.

Building systems that work when everything goes perfectly is easy. Engineering systems that gracefully handle failure, retries, and latency is the real challenge and the real fun.

Have you ever had a network retry cause havoc in your database? How do you handle idempotency in your own APIs? Let me know in the comments below!