11 Postgres as message queue

 

This chapter covers

  • When, why and how to use Postgres as a message queue
  • Implementing a custom message queue using Postgres built-in capabilities
  • Notifying subscribers of new messages in the queue
  • Using the pgmq extension as a lightweight, ready-to-use messaging solution

A message queue is a component that enables services and other system components to communicate asynchronously. Instead of calling each other directly, one service (the producer) sends a message to the queue, and another service (the consumer) reads and processes the message later. The queue holds messages until they are processed and deleted. Common use cases for message queues include decoupling applications by using the queues as an asynchronous communication layer, broadcasting and exchanging events, and distributing jobs among worker processes.

Let’s explore when and how to use Postgres as a message queue as we build a visitor registration and tracking system for a Department of Motor Vehicles (DMV). We’ll learn how to create, manage, and work with queues in Postgres while processing new registrations and notifying visitors in line.

11.1 When to use Postgres as message queue

11.2 Building custom message queue

11.3 Using custom queue

11.4 Using LISTEN and NOTIFY

11.5 Queue implementation considerations

11.5.1 LISTEN and NOTIFY considerations

11.5.2 Indexing considerations

11.5.3 Partitioning considerations

11.5.4 Messages processing failover considerations

11.6 Starting Postgres with pgmq

11.7 Using pgmq

11.7.1 Creating and using visitors queue

11.7.2 Using message visibility timeout

11.8 Summary