<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Shemang</title>
    <description>The latest articles on DEV Community by David Shemang (@shemzz).</description>
    <link>https://dev.to/shemzz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F509480%2F6bfce539-106a-46ed-8a6b-932d38b5731d.png</url>
      <title>DEV Community: David Shemang</title>
      <link>https://dev.to/shemzz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shemzz"/>
    <language>en</language>
    <item>
      <title>Your Payment Webhook Is Not a Notification. It's a Distributed Systems Problem.</title>
      <dc:creator>David Shemang</dc:creator>
      <pubDate>Sun, 08 Nov 2026 11:08:00 +0000</pubDate>
      <link>https://dev.to/shemzz/your-payment-webhook-is-not-a-notification-its-a-distributed-systems-problem-2heh</link>
      <guid>https://dev.to/shemzz/your-payment-webhook-is-not-a-notification-its-a-distributed-systems-problem-2heh</guid>
      <description>&lt;p&gt;&lt;em&gt;Why building reliable payment infrastructure requires thinking beyond HTTP 200.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Consider this scenario.&lt;/p&gt;

&lt;p&gt;A customer places an order on an e-commerce platform and pays ₦85,000. The payment provider processes the transaction successfully and sends a webhook to your backend.&lt;/p&gt;

&lt;p&gt;Your backend receives it, updates the order to &lt;code&gt;PAID&lt;/code&gt;, and returns HTTP 200.&lt;/p&gt;

&lt;p&gt;Everything works.&lt;/p&gt;

&lt;p&gt;Except the payment provider never receives your response because of a temporary network interruption.&lt;/p&gt;

&lt;p&gt;It retries the webhook.&lt;/p&gt;

&lt;p&gt;Your backend receives the exact same event again.&lt;/p&gt;

&lt;p&gt;Now, depending on how you implemented your payment processing logic, you might have just credited a wallet twice, created two orders, triggered duplicate notifications, or initiated another fulfilment process.&lt;/p&gt;

&lt;p&gt;This is not an edge case. It is a fundamental characteristic of distributed systems.&lt;/p&gt;

&lt;p&gt;And it is something every developer building payment-enabled applications should understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with assuming exactly-once delivery
&lt;/h2&gt;

&lt;p&gt;Most webhook integrations operate with an at-least-once delivery guarantee.&lt;/p&gt;

&lt;p&gt;In simple terms, the sender will keep attempting delivery until it receives an acceptable acknowledgement or exhausts its retry policy.&lt;/p&gt;

&lt;p&gt;This means your application must assume that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The same event can arrive multiple times.&lt;/li&gt;
&lt;li&gt;Events can arrive out of order.&lt;/li&gt;
&lt;li&gt;A request can time out even when the operation succeeds.&lt;/li&gt;
&lt;li&gt;Your application can process an event but fail before acknowledging it.&lt;/li&gt;
&lt;li&gt;Two identical requests can execute concurrently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mistake is designing your application around the assumption that one request equals one business operation.&lt;/p&gt;

&lt;p&gt;HTTP does not guarantee that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency is your first line of defence
&lt;/h2&gt;

&lt;p&gt;An operation is idempotent when executing it multiple times produces the same effective result as executing it once.&lt;/p&gt;

&lt;p&gt;For webhook processing, this means identifying an event that has already been processed and refusing to repeat its business effects.&lt;/p&gt;

&lt;p&gt;A straightforward implementation might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processPaymentWebhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webhookEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUnique&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;eventId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PROCESSED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;duplicate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Process the event...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there is a concurrency problem here.&lt;/p&gt;

&lt;p&gt;Two identical webhook requests could arrive almost simultaneously. Both might check the database before either has inserted the event.&lt;/p&gt;

&lt;p&gt;Both see nothing.&lt;/p&gt;

&lt;p&gt;Both proceed.&lt;/p&gt;

&lt;p&gt;This is why checking for an existing record alone is insufficient.&lt;/p&gt;

&lt;p&gt;You need a database-enforced uniqueness constraint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhook_events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;provider_event_id&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;received_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="n"&gt;NOW&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;processed_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The unique constraint ensures that concurrent requests cannot successfully register the same provider event twice.&lt;/p&gt;

&lt;p&gt;Your application should then use an atomic insert or transaction to claim the event before executing its business effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate event receipt from business execution
&lt;/h2&gt;

&lt;p&gt;Another mistake is treating webhook receipt and business processing as the same operation.&lt;/p&gt;

&lt;p&gt;They should be separate concerns.&lt;/p&gt;

&lt;p&gt;A more resilient architecture looks like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Receive the webhook.&lt;/li&gt;
&lt;li&gt;Verify its authenticity using the provider's signature mechanism.&lt;/li&gt;
&lt;li&gt;Validate the event structure.&lt;/li&gt;
&lt;li&gt;Persist the event with a unique provider event ID.&lt;/li&gt;
&lt;li&gt;Acknowledge receipt.&lt;/li&gt;
&lt;li&gt;Process the event asynchronously.&lt;/li&gt;
&lt;li&gt;Record the processing outcome.&lt;/li&gt;
&lt;li&gt;Retry failed processing safely.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach prevents a slow downstream operation from holding up the webhook response.&lt;/p&gt;

&lt;p&gt;For stronger reliability, use a transactional outbox pattern or an equivalent durable job mechanism. Simply returning HTTP 200 after placing a job in an in-memory queue is not sufficient. If the application crashes before the job is persisted, the event can be lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency is not just about webhooks
&lt;/h2&gt;

&lt;p&gt;This is where things become particularly interesting.&lt;/p&gt;

&lt;p&gt;Imagine a customer clicking the Pay button multiple times because the page appears unresponsive.&lt;/p&gt;

&lt;p&gt;Or a mobile application retrying a request after losing connectivity.&lt;/p&gt;

&lt;p&gt;Or a merchant's POS terminal submitting a transaction again because it never received the first response.&lt;/p&gt;

&lt;p&gt;The same principle applies.&lt;/p&gt;

&lt;p&gt;For operations that create financial or irreversible effects, introduce an idempotency key.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/v1/payments
Idempotency-Key: order_123_payment_attempt_01
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your backend should associate that key with the original operation and its result.&lt;/p&gt;

&lt;p&gt;Subsequent requests using the same key should return the original result rather than initiating another payment.&lt;/p&gt;

&lt;p&gt;Be deliberate about the key's scope, retention period, and payload consistency. Reusing the same key with a different payment amount or currency should not silently return an unrelated result.&lt;/p&gt;

&lt;h2&gt;
  
  
  The other problem: Out-of-order events
&lt;/h2&gt;

&lt;p&gt;Suppose your payment system receives these events:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;payment.successful&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;payment.pending&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In that order.&lt;/p&gt;

&lt;p&gt;If your implementation blindly updates the payment status based on the latest event received, you could accidentally downgrade a successful payment to pending.&lt;/p&gt;

&lt;p&gt;Arrival order is not necessarily business-event order.&lt;/p&gt;

&lt;p&gt;Your system needs explicit state transition rules.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allowedTransitions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SUCCESSFUL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FAILED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;SUCCESSFUL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;FAILED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is deliberately simplified. Real payment systems may support additional states, reversals, refunds, disputes, and provider-specific transitions.&lt;/p&gt;

&lt;p&gt;The important principle is that a state transition should be validated against the current state, not blindly accepted because an event arrived.&lt;/p&gt;

&lt;p&gt;For financial systems, you should also maintain an immutable transaction history or ledger rather than relying exclusively on a mutable balance or status field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build for failure, not just the happy path
&lt;/h2&gt;

&lt;p&gt;When building payment-enabled applications, I pay particular attention to questions that are easy to overlook during initial development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when the provider processes a payment but our request times out?&lt;/li&gt;
&lt;li&gt;What happens when the same webhook arrives five times?&lt;/li&gt;
&lt;li&gt;What happens when two workers process the same event?&lt;/li&gt;
&lt;li&gt;What happens when the database commits but the application crashes before responding?&lt;/li&gt;
&lt;li&gt;What happens when our system and the provider disagree about a transaction's status?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last question deserves special attention.&lt;/p&gt;

&lt;p&gt;You need reconciliation.&lt;/p&gt;

&lt;p&gt;Your application should periodically compare internal transaction records against the payment provider's records, identify discrepancies, and initiate a controlled resolution process.&lt;/p&gt;

&lt;p&gt;Webhooks are useful for receiving updates. They should not be your only source of financial truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Building a payment integration is relatively straightforward.&lt;/p&gt;

&lt;p&gt;Building one that remains correct when networks fail, requests are duplicated, workers crash, and events arrive in unexpected orders is a different engineering problem entirely.&lt;/p&gt;

&lt;p&gt;The difference is not necessarily the number of technologies you use. It is the assumptions you make about failure.&lt;/p&gt;

&lt;p&gt;A production-grade payment system should not depend on requests arriving exactly once, responses arriving on time, or external services behaving perfectly.&lt;/p&gt;

&lt;p&gt;Because eventually, they won't.&lt;/p&gt;

&lt;p&gt;And when that happens, your architecture should already know what to do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real test of a payment system isn't whether it works when everything goes right. It's whether it remains financially correct when everything goes wrong.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>architecture</category>
      <category>backend</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
