Back to blogYouTube Video

Published September 4, 2025

Building a Redis clone in Go - Part 1 (TCP server, RESP protocol)

Can't play the video or having issues? Here's the direct link.

AI Summary

This video provides a step-by-step guide to building the initial foundation of a Redis clone using Go. The focus is on establishing a TCP server and implementing the Redis Serialization Protocol (RESP) to communicate with official Redis clients.

Key Takeaways

  • Redis is an in-memory key-value store that uses a specific protocol called RESP (Redis Serialization Protocol) for communication between the client and server.
  • A basic Redis server can be initialized in Go using the `net` package, specifically `net.Listen` on the default Redis port 6379.
  • RESP messages are structured with specific prefixes: `*` for Arrays, `$` for Bulk Strings, and `+` for Simple Strings.
  • To handle Redis client requests, the server must parse these prefixes to determine the data type and length of the incoming message before processing the actual value.

Description

Full Playlist: https://youtube.com/playlist?list=PLTGiYd8gFivgrd_INfVrFDRuBBHfiTxRP&si=lKRTLS38vN4iWqAQ Source Code: https://github.com/hassanaziz0012/go-redis-video LINKS Website: https://www.hassandev.me My Book: https://www.hassandev.me/designing-websites X / Twitter: https://x.com/nothassanaziz

Transcript

Auto-generated transcript
Alright guys welcome back to another video. I'm going to in this video We're going to build a Redis clone inside Golang. So Redis you probably already know about that It's a kind of like an in-memory database a database that you store in memory It's basically a key value store and we're going to build a clone of it in go. Alright So first of all, let me just create a project folder for this. I'll just call this go Redis video and I'll CD into this and open up my VS Code window inside this folder. So first what we need to do is install Redis. Alright so I already have it installed so let me just uninstall it just so I can show you what the process is like right. So let's uninstall this whole thing and now I'm just going to open up the Redis docs and install it all over again alright so obviously if you're on Windows you go to the Windows section over here if you're on Mac you go to the Mac section but since I'm on Linux on WSL I'm going to follow these instructions alright so all you have to do is just copy paste these commands and you can install Redis I'm I'm just going to do all of this real quick and yes, override it because I already did this before. There we go. Super simple copy and paste, alright? There we go. So you've installed Redis now, alright? Make sure you do this as well so you can follow along. Now what we need to do next is, so Redis, when you install Redis, it comes alongside a client and a server, alright? So what we're going to do is use the Redis client that we get from this installation and create our own Redis server, alright? So our server is going to be able to communicate with any Redis client, alright? Because we're going to satisfy the required protocols and everything. So what you want to do first is stop the actual Redis server that's running right now. This is the official server that comes along with this Redis installation All right, we want to stop this because we're going to create our own Redis server in place of this so make sure you stop this and If I try out Redis CLI right now, this is the client. It's not gonna work right because the server is down So now we can actually start creating a server First of all, let's just create a main dot go file over here I'll call this package main I'll add a main function and what I'm going to do right now is just build a basic TCP listener alright so using the net package I'm gonna say net dot listen on the TCP network on localhost port six three seven nine so this is the default port where the Redis server is hosted alright so we're just going to copy this one make sure we handle the error and if If we can't connect to this port then that means we can't even run anything right so let's just exit the program right there with an error message Cannot listen on For six three seven nine there we go Now we have a basic TCP listener on this on this port right so now what we need to do is accept connections over here so let's say con which is connection and error equals L dot except L is the listener obviously what this is going to do is it's going to block the code until a connection is open on this port all right again handle the error as well let's print the error and exit the program over here all right if there's an error now we have a connection what we can do now is we can read everything in here I'm just going to build a very basic ready server for now right it's not going to do anything because I just want to get the basics out of the way first all right so what we need to do now is create a small buffer I'm just going to make it a thousand and twenty four bytes all right so one whole KB we pass that in over here as you can see this function requires a byte slice so we pass in a buffer over there and then we can actually just print what we get from this buffer convert it into a string obviously and then just print it and now we can write back to the client a message that it will understand so for now just copy and paste this i will explain what's going on but for now just copy and paste plus okay backslash r backslash n all right and obviously it's giving me an error because this needs to be in bytes so convert this to a byte slice like this that's it all right uh up here i forgot to do this but make sure you add a defer to close this listener when you exit the program same for this connection as well there we go just so we're freeing up the port and freeing up the resources that we're using right so let's actually play around with this now let's see if we actually made this properly I'm going to run I forgot to create a mod file okay so I forgot to create a mod file so let's do that first we'll call this go redis video there we go now let's run this again okay so it's listening on that port now on 6379 so let me open up redis cli and there we go this time you see we don't get the connection refused error right the client can actually connect to the server properly and you can see over here what happened is it sent so the client sent a bunch of commands and we printed all of them over here now this obviously doesn't make much sense right now because this is in REST format. So REST stands for Redis Serialization Protocol, all right? All of the messages that the Redis client sends and the Redis server sends are going to be in this REST format, all right? Redis Serialization Protocol. What we need to do is we need to parse this protocol and convert it into a format that we can understand and then we just need to store data, retrieve data, and do all of those basic things all right so instead of exiting the program over here what we can do is have an infinite loop let's just say there we go so this by doing this at least the server won't like shut down after just one message all right so let's run the client again and then just try sending a bunch of commands right like get name, set name to Hassan. That's my name. Get name again. And you can see in every single instance, it's just sending this okay message that we that we're sending over here, right? It's not sending anything else, because that's, that's all we've coded for now. And you can see, every time I send a message over here in this in this client, the server prints all of these like weird letters and everything. Alright, so this is what we actually need to understand now, because this is the REST protocol and this is what we need to implement if we want to create a Redis server. So let's go through this. You can see that every single one of these messages starts with a star symbol. The star symbol means array. So every single message that the client is going to send to the server is going to be an array that is filled with a bunch of commands. So let's create our first data type up here. I'll create a type called value type which is going to be a string all right and then I'll create a bunch of constants all right so the first one is going to be array which is going to be of type star all right this little star you see over here now after the star this two means how many elements are in this array all right we don't need to worry about that right now let's just keep defining our data types. After that, you see this dollar sign? The dollar sign means a bulk string, right? A multiple word string, basically. And every single message that the client sends to the server is going to be an array that is filled with a bunch of bulk strings. So let's handle that as well. Let's create a data type for bulk as well. This is going to be what's going to be sent in the command. And let's also create a value type for string. Now string is represented by this plus sign over here, alright? You can see I'm sending plus, okay, the letters okay, and then backslash r, backslash n, alright? So this plus sign means a string, simply a string of one word. So let's handle that as well. The symbol for this is obviously going to be the plus sign, there we go. So now we have a bunch of value types Now let also create a struct called value which we will use to actually convert this whole message to a format that we can understand Alright so I create a struct I give it a type which can just be value type I'll also create fields for bulk which is just going to be a string another field for string and also a field for array which is just going to be a list of value types there we go all right so now we have our value struck all right now we need to actually convert these little messages over here into this value struck so I mentioned before that every single message that the client is sending is going to be an array right so now let's create a method to read those arrays I'm going to create a pointer receiver for this new value struct that we created and I'll call it read array all right there we go and what this is going to do is it's going to take a reader so reader will just be an IO dot reader interface and I'm going to go down here and instead of reading the connection over here and printing all of this what I can do is I can just call or I can create a new value and give it the type of array because every single message again every single message that the client is sending is going to be an array all right so we can just hard code that over here then we can just do read array and pass in the connection because the connection satisfies the IO dot reader interface because it implements the read method all right so it satisfies the reader interface so we can just pass in the connection here directly now we have the connection over here now we can read the bytes that the message is sending that the client is sending and then just parse it and convert it into a value all right so let's do that first let's create a buffer that had that can store four bytes and then read those bytes all right so this is going to convert this first line over here all right the star and the two let me just maybe I can maybe I can keep this over here just so it's easier to see there we go so to read this first line we need to store at least two bytes all right so let's read this now the data that we want to fetch from this buffer is in the second element this is going to be the actual length of the element so let me just print one of the messages again I guess or let me just finish this and I'll explain I'm not making much sense right now so we create a buffer that can store about four bytes right exactly four bytes and then we read those four bytes from the connection all right now there's no need to print this right now I guess but we can't what we can do is we can we can now get the length of the array right so to get the array length we'll just say array length and error equals str con dot at toy which ascii to integer i guess that that's what it stands for and the string over here is going to be string and then the second element in the buffer this is the array length all right so you saw that the the first line over there was something like star two, right? And then just a new line, right? Like this. This is why we need to store it in four bytes, right? So one byte, two byte, and three and four, there we go, right? And the length of the array is in this second byte. So that's what we fetch over here, we convert it to a string from bytes. And then we convert that string into an integer. So make sure you handle the error as well. For now, I'm just going to print the error, right? Because I don't really care. There we go. And then let's just use this array length to then parse the rest of the array. All right. So what we can do is say for range array length. Now, again, I mentioned that the messages that the client sends are just an array, right? Now that array, and I said this before as well the array that the client sends is just going to be a bunch of bulk strings right so now we need to create a method to read those bulk strings so what i'm going to do is going to i'm going to say i'm going to say bulk equals v dot read bulk and we need to actually create this method obviously and i pass in the connection again as the reader over here so now obviously we need to implement this read bulk method but we gonna receive the bulk value over here and then I append it to this array in the original value all right pass in bulk over here and then you can just return this value as such actually we don't we don't even need to we don't even need to return it because we're modifying the pointer directly right so we don't need to return it the changes are going to reflect in that value anyway what we can do now is we can create another pointer receiver and this is going to be read bulk it's going to take another reader as well and this is actually going to return a value all right and we're going to store that value inside this array all right so let's do that next so this is going to be a bit similar to the array first we need to actually create a buffer just to store four bytes and again we're just going to read those bytes from the connection then we're going to get the length of the bulk string all right so this is again just going to be the second element i'll just call this and again we'll just convert this to an integer the second element there we go make sure we handle the error again normally here you would return the error so that the caller can handle it in whatever way it chooses to but for now since this is not a serious program since we're just trying to get this over with I'll just print the error and return all right return nil or just return an empty value. There we go. And now we can actually use this end value to read the bytes. So let's create another buffer for this bulk string. This is going to have a capacity to read n bytes, which is the length of the string, and then two more, which will be the new line characters, r and n, right? So we need to make sure that we read those as well. So we add a Capacity for two extra bytes, then we just read that and then we have the bulk string now we need we need to convert this to a bulk string and we also need to just Remove those last two new line characters, right? So for that we can just do this or not this but More like this, right? So this is going to remove the last two elements and just give us the string itself and then we can just create a value right you're the type of bulk and assign the bulk value over here awesome there we go so now we have all this done what we can do now is we can read the array and then just I guess print it and just see what we get right so I'll run the server again and okay it can't listen on 6279 let me stop the actual ready server I did this before as well but I my computer crashed and when I restarted it I guess the server just restarted on autostart or something about who cares make sure that the ready server is stopped and then launch your own server now it's running now let's run the cli there we go i'll pass in a bunch of commands as such and you can see now we're getting the actual commands over here right and we can just read these we have them stored in in like a format that we can understand in this value struck okay so now you can see that instead of having those weird strings in resp format right ready serialization protocol instead of that now we can actually parse those strings into this value struct that we can actually understand right so now that we've gone we've gotten this far i'm going to end the video in the next part i'm going to build commands like get set exists delete and others other commands like that right so the actual key value store the actual database stuff storing and retrieving data that's what we're going to build in the next part in this first part of the video i just introduced you to redis and the redis serialization protocol how it looks like how it works and we just built a bunch of methods to actually parse that resp message from the client and just understand it a bit more right so this is good progress for part one in the next part we're going to handle all of the storing and retrieving of data right so stay tuned for that thank you for watching like subscribe all of those wonderful things thank you for watching and bye

Share this article

All great things started with a conversation

If you've got a cool project or opportunity and you want me to be a part of it, set up a free meeting with me here, and let's talk. 😊