Logo
Published on

Rebuilding Redis in Go, Part 1: Bytes, Handlers, and Resisting Autocomplete

Authors
  • avatar
    Name
    Ryan Flores
    Twitter

I've been writing C# professionally for a couple of years now, and it's comfortable, but I'm probably a little too comfortable with it, and I wanted to branch out to a second language. Go kept coming up for the kind of work I'm interested in, tooling, CLIs, backend services, and cloud. So, I did what any developer does when they want to learn something properly, I picked the hardest reasonable project I could find and jumped in. :D

That project is rebuilding Redis from scratch in Go. I'm working through a 124-lesson course on CodeCrafters, and I just finished the first section (9 lessons). Here's how it went.

Getting Used to Go

Coming from C#, Go's syntax took some getting used to. It's not hard exactly, just... different in ways that kept tripping me up.

I spent most of this section using the stdlib, especially strings and substrings. But the real issue was working with bytes. Redis speaks RESP (REdis Serialization Protocol), so before I could implement a single command, I had to reliably parse raw bytes into an argument list. That took a little while.

Once the argument extraction clicked for me, everything after it became straightforward. Parsing was the hard part. The commands themselves were just logic.

The AI Temptation

I do have to be honest about the temptation to use AI for the quick solution more than once. The answer was right there, just one prompt away!

But that would've defeated the entire point. I'm doing this to learn Go, not to complete lessons. So I hand-wrote every solution, even the small ones, and it was a lot of fun. There's still something satisfying about doing this all by yourself that you just don't get from pasting an answer.

Autocomplete was its own menace too. It kept offering frequent hints and code blocks while I was trying to think, which was more distracting than helpful when the goal is to actually learn the language. I found myself fighting my editor as much as the problems.

The Easy Wins

Once parsing worked, GET and SET took about five minutes to implement! I was even able to get the expiry time up and running right after that too. After all that byte-wrangling, it felt like things were just falling into place.

What's Next: The Refactor

After completing the base stages, I looked at my command handling and... it's definitely a bit of spaghetti code at the moment. It's all in main.go, and only a handful of functions. I can already see that this is going to get a whole lot uglier as the list of commands grow.

So, my next goal is to refactor this and replace the whole thing with a map of command names to handler functions. Adding a new command would now be one map entry.

No major issues so far, and I've been having fun.

Section 2 is next. I'll keep writing these as I go one post per section. See you in the next one!