Message Queues Explained
- Process Management
- Memory Management
- Deadlocks
- OS Introduction & Fundamentals
- CPU Scheduling Algorithms
- Threads & Concurrency
- Inter-Process Communication (IPC)
- Process Synchronization
- Virtual Memory & Paging
- File Systems
- Disk Scheduling
- I/O Management
Sending Discrete, Structured Messages Rather Than a Raw Stream
Unlike a pipe (covered in the previous note), which treats data as one continuous, unstructured stream of bytes, a message queue lets processes send and receive discrete, individually-structured messages - each message keeps its own distinct boundaries, rather than blending into a single stream.
Process A sends: Message 1 | Message 2 | Message 3
(each message stays clearly separate)
Process B receives: Message 1, then Message 2, then Message 3 - each as its own distinct unitWhy a Queue Decouples the Sender From the Receiver
A message queue exists independently of both the sending and receiving processes - a sender can place a message into the queue even if the receiver isn't currently ready to read it yet, and the message simply waits in the queue until the receiver eventually retrieves it.
Optional Message Priority
Some message queue implementations let messages carry a priority or type value, allowing a receiver to selectively read messages of a specific type first, rather than being strictly limited to first-in-first-out order the way a basic pipe is.
How This Compares to Pipes
| Pipes | Message Queues | |
|---|---|---|
| Data structure | Continuous, unstructured byte stream | Discrete, individually bounded messages |
| Message selection | Strictly first-in-first-out | Can selectively read by type/priority |
| Sender/receiver coupling | Both typically need to be active around the same time | More decoupled - messages can wait in the queue |
Where Message Queues Fit Especially Well
Message queues suit scenarios where a producer and consumer don't operate in perfect lockstep - a logging system, or a task queue where work items pile up and get processed as workers become available, are natural fits for exactly this kind of decoupled, message-based communication.