<?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: Bismarck Ndou</title>
    <description>The latest articles on DEV Community by Bismarck Ndou (@ndoubismarck).</description>
    <link>https://dev.to/ndoubismarck</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%2F1561652%2Fce701563-be37-4257-996a-bf705ded2fee.jpeg</url>
      <title>DEV Community: Bismarck Ndou</title>
      <link>https://dev.to/ndoubismarck</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ndoubismarck"/>
    <language>en</language>
    <item>
      <title>How to stop double charges in Go with idempotency keys</title>
      <dc:creator>Bismarck Ndou</dc:creator>
      <pubDate>Wed, 02 Dec 2026 17:10:05 +0000</pubDate>
      <link>https://dev.to/ndoubismarck/how-to-stop-double-charges-in-go-with-idempotency-keys-2oan</link>
      <guid>https://dev.to/ndoubismarck/how-to-stop-double-charges-in-go-with-idempotency-keys-2oan</guid>
      <description>&lt;p&gt;A client sends &lt;code&gt;POST /payments&lt;/code&gt;. Your server charges the card, but the response is lost to a timeout. The client retries, and the customer is charged twice.&lt;/p&gt;

&lt;p&gt;Retrying is the right thing for a client to do. The bug is that the server can't tell a retry from a new request.&lt;/p&gt;

&lt;p&gt;The standard fix is an &lt;strong&gt;idempotency key&lt;/strong&gt;. The client sends the same &lt;code&gt;Idempotency-Key&lt;/code&gt; header on every retry of one operation. The server runs the operation once and replays the stored response for every retry. Stripe popularized this, and there is an &lt;a href="https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/" rel="noopener noreferrer"&gt;IETF draft&lt;/a&gt; for the header.&lt;/p&gt;

&lt;p&gt;I couldn't find a Go library I liked, so I built one: &lt;a href="https://github.com/ndoubismarck/go-idempotency" rel="noopener noreferrer"&gt;go-idempotency&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;mw&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;idempotency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"POST /payments"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;createPayment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mux&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;Now retry with the same key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i -X POST localhost:8080/payments -H &lt;span class="s1"&gt;'Idempotency-Key: 8e03978e'&lt;/span&gt; -d &lt;span class="s1"&gt;'{"amount":100}'&lt;/span&gt;
curl -i -X POST localhost:8080/payments -H &lt;span class="s1"&gt;'Idempotency-Key: 8e03978e'&lt;/span&gt; -d &lt;span class="s1"&gt;'{"amount":100}'&lt;/span&gt;
&lt;span class="c"&gt;# second response: same body, plus "Idempotent-Replayed: true"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your handler ran once. The second response came from the store.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the middleware does
&lt;/h2&gt;

&lt;p&gt;For each protected request with a key, it fingerprints the request (method, path, query, body) and checks the store:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First time this key is seen&lt;/td&gt;
&lt;td&gt;Handler runs, response is stored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same key, same request, finished&lt;/td&gt;
&lt;td&gt;Stored response is replayed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same key, same request, still running&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;409 Conflict&lt;/code&gt; with &lt;code&gt;Retry-After&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same key, different request&lt;/td&gt;
&lt;td&gt;&lt;code&gt;422 Unprocessable Entity&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handler returns 5xx or panics&lt;/td&gt;
&lt;td&gt;Nothing is stored, the key is released&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last row is deliberate. A server error is usually temporary, so the client should be able to retry the same key and get a fresh attempt.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hard part: concurrency
&lt;/h2&gt;

&lt;p&gt;A naive version looks like "check if the key exists, run the handler, store the result". That has a race: two retries arrive together, both see no key, and both run the handler.&lt;/p&gt;

&lt;p&gt;The fix is to make the first step atomic. The middleware asks the store to create an "in progress" record, and only one request can win. Redis uses a Lua script for this, and PostgreSQL uses a single atomic statement.&lt;/p&gt;

&lt;p&gt;Then there is a subtler problem. What if the winning handler is slow, and its lock expires? A retry takes over and finishes first. When the slow original finishes, it would overwrite the newer result.&lt;/p&gt;

&lt;p&gt;To prevent that, every request gets a random &lt;strong&gt;lock token&lt;/strong&gt; when it takes the lock. Only the holder of the current token can complete or release the key. The slow request's late write is rejected, and the newer result stands.&lt;/p&gt;

&lt;h2&gt;
  
  
  A framework pitfall I hit
&lt;/h2&gt;

&lt;p&gt;Echo renders errors returned by handlers &lt;em&gt;after&lt;/em&gt; middleware finishes. If you wrap standard middleware with &lt;code&gt;echo.WrapMiddleware&lt;/code&gt;, the middleware sees a handler that wrote nothing, and stores an empty &lt;code&gt;200&lt;/code&gt; instead of the real error.&lt;/p&gt;

&lt;p&gt;So the Echo adapter renders handler errors inside the middleware, and the real error response is what gets stored and replayed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stores and structure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory:&lt;/strong&gt; for tests and single-instance services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redis:&lt;/strong&gt; for multiple instances, with automatic expiry&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PostgreSQL:&lt;/strong&gt; for multiple instances with no new infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core module has no dependencies. Each store and framework adapter is its own Go module, so importing the Redis store never pulls in a Postgres driver or Gin.&lt;/p&gt;

&lt;p&gt;There is also a &lt;code&gt;storetest&lt;/code&gt; package, a shared conformance suite. Every store in the repo runs it, and you can run it against your own store.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to get right on your side
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Generate a fresh key per operation (a UUID v4 works) and reuse it for every retry of that operation.&lt;/li&gt;
&lt;li&gt;Scope keys per user in a multi-user API, so two customers can't collide:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;idempotency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WithKeyScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UserID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Context&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;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;ul&gt;
&lt;li&gt;This does not replace database constraints. Unique indexes are still your last line of defence.&lt;/li&gt;
&lt;li&gt;If a server dies mid-request, the key unlocks after the lock timeout and the retry runs the handler again. Side effects outside your database should be safe to repeat.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get github.com/ndoubismarck/go-idempotency
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/ndoubismarck/go-idempotency" rel="noopener noreferrer"&gt;https://github.com/ndoubismarck/go-idempotency&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Docs: &lt;a href="https://ndoubismarck.github.io/go-idempotency/" rel="noopener noreferrer"&gt;https://ndoubismarck.github.io/go-idempotency/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is v0.1.0, so the API is still open to change. If you build payment or order APIs in Go, I would like to hear what is missing or awkward. Issues and comments are welcome.&lt;/p&gt;

</description>
      <category>go</category>
      <category>idempotency</category>
    </item>
    <item>
      <title>Most businesses don't need more software. They need software that fits how they work. I build it in Go: backends, APIs, automation, and full-stack apps with Vue, Next.js, React or Flutter. 

https://www.fiverr.com/s/X0Lv2g5 

#go #webdev #backend</title>
      <dc:creator>Bismarck Ndou</dc:creator>
      <pubDate>Fri, 25 Sep 2026 10:51:25 +0000</pubDate>
      <link>https://dev.to/ndoubismarck/most-businesses-dont-need-more-software-they-need-software-that-fits-how-they-work-i-build-it-in-55od</link>
      <guid>https://dev.to/ndoubismarck/most-businesses-dont-need-more-software-they-need-software-that-fits-how-they-work-i-build-it-in-55od</guid>
      <description>&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.fiverr.com/s/X0Lv2g5" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fnpm-assets.fiverrcdn.com%2Fassets%2Fuploads%2Ffiverr-og-logo.png" height="420" class="m-0" width="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.fiverr.com/s/X0Lv2g5" rel="noopener noreferrer" class="c-link"&gt;
            It needs a human touch
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories.
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/data%3Aimage%2Fx-icon%3Bbase64%2CAAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAB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