Why this matters
Distributed systems must handle incoming messages efficiently. Messages arrive and need to be stored temporarily before processing.
Storage choices directly impact performance, memory usage, and system stability.
A queue provides ordered, temporary storage with predictable behavior.
Queue Data Structure
A queue is a data structure used to store messages in memory.
It follows the first-in, first-out (FIFO) principle.
The first message added is the first one removed.
Queue Types
Queues can be bounded or unbounded.
An unbounded queue has no size limit.
It may use all available memory.
A bounded queue has a fixed capacity.
This capacity is set when the queue is created.
Implementation Options
Queues can be implemented using linked lists or arrays.
A linked list implementation is straightforward.
It requires a pointer to the first node and a pointer to the last node.
This allows fast insertion at the tail and deletion from the head.
Array-based implementation is less efficient.
Adding elements to the end is easy.
Removing elements from the beginning requires shifting all elements.
This operation takes linear time and is slow.
Ring Buffer
A ring buffer is a specialized array-based queue.
It tracks two positions: read and write.
The read index marks the next position to read.
The write index marks where the next element is added.
When either index reaches the end, it wraps back to zero.
This creates a circular traversal.
When full, two options exist:
-
Overwrite the oldest data
-
Throw an exception
Use Cases
Ring buffers are used in logging.
Log data is stored in memory.
It is written to disk periodically by a background process.
Ring buffers are used in Hadoop MapReduce.
Mappers generate key-value pairs.
These are stored in memory using a ring buffer.
When the buffer reaches a threshold, a background thread copies data to disk.
This process is called spilling.
Questions
- What is the first-in, first-out principle in a queue?
- How does a bounded queue differ from an unbounded queue?
- Why is shifting elements in an array inefficient?
- What are the two positions tracked in a ring buffer?
- What happens when a ring buffer is full?
- How does a ring buffer improve logging performance?
- What is the process called when data is copied from memory to disk in Hadoop?
- Why might a mapper use a ring buffer instead of writing directly to HDFS?
Key takeaways
- A queue stores messages in memory using FIFO order.
- Queues can be bounded or unbounded.
- Linked lists offer efficient insertion and deletion.
- Array-based queues require shifting elements, which is slow.
- A ring buffer tracks read and write indices.
- Ring buffers wrap around when indices reach the end.
- When full, a ring buffer either overwrites or throws an exception.
- Ring buffers are used in logging and Hadoop MapReduce.
- Spilling copies data from memory to disk in Hadoop.