Mahir Shahriar Tamim

AI Researcher · Explainable & Responsible AI · Vision, Language & Agentic Systems

Part 1 of Learning Kafka

Why my service shouldn’t wait for anyone

Module 1 · 4 min read

Why my service shouldn’t wait for anyone

TL;DR

  • If a payment calls every service in turn, it’s as slow and fragile as the worst one. Publishing one event keeps them apart.
  • A queue holds jobs that vanish once done. A log holds facts that many readers can read and replay.
  • Saving and publishing are two writes. The outbox pattern makes them one.

The obvious design

Someone sends $5 to a friend. That should trigger a push to the sender, an SMS to the receiver, an email receipt, a row in both users’ in-app inbox and an analytics event.

The first design anyone writes (me included) is a chain. The payment service calls each service, waits for each, and only then shows “Payment successful”. In production the user watches a spinner for every call added up. A four-second SMS gateway adds four seconds to every payment. If email is down, the transfer shows as failed, because the payment now depends on everything it calls.

The payment service should just do its job, answer the user, and leave a note, “payment #8812 completed”, for the others. That note is an event. Click a channel to make it slow, again to take it down.

Design
user waiting: 0.0 s Payment service idle Kafka topic payments 0 events

Try: take email down and send a payment in each design. In the Kafka design, send a few payments while SMS is down, then bring it back. The orange number is how many events that service still has to read.

With Kafka the payment confirms in a fraction of a second either way. A broken channel only delays its own notifications, and catches up later because the log keeps unread events.

Seven ways to “do it later”

Kafka isn’t the only way to put work off, or always the right one. Here’s what I’ve seen used for notifications, simplest first.

OptionHow it worksSurvives a crash?Scales out?Real-time?Where I’d use it
Background threadhand the job to a thread pool in the same processnonoyesa side project. In production, a deploy restarts the pod and the jobs in memory are gone.
DB table + pollinginsert a “to send” row; workers poll the tableyespoorlyas fast as you polllow volume, when a database is all you have
Cron jobrun a script every few minutesyesso-sonothe daily summary email. Never OTPs.
Webhookanother system calls your URL when something happensonly with retriesyesyesdelivery reports: the SMS gateway calls back to say “delivered”
Serverless taska cloud function per jobyesautocold startsspiky, low-volume jobs you don’t want servers for
Message queuea broker holds jobs until a worker takes oneyesyesyes“send this one SMS”
Event logan append-only log that many readers followyesyesyes“payment #8812 completed”, read by everyone who cares

Cron runs on the clock. Every five minutes means an OTP can wait up to five minutes. Fine for a digest, useless for a login code.

A queue is a to-do list. A log is a diary.

I filed Kafka under “message queue” for a long time. It isn’t one.

A queue holds jobs, e.g. “send SMS to +1 555…”. Each job goes to one worker and is gone once done. RabbitMQ is the classic example.

A log holds facts, e.g. “payment #8812 completed”. Push, SMS, inbox and analytics each read it, today or next week. Reading removes nothing. Retention does, by time (7 days by default), by size, or never. Kafka is the classic example.

Message queueEvent log (Kafka)
Holdsjobs to dofacts that happened
Who gets a messageone workerevery interested service
When it’s deletedwhen it’s processedwhen it’s old (7 days by default), read or not
Replay old messages?noyes: rewind and read again
Who remembers progressthe broker, per messageeach reader, as one number per partition

So the payment service publishes “payment completed” and doesn’t know which channels exist. Adding WhatsApp next month is just a new reader. The payment service doesn’t change a line. And since facts are never edited, the log only grows at one end, which is where almost all of Kafka’s speed comes from (Module 5).

The dual-write trap. Saving the payment and publishing the event are two writes. Crash in between and the money moved with no notification. Or the reverse, a “you received $5” SMS for a payment that rolled back. The usual fix is the outbox pattern. Insert the event into an outbox table in the same database transaction as the payment, and let a small relay publish it to Kafka. It’s really one write, so both succeed or fail together.

Quick check

In the Kafka design, SMS is down for 10 minutes during a busy hour. What happens to the SMS notifications from those 10 minutes?

Q&A

Doesn’t the extra hop make things slower?

One event write adds a few milliseconds. In return the payment stops waiting on five services, so “Payment successful” shows up much sooner. Notifications come a moment later, which is fine for a push or an SMS.

What if Kafka itself is down when the payment finishes?

The producer retries for a while, but in a long outage the event can’t be written. With the outbox the event is already saved with the payment, and the relay copies it over once Kafka is back. The two can never disagree.