“If your application is cloud-native, or large scale, or distributed and doesn’t include a messaging component, that’s probably a bug”
Tim Bray
General purpose internet software geek
How Asynchronous messaging helps in software architecture
When it comes to software architecture, there are 2 things very important
Divide and Conquer: With Microservices we already apply Divide and Conquer
Loose coupling: And through Asynchronous messaging we can achieve Loose coupling
Synchronous messaging
- Synchronous systems are tightly coupled
- Problems in downstream dependencies can have immediate impact on upstream callers, and that’s why we have Circuit breaker pattern
- Retry from upstream callers can all-too easily fan-out and amplify problems
Hence here comes the line …
“… and some things simply take too much time to wait, or are asynchronous by nature”
Application integration patterns
Message exchange
One way
- There’s a sender and a receiver
- Message channel in between can decouple both sides, so either know nothing about each other
- No response or acknowledgement received Request-response
- There’s a sender and a receiver
- 2 dedicated message channels for request and response
- Returns address and correlation ID
obvious questions
Question: How requester know where to send the response?
Answer: Return Address pattern, requester adds meta information to the request message that instructs the responder about the address of the return channel
Question: How requester know how to assign an incoming response to a previous request?
Answer: Return Address pattern, requester adds meta information to the request message with a unique ID for the request. the responder is asked to forward that Id also into the response message
Message channel
Point to point
- Implemented by Queues
- Sender to send messages into the queues, there can be multiple senders
- And there can be one to many receivers to consume messages out of the queue
- Queue acts as a load balancer in front of the receiver, which means every message from the queue is delivered to one of the receivers
- Benefits a. Easy to scale out receivers if load on the queue increasing b. Queue can persist messages c. Messages do not need to be consumed right away, queue can flatten a peak load for receiver, so queue can be called a buffering load balancer in front of the receiver d. We can also safely take receiver processes offline for maintenance work, and no danger of losing messages as they are buffered in queue
Publish-subscribe model
- One or more publisher
- Every subscriber will receive every message
- Topic is not persisted, so if any subscriber is offline, they will lose messages
- If we add another subscriber it doesn’t help with the load as all the subscribers will receive every message
AWS
- SQS for queue
- SNS for topic
- Bot hare serverless, no infra to manage
- Consume functionality through simple API
- Both can be event source for AWS lambda
- If we need strict order for our messages both (SNS, SQS) also support FIFO message ordering.
Amazon MQ
- If we are restricted messaging protocol like JMS or AMQP, there’s Amazon MQ
- Non serverless
- Managed a. Apache Active MQ b. RabbitMQ
How to scale out for Pub-sub?
Topic Queue chaining (Topic+Queue)
- Publisher at left
- Sends messages to topic
- Now we do not directly attach our consumer processes to the topic, but we put queue in between
- Then we have our consumer processes
- Benefits a. We can have fan-out functionality, b. and have consumer scale out at the same time c. edit resiliency as messages will be buffered so consumers can go offline for maintenance work
Message Routing Patterns
Message Filter
- possible that subscribers are interested for a sub set of messages, for example in above case 1 subscriber is only interested in orange messages
- dedicated topic (channel) can be added for each message types, but if there’s multiple permutations it can be tough to manage
- so here, each subscriber can individually agree with the messaging system that it is only interested in a certain set of messages. And it is done by the meta information of those messages
- Publisher stays totally unaware of these
Recipient list
- Same as Message Filter
- Recipient list is added which manages the messaging route
- Its our own code, can be put in Publisher
- Maintain the code by yourself
Scatter-Gather pattern
- For use cases where you need to distribute requests across potentially relevant or interested parties, and need to capture individual responses
- Use-cases – parallel processing
- Process a. Requests are sent to potential responders b. Responders work on their individual response c. And send it back to response queue d. Return address pattern is leveraged so responders know address of the response queue e. From there individual responses are consumed b an aggregator component f. Finally processed by a processor component g. Aggregator and processor can be separate processes or same process h. Requester can be in same application as aggregator and processor
Top comments (0)