Written on 2025-12-09 by Adam Drake - 9 min read

My Medium friends can read this story over on Medium.
I have a deep seated fear.
One day I will be in an interview for a Senior position. Everything will be going swimmingly and then they ask the following — “I would like you to build a rate limiter”. I will freeze up completely. I will start perspiring from places I didn’t think possible. Imposter syndrome will smack me in the face like the cold bitch she is.
I will panic. I will scream. I will run from the room a blathering mess never to touch another piece of software in my life and turn into a goose farmer. No imposter syndrome can touch me there.
I decided it was time to do something about it. I would face my fear head on and build an actual rate limiter. Deep down I know it’s the unknown that really scares me so the best thing I can do is get to know it intimately.
According to Google:
Actually something practical that can be and is used in the real world.
Go is a language I am somewhat familiar with. I don’t use it on a professional level (yet) but I aim to in the future. A few reasons why I like it:
There are different algorithms you can use when building a rate limiter.
There are more I discovered on my research but for the sake of this article I am only showing these.
The approach I chose to go with was the “Token Bucket”. This was for a couple of reasons:
Full Disclaimer: I learnt how to do this using both ChatGpt free chatbot and the newish Opus 4.5 model from Claude. I tried both models and asked many questions throughout to get a comprehensive learning as possible. For me this is one of the best usages of AI currently.
Let’s start getting into the implementation.
I started with a basic project structure in Go
ratelimiter/
├── go.mod
├── limiter/
│ ├── limiter.go
│ └── limiter_test.go
└── main.go
There are two things to consider in this approach:
With this in mind I started to lay out some code.
When it comes to laying out code for something new I like to take the “sculpture approach”. Make the basic shape first and then go around and fill in the details.
So what would I need here?
With that in mind I built out the struct first.
const precision int64 = 1_000_000 // Needed to avoid floating point issues later on
The nanosPerToken was used internally for precision to avoid floating point issues that I run into when running some tests but I don’t want to worry too much about that now.
Next up we want the function to create a new “token bucket”:
With this approach we can create different rate limiters for different use cases. If we want a really strict rate limiter we reduce the capacity and reduce the refillRate. If we want a very relaxed rate limiter we can make these values large.
One thing I am really loving about Go in the testing. They make it so easy to set up tests that it almost feels wrong not to. I set up a basic test to make sure this was working.
Let’s add the logic for refilling the bucket next:
This does the following:
Next up we want to implement the logic to see whether we allow a request or not:
What this function is doing:
We have very similar logic for allowing n requests too, in the case of a “burst” of requests
Like I mentioned earlier I love the testing functionality in Go. Here are the basic tests I wrote to make sure the functionality of the rate limiter was working:
These tests test the following
I learnt many things during this experience and I think it confirms my belief that practical experience is the best way (for me at least) to learn.
Overall thanks to this experience of actually building out a Rate Limiter I realised that it’s just like any other problem I have faced. When you think about the actual problem, break it down into smaller pieces and start to understand the details it doesn’t become so scary anymore.
This is what I love about programming. You start with something you really don’t understand. You spend time with, trying things out, asking questions, poking at it to see what happens. Then at a certain point you do understand and it’s no longer mysterious.
It does take time but that’s ok. The effort and time you spend are totally worth it at the end of the day as it brings clarity to your understanding of software.
Enjoyed This Post?
If you found this blog post helpful, why not stay updated with my latest content? Subscribe to receive email notifications every time I publish.
If you're feeling really generous you can buy me a coffee. (Btw, I really like coffee…)
I live in the vibrant city of Prague, Czech Republic, with my family. My blog is more than just articles; it's a community of like-minded developers who share a love for innovation and learning.
I'm a passionate Frontend Developer specialising in React and TypeScript. My professional journey revolves around exploring and mastering new tools and libraries within the JavaScript ecosystem.

Adam Drake is a Frontend React Developer who is very passionate about the quality of the web. He lives with his wife and three children in Prague in the Czech Republic.
Adam Drakes Site © 2025