A fixed-size queue that wraps around and overwrites the oldest data. Fast, simple and never grows.
A ring buffer (or circular buffer) is a fixed-size queue stored in an array. Two numbers, a head (where to read) and a tail (where to write), move forward and wrap around to the start when they reach the end. It never grows and never shifts items.
tail, then tail = (tail + 1) % size.head, then head = (head + 1) % size.A normal queue that grows needs memory to be allocated and freed, and a simple array queue has to shift every item forward each time you remove one. A ring buffer avoids both. It reserves N slots once and then reuses them in a circle. When the writer reaches the last slot, the next write goes back to slot 0, because the reader has probably already freed it.
The wrap-around comes from the modulo operator: (i + 1) % N turns N into 0. If N is a power of two (8, 16, 1024...), the computer can replace the slow modulo with a fast bit mask: i & (N − 1).
When head == tail, the buffer could be completely empty or completely full, because both pointers are in the same place. There are three common fixes: keep a separate count, keep a one-slot gap (the buffer is full when tail + 1 == head, so one slot is always unused), or let the pointers count up forever and only use modulo when indexing (then full is tail − head == N).
| Policy | What happens | Good for | Watch out for |
|---|---|---|---|
| Overwrite oldest | New item replaces the oldest unread one | Logs, metrics, video frames, latest-value feeds | Slow readers silently lose data |
| Reject new | The write fails or returns false | Work that must not be silently lost | The producer must handle failure |
| Block | The writer waits until the reader frees a slot | Pipelines with back pressure | Can stall the producer or deadlock if misused |
| Grow (not a ring any more) | Allocate a bigger array and copy | Unknown peak sizes | Gives up the fixed-memory promise |
| Trick | Empty when | Full when | Cost |
|---|---|---|---|
| Keep a count | count == 0 | count == N | One extra number to update |
| Waste one slot | head == tail | (tail + 1) % N == head | One slot is never used |
| Pointers that only go up | tail == head | tail − head == N | Use 64-bit counters so they never overflow |
Writes (2/tick) are faster than reads (1/tick), so the buffer will fill up and then lose its oldest items. Items are numbered in the order they were written, so overwritten items are always the lowest numbers. A fixed speed per tick stands in for real timing, so the run is the same every time. The simulation stops after 60 ticks.
write: buf[tail & mask] = x; tail++ · read: x = buf[head & mask]; head++ · full: tail − head == N| Term | Meaning in easy words |
|---|---|
| Ring buffer | A fixed-size queue in an array whose indexes wrap around |
| Circular buffer | Another name for a ring buffer |
| Head | The index of the oldest item, where the next read happens |
| Tail | The index where the next write happens |
| Modulo | The remainder after division, used to wrap an index back to 0 |
| Mask | A bit trick for modulo when the size is a power of two |
| Overwrite | Replace the oldest item when full |
| Back pressure | Making the producer slow down because the buffer is full |
| SPSC | Single producer, single consumer: the simplest lock-free case |
| MPMC | Many producers, many consumers: needs atomics or locks |
| Lock-free | Threads cooperate through atomic operations, with no mutex |
| Disruptor | A high-speed ring-buffer design for passing events between threads |
| Choice | We gain | We lose |
|---|---|---|
| Overwrite when full | Producer never blocked | Old data silently lost |
| Reject or block when full | No data loss | Producer can stall or fail |
| Power-of-two size | Fast mask instead of modulo | May waste some memory |
| Bigger buffer | Survives larger bursts | More memory and more delay |
| Lock-free SPSC | Very low latency | Harder to reason about memory order |
head == tail means both. Use a count or a gap slot.