Prompt
How do I decouple services with pub/sub instead of direct API calls?
Latest observation
Oct 24, 2026
OpenAI APIWeb search: off
To decouple services with pub/sub, replace synchronous “service A calls service B” interactions with event publication:
- Service A does its own work and publishes an event like
OrderPlaced. - Service B subscribes to that event and reacts when it sees it.
- Neither service needs to know the other’s URL, availability, or internal logic.
Core idea
Instead of this:
- API request goes to Service A
- Service A directly calls Service B
- Service A waits for B to respond
Do this:
- Service A finishes its local transaction
- Service A publishes an event to a message broker
- Any interested services consume the event asynchronously
Benefits
- Loose coupling: services don’t depend on each other’s endpoints
- Resilience: if a consumer is down, the broker can buffer messages
- Scalability: consumers can scale independently
- Extensibility: new consumers can be added without changing the producer
Typical setup
Use a broker such as:
- Kafka
- RabbitMQ
- AWS SNS/SQS
- Google Pub/Sub
- Azure Service Bus
Pattern example
Direct call
OrderService -> PaymentService -> InventoryService
Pub/sub
OrderService -> publishes OrderPlaced
PaymentServicesubscribes and charges cardInventoryServicesubscribes and reserves stockNotificationServicesubscribes and sends email
Implementation tips
1. Define events, not commands
Publish facts that already happened:
- Good:
OrderPlaced,PaymentCaptured - Less ideal:
ChargeCustomerNow
2. Keep event payloads small and stable
Include only what consumers need:
{
"eventType": "OrderPlaced",
"orderId": "123",
"customerId": "456",
"total": 89.50,
"timestamp": "2026-10-24T12:00:00Z"
}
3. Make consumers idempotent
Messages may be delivered more than once, so consumers should safely handle duplicates.
4. Use retries and dead-letter queues
- Retry transient failures
- Send repeatedly failing messages to a dead-letter queue for investigation
5. Avoid hidden synchronous dependencies
If Service A still waits for Service B’s result before responding, you haven’t fully decoupled it. In that case:
- return
202 Accepted, or - use a separate workflow/saga for orchestration
6. Version events carefully
You’ll eventually evolve schemas. Plan for:
- backward compatibility
- additive changes
- schema registry or versioned contracts
When pub/sub is a good fit
Use it when:
- multiple services need to react to the same business event
- you want async processing
- eventual consistency is acceptable
When direct API calls are still better
Use direct APIs when:
- you need an immediate response
- the operation is strongly request/response oriented
- a service needs a live query for current data
Practical rule
A good design often uses both:
- APIs for queries and immediate commands
- pub/sub for domain events and side effects
If you want, I can show:
- a concrete example in your stack, or
- an architecture diagram and sample code for Kafka/RabbitMQ/SNS.