Q&A-1Choose synchronous or asynchronous integration for order submission.
Answer
Use synchronous integration for the acceptance boundary and asynchronous for fulfillment. The caller needs an immediate answer on whether the order was accepted: validation, inventory reservation, payment authorization, and a durable order ID. That path should be bounded and idempotent. Fulfillment (picking, packing, shipping, notifications) runs asynchronously with explicit status.
Assumptions: typical e-commerce order, caller is a web or mobile client, fulfillment takes minutes to days. If fulfillment is instant (digital goods), a single synchronous call is simpler. If the caller is a POS terminal that must know final state, keep it synchronous but budget a longer timeout.
Mechanism: The synchronous path does four things: validate payload, reserve inventory, authorize payment, and persist the order plus an outbox event in one transaction. The outbox event is the publication intent. A relay process polls the outbox and publishes to a queue (Kafka, SQS). The response is 202 Accepted with order ID and status ACCEPTED. The client polls a status endpoint or receives a webhook.
Why bounded? Because each dependency can fail or slow down. Set a total timeout of 2 seconds. Per-step p99 budgets:
| step | p99 budget | timeout |
|---|---|---|
| validate payload | 5 ms | 50 ms |
| reserve inventory | 40 ms | 500 ms |
| authorize payment | 120 ms | 800 ms |
| insert order + outbox | 15 ms | 200 ms |
| total | 180 ms | 2 s |
If any step exceeds its budget, return 503 with Retry-After: 1. The client retries with the same idempotency key.
Idempotency: client generates a UUID per order attempt. Store it in a unique index. Here is the PostgreSQL 15 pattern:
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
idempotency_key TEXT UNIQUE NOT NULL,
status TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
INSERT INTO orders (idempotency_key, status)
VALUES ($1, 'ACCEPTED')
ON CONFLICT (idempotency_key) DO NOTHING
RETURNING id;Trace: first call with key 'abc' inserts and returns id. Second call with same key: ON CONFLICT DO NOTHING returns no row; application then runs SELECT id FROM orders WHERE idempotency_key = 'abc' and returns the existing id. Duplicate submit is idempotent.
Async fulfillment: outbox relay polls every 100 ms. Publishes to Kafka topic orders.fulfillment with acks=all and enable.idempotence=true (both defaults in Kafka 3.0+). Consumer updates status to FULFILLING, then SHIPPED. Consumer lag alarm at 1000 messages. Dead-letter queue after 5 attempts. For SQS, set visibility timeout to 5x expected processing time (default 30 s, max 12 h).
Failure modes: client disconnect during sync call. Server may still process, so idempotency key prevents duplicate. Dependency loss: payment service down: sync path fails fast with 503; async path retries with exponential backoff. Delayed worker: consumer lag grows; detect via lag metric and alert. Duplicate submit: unique index returns same order ID.
When not to use: if the caller cannot handle eventual consistency (e.g., financial settlement), keep the whole flow synchronous with a longer timeout and compensating transactions. If fulfillment is trivial, skip the queue.
The response must be truthful: 202 means accepted, not completed. Give a correlation ID (the order ID) and a status URL.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.