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.
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.
| Option | How it works | Survives a crash? | Scales out? | Real-time? | Where I’d use it |
|---|---|---|---|---|---|
| Background thread | hand the job to a thread pool in the same process | no | no | yes | a side project. In production, a deploy restarts the pod and the jobs in memory are gone. |
| DB table + polling | insert a “to send” row; workers poll the table | yes | poorly | as fast as you poll | low volume, when a database is all you have |
| Cron job | run a script every few minutes | yes | so-so | no | the daily summary email. Never OTPs. |
| Webhook | another system calls your URL when something happens | only with retries | yes | yes | delivery reports: the SMS gateway calls back to say “delivered” |
| Serverless task | a cloud function per job | yes | auto | cold starts | spiky, low-volume jobs you don’t want servers for |
| Message queue | a broker holds jobs until a worker takes one | yes | yes | yes | “send this one SMS” |
| Event log | an append-only log that many readers follow | yes | yes | yes | “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 queue | Event log (Kafka) | |
|---|---|---|
| Holds | jobs to do | facts that happened |
| Who gets a message | one worker | every interested service |
| When it’s deleted | when it’s processed | when it’s old (7 days by default), read or not |
| Replay old messages? | no | yes: rewind and read again |
| Who remembers progress | the broker, per message | each 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?
Events stay in the log, read or not. SMS picks up where it stopped and catches up, late but with nothing lost. The payments never noticed.
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.