Track: APIs and back end · Lesson 35 of 41 · 18 min
Async systems: queues, events, webhooks and eventual consistency
How to test when the result doesn't appear right away: message queues, retries, duplicates, event order, webhooks and eventually consistent data.
When the response is "202 Accepted"
In modern systems many actions run asynchronously: placing an order puts an event on a queue, the warehouse service processes it, the notification service sends an email. The API responds right away and the result shows up seconds later. A "click — check" test doesn't work here.
What can go wrong
| Risk | Test scenario |
|---|---|
| Message lost | Stop the consumer, send the event, start it again — the event is processed |
| Duplicate | Send the same event twice — inventory is reserved once |
| Wrong order | "Canceled" arrives before "Paid" — the final state is still correct |
| Poison message | An invalid event doesn't block the queue and lands in the dead letter queue |
| Endless retries | A limited number of attempts with backoff |
| Long wait | The user sees "processing", not an error or stale state |
How to test asynchronous behavior
Polling with a timeout, not sleep:
def wait_for(fn, timeout=15, step=0.5):
end = time.monotonic() + timeout
while time.monotonic() < end:
result = fn()
if result:
return result
time.sleep(step)
raise AssertionError(f"condition not met within {timeout} s")
order = api.post("/orders", json=payload, token=user.token).json()
wait_for(lambda: api.request("GET", f"/orders/{order['id']}", token=user.token).json()["status"] == "reserved")
The test waits exactly as long as needed and fails with a clear message if the state never arrives.
Idempotent consumers
Most brokers (Kafka, RabbitMQ, SQS) guarantee "at least once" delivery — so duplicates will happen. The check: duplicate an event (through a test producer or a webhook replay) and confirm there's a single side effect — one email, one charge, one record.
Webhooks
A webhook is an HTTP request from an external service to you (a payment provider, a shipping service, a CRM).
- Signature. A request without a valid signature is rejected — otherwise anyone can "pay" for an order.
- Retries. The provider retries the webhook if you don't answer with a 2xx — processing must be idempotent.
- Order. Events can arrive out of order — the final state is decided by the state logic, not by "whatever arrived last".
- Delay. What does the user see while the webhook hasn't arrived yet?
For local webhook testing, use tunnels (ngrok and similar) or test request catchers. Stripe CLI can forward and replay test webhooks.
Eventual consistency
"Eventually consistent" data means that state differs between services for a while. What matters for QA:
- know the acceptable window of inconsistency (seconds? minutes?) — it's a requirement, so clarify it (requirements analysis);
- check that the UI never shows contradictory data (the cart is empty, but the order "doesn't exist yet");
- look for mismatches in the database once things have settled (SQL for testers).
Observability
A correlation ID should pass through every service and queue — then one ID shows the event's whole path. Distributed tracing tools (OpenTelemetry, Jaeger, Datadog APM) make this visible.
Key takeaways
- An asynchronous result is checked by polling with a timeout, not a fixed pause.
- "At least once" delivery means duplicates — consumers must be idempotent.
- A webhook without a valid signature must be rejected.
Check yourself
1. Why is sleep(10) a bad idea in tests of async processes?
2. The broker guarantees at-least-once delivery. What must you check?
3. Where should an invalid message that can't be processed go?