β Communication Patterns
π Communication Patterns β Synchronous (REST/gRPC/GraphQL)
π¦ What is Synchronous Communication?
In synchronous communication, the caller waits for the response before continuing. Itβs like making a phone call β you ask a question and wait for the answer.
π§ Common Synchronous Communication Methods:
1. π REST (Representational State Transfer)
- Uses HTTP and JSON
- Simple and widely used
- Based on resources (like
/orders
,/users
) - Good for: Public APIs, CRUD operations
β Pros:
- Easy to use
- Readable URLs
- Standard HTTP methods (GET, POST, PUT, DELETE)
β Cons:
- Over-fetching or under-fetching of data
- No built-in schema validation
2. β‘ gRPC (Google Remote Procedure Call)
- Uses HTTP/2 and Protobuf (binary format)
- Fast and efficient
- Based on methods, not resources
- Good for: Internal microservice-to-microservice communication
β Pros:
- Very fast (binary + HTTP/2)
- Strongly typed contracts (Protobuf)
- Bi-directional streaming supported
β Cons:
- Harder to debug than REST
- Browser support limited
3. π GraphQL
- Query language for APIs
- You ask for exactly the data you need
- One endpoint for everything (
/graphql
) - Good for: Complex UIs and mobile apps
β Pros:
- No over-fetching or under-fetching
- Flexible queries
- Strong typing (via schema)
β Cons:
- More complex to implement
- Performance tuning can be tricky
π§ When to Use Synchronous Communication?
Use when:
- Immediate response is required (e.g., UI interactions)
- Request β Response pattern fits well
- Services are closely connected
Avoid when:
- High load may cause latency
- You need loose coupling or retries β prefer asynchronous/event-driven
π Communication Patterns β Asynchronous (Message Queues & Events)
β³ What is Asynchronous Communication?
In asynchronous communication, the sender does not wait for a response. Itβs like sending a message or email β the receiver can process it later, at their own pace.
π§° Common Asynchronous Communication Methods:
1. π¬ Message Queues
- Services send messages to a queue
- Other services pick up messages and process them
- Examples: Azure Service Bus, RabbitMQ, Amazon SQS
β Good for:
- Decoupling services
- Load buffering
- Background processing
2. π’ Events (Pub/Sub)
- Services publish events to a topic or event bus
- Other services subscribe and react to those events
- Examples: Kafka, Azure Event Grid, AWS SNS/SQS
β Good for:
- Event-driven systems
- Broadcasting changes to many services (e.g., "OrderPlaced" triggers shipping, email)
π― Benefits of Asynchronous Communication
Feature | Benefit |
---|---|
π§© Loose Coupling | Services donβt depend on each otherβs availability |
π Improved Resilience | Temporary failures donβt break the flow |
βοΈ Scalability | Queues handle high load by buffering requests |
β±οΈ Non-blocking | Fast response for sender (fire-and-forget) |
β Challenges
- Eventual consistency: Responses arenβt immediate
- Harder to debug: No direct trace like REST calls
- Message duplication or loss: Requires retry and idempotency logic
- Delivery guarantees: At-least-once, at-most-once, exactly-once
π§ When to Use Asynchronous Communication?
Use when:
- Tasks can run in the background
- You need decoupled services
- Services should react to events (e.g., OrderPlaced β SendInvoice)
Avoid when:
- Immediate feedback is needed (e.g., form submission UI)
- Too many events make flow hard to track