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

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The Cloudflare Blog
IT之家
IT之家
雷峰网
雷峰网
小众软件
小众软件
博客园 - 叶小钗
博客园 - 聂微东
爱范儿
爱范儿
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
T
Tailwind CSS 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
Building a Payment Processor: The Final One Percent
Oreoluwa Bim · 2026-05-08 · via DEV Community

Oreoluwa Bimbo-Salami

I had idempotency, a message queue, and retries. I thought I was finally building something production-ready. Then I started thinking about the "in-between" failures.

What if the worker starts a charge, the provider succeeds, but the worker crashes before it can update the database?

The payment stays stuck in processing forever. The user was charged, but my system thinks it's still "happening." This is an orphaned payment, and it’s a nightmare to debug because, from the outside, it looks like nothing went wrong.

The Processing Lease

I solved this by treating the processing status as a lease. When a worker starts, it stamps the payment with a processing_started_at timestamp.

If a payment has been in processing for more than 2 minutes, I assume the worker died. I wrote a "sweeper" that finds these stale leases and resets them to failed so they can be picked up by the retry logic.

UPDATE payments
SET status = 'failed', next_retry_at = datetime('now', '+1 minute')
WHERE status = 'processing'
  AND processing_started_at <= datetime('now', '-2 minutes');

Enter fullscreen mode Exit fullscreen mode

The Golden Rule: Commit Before You Charge

This was the most important lesson I learned. I used to do this:

tx.Begin()
// ... update status to processing ...
provider.Charge() // DANGEROUS: external call inside a DB transaction
tx.Commit()

Enter fullscreen mode Exit fullscreen mode

This is a disaster. If provider.Charge() succeeds but the tx.Commit() fails (maybe the DB is slow or the connection drops), the transaction rolls back. My database says the payment is still pending, so the system tries to charge the user again. Double charge.

I had to flip the logic entirely:

  1. Update status to processing and COMMIT the transaction immediately.
  2. Call the provider OUTSIDE of any transaction.
  3. Update status to completed.

If step 3 fails, the sweeper will eventually see a stale processing record and reset it. Because the provider itself is idempotent (using the same key), the retry will simply return the existing success. The user is never charged twice.

Distributed Tracing (Actually seeing the mess)

As the system grew, logs weren't enough. I couldn't easily tell which API request led to which worker error.

I added OpenTelemetry to pass a Trace-ID through RabbitMQ. Now, when I look at a trace in Grafana, I can see the entire journey across different services:

Client
  |
  | POST /charge (Idempotency-Key)
  v
API
  |-- Store pending payment & Outbox entry (Transaction)
  |-- (Outbox Poller) Publish message (payment_ref, Trace-ID)
  v
RabbitMQ
  v
Worker
  |-- Acquire lease (status=processing, timestamp)
  |-- Call provider.Charge()
  |   |-- Success -> Update DB status=completed
  |   +-- Failure -> Set next_retry_at, status=failed
  v
Sweeper (periodic)
  |-- Find stale processing leases
  |-- Reset to failed for retry

Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Building a payment processor isn't about the happy path. The real engineering happens in the 1% of the time when the network dies, the worker crashes, or the database is locked.

There's more to it, of course. A real system would need circuit breakers to stop hammering a failing provider, rate limiting to stay upright, and reconciliation jobs to cross-reference the database with the provider's records every night.

The goal isn't just to make it work; it's to make it fail gracefully without losing anyone's money.

The final code is at github.com/oreoluwa-bs/dinero.