Part 10 of 11
Event-driven architectures
In this style, a process sends a request, called an event or a message, and usually does not wait for it to be handled. A go-between holds the message for a while. It is called a message broker; other names are event broker, message queue, and message-oriented middleware.
Compared with calling a service directly, a broker has several advantages:
- It buffers messages when the receiver is down or overloaded, which makes the system more reliable.
- It can deliver a message again to a process that crashed, so messages are not lost.
- The sender does not need service discovery, because it never connects to the receiver's address.
- One message can go to several receivers.
- The sender and receiver are decoupled: the sender does not care who reads its messages.
The flow is one-way: send and forget. You can still build request and response on top, by having the sender wait for a reply on a separate channel.
Message brokers
Brokers started as commercial products, then open source projects took over, and now cloud services are common too. They use two main patterns:
| Pattern | What happens |
|---|---|
| Queue | A message goes to a named queue, and one of the consumers of that queue gets it |
| Topic (publish/subscribe) | A message goes to a named topic, and every subscriber gets a copy |
A broker does not enforce any data model. A message is just bytes plus some metadata, so any encoding works. A common practice is to use protobuf, Avro, or JSON, with a schema registry next to the broker that stores the valid schema versions and checks compatibility.
How long messages last varies:
- Many brokers write messages to disk, so they survive a crash.
- Many delete a message once it has been consumed, unlike a database.
- Some can keep messages forever, which event sourcing needs.
A consumer that reads a message, changes it, and publishes it to another topic must keep fields it does not understand. It is the same unknown-fields problem as with databases.
Actors across machines
The actor model handles concurrency inside one process without raw threads, so you avoid race conditions, locks, and deadlocks.
- Each actor usually stands for one client or one thing, and holds its own private state.
- Actors talk to each other only by sending asynchronous messages.
- Delivery is not guaranteed; in some failures, messages are lost.
- Each actor handles one message at a time, so it never needs to worry about threads. The framework schedules each actor on its own.
A distributed actor framework spreads actors across many machines, using the same messaging. When a message goes to an actor on another machine, the framework encodes it, sends it, and decodes it, without the code noticing. Location transparency works better here than in RPC. Actors already assume a message might be lost, even on one machine, so a remote message only adds more delay, not a different set of failures. In effect, a distributed actor framework is a message broker and the actor model built into one.
Rolling upgrades still apply. A new machine may send messages to an old one and the other way round, so messages need forward and backward compatibility, using one of the encodings from this chapter.