Hands On Kafka Course

Hands On Kafka Course

Day 10: Delivery Guarantees - Building Exactly-Once Payment Processing

SystemDR's avatar
SystemDR
Sep 07, 2025
∙ Paid
2
2
Share

What We're Building Today

Today we're implementing exactly-once delivery guarantees for StreamSocial's payment processing system. You'll build a robust billing service that ensures every user payment is processed exactly once - no double charges, no lost transactions.

High-Level Goals:

  • Implement exactly-once semantics for payment events

  • Build idempotent payment processors

  • Create transactional Kafka producers/consumers

  • Design payment reconciliation mechanisms

Core Concepts: Delivery Guarantees in Kafka

The Three Guarantees

At-Most-Once: Messages may be lost but never duplicated. Like sending a letter without tracking - it either arrives once or gets lost.

At-Least-Once: Messages are never lost but may be duplicated. Like persistent email - you're guaranteed delivery but might get duplicates during retries.

Exactly-Once: Messages are delivered precisely once. The holy grail for financial systems where duplicate payments could bankrupt users.

Why Exactly-Once Matters for Payments

In StreamSocial's billing system, a duplicate payment event could charge a user twice for their premium subscription. Traditional distributed systems struggle with exactly-once delivery because network failures, crashes, and retries create uncertainty about message delivery status.

Context in Ultra-Scalable System Design

StreamSocial's Payment Architecture

Your payment service sits between the user interface and external payment processors (Stripe, PayPal). When users upgrade accounts or purchase virtual gifts, payment events flow through Kafka topics with exactly-once guarantees.

Component Placement:

  • User actions trigger payment events

  • Kafka ensures exactly-once delivery

  • Payment processors handle idempotent operations

  • Analytics systems consume payment data safely

Real-World Application

Companies like Netflix process millions of subscription payments monthly. A single duplicate charge creates customer support tickets, refund processing, and trust issues. Exactly-once semantics prevent these cascading problems.

Architecture: Exactly-Once Implementation

Transactional Producers

Kafka's transactional producers wrap message production in ACID transactions. If the producer crashes mid-transaction, Kafka rolls back uncommitted messages, preventing partial writes.

Producer Transaction Flow:
1. Begin transaction
2. Send payment event to billing-topic
3. Send analytics event to metrics-topic
4. Commit transaction (atomic)

Idempotent Consumers

Consumers use Kafka's consumer groups with automatic offset management inside transactions. This ensures consumption and processing happen atomically.

The architecture shows transactional producers sending to multiple topics while consumers process messages exactly once using coordinated transactions.

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 SystemDR
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture