Message Queue

Event bus, Event loop

Definition
This pattern is used to send a message from one component to one or more other components, asynchronously.
Message Queue

How does it work?

The sending component places a message on the message queue. The distributor reads the oldest message from the queue an passes it to the recipient component.

Variants

The pattern can be used within a program, but also between programs, using a specialized queue application.

Examples

  • Email: send email to one or more recipients, or a group
  • The JavaScript event loop handles messages in a single thread
  • Message queue software

When should you use it?

Use it when it takes too long to wait for a response.

Problems

  • There is an element of uncertainty introduced when messages are handled asynchronously. You can't be sure if and when they are handled.
  • If the modules share large amounts of data, it may not be a good idea to pump these over the bus all the time. If you choose to share the data between the modules, make sure no synchronization issues occur.

Common implementation techniques

  • Modules may run in the same process or in different processes (as is the case with Microservices)
  • The event bus may be part of the same process or it may be another process. It may use the internet as its substrate.

Links