Messaging and Event Handling
What this lesson covers
Asynchronous processing gives a client a confirmation at once and the result later. A pizza shop keeps taking orders while pizzas are being made. The client does other tasks during that time, and the pizza maker orders the tasks in the queue by priority. Then the shop becomes a chain. If one shop goes down in a power outage, its delivery orders are sent to the other shops. A list in memory does not allow this, so the list is stored in a database. With four servers, a notifier checks each server for a heartbeat every 10 or 15 seconds and distributes the orders of a dead server to the rest. Load balancing with consistent hashing stops two servers making the same pizza. A message queue takes tasks, persists them, assigns them to a server, and reassigns them when the server does not acknowledge in time. RabbitMQ is an example. In a publisher subscriber model a service publishes a message to a message broker, and the broker sends it to the subscribers. The broker persists the message and replays it when a subscriber is back up. The services are decoupled, delivery is guaranteed at least once, and a new service only registers with the broker. The model has two disadvantages. There is no atomicity, so two transfers from one account leave 100 where 0 was expected. And there is no idempotency, so a replayed message debits 50 again unless it carries a request ID.
- Asynchronous processing gives a client a confirmation at once and the result later
- A list of pending orders needs persistence, so it is stored in a database
- A task queue holds the assignment, load balancing, heartbeat and persistence in one thing
- A message broker persists a message and replays it when a subscriber is back up
- Publish-subscribe has no atomicity across services, and a replayed message needs a request ID