The hidden trap in simple queues (and how to fix it)


Peter Mbanugo

pmbanugo.me

Hello Reader,

I've published a new blog post and it is about QUEUES.

You have used a queue today. In fact, you have probably used one dozens of times before lunch.

Whether it is Spotify loading songs in order, your server processing background jobs, or just waiting at the supermarket checkout, the FIFO (First-In, First-Out) queue is the invisible engine that keeps modern systems running smoothly.

You have used Kafka, RabbitMQ, and likely a Queue data types in your language's standard library.

But have you ever tried to build one from scratch?

In my latest article, Write Your Own FIFO Queue: An Essential Data Structure for Modern Systems, we peel back the layers of abstraction to build a high-performance queue.

Here is what is inside:

1. The "Creeping Index" Trap

If you implement a queue using a standard array, you will hit a nasty bug: your queue can run out of space even when it is half empty. I explain why this happens and how it breaks your system.

2. The Solution

We solve the memory problem using a Ring Buffer. I’ll show you the logic (and the maths) behind wrapping your data around the end of an array to achieve infinite scrolling within a fixed memory block.

3. Arrays vs. Linked Lists

"Why not just use a Linked List?" It is a common question. I dive into how your CPU cache works and why hopping around memory with a Linked List can be significantly slower than sticking to a contiguous array.

Even if you don't write Zig, the logic here applies to Rust, C, TypeScript, or any language where performance matters.

Enjoy it and let me know if you find it useful

600 1st Ave, Ste 330 PMB 92768, Seattle, WA 98104-2246
​Unsubscribe · Preferences​

Hi, I’m a Software Consultant and Technical Trainer

Subscribe to my newsletter if you want to see me write about different stack of software engineering and how to use them to build products. I write about high-performance Node.js/Bun applications, React and Astro, and my journey programming with Zig.

Read more from Hi, I’m a Software Consultant and Technical Trainer

PETER MBANUGO Programmer & Consultant Hey Reader, it's been a while. I've been busy working on Tina, the high-performance concurrency framework I've written about a few times. It's a huge rewrite (from scratch) because I've learned a few new things and I'm expanding the framework from the initial idea of "just an eventloop library", to having a few more components for building robust applications. There's still a lot to do, but I hope to share some update next month. Stay tuned! New Blog Post...

PETER MBANUGO Programmer & Consultant Hi everyone, If you’ve ever tried to build a high-performance backend, you know the pain of the "Concurrency Trilemma." You usually have to pick your poison: Callback, Promise, or Async Hell (Rust/JS) where coloured functions fragment your codebase. Mutex Hell (Go/C++) where shared-state leads to subtle race conditions and deadlocks. The GC Ceiling (Erlang/Elixir) where you get a brilliant mental model, but pay for it in garbage collection pauses and VM...

PETER MBANUGO Programmer & Consultant Hey 👋 Have you ever fixed a bug, or shipped the patch only to have it haunt you in production? That feeling exists because tests only catch scenarios you think of. There's another way to approach correctness: specify what the system should do, not how to do it — and let a computer program verify that all possible executions satisfy your requirement. That’s the idea behind formal verification, and I’ve started a new series where I’m building a new tool to...