Back to blogYouTube Video

Published September 20, 2025

Building a Redis clone in Go - Part 8 (DBSIZE and FLUSHDB)

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

AI Summary

In this video, the instructor implements two basic Redis commands—`DBSIZE` and `FLUSHDB`—into a Redis clone written in Go.

Key Takeaways

  • **DBSIZE Implementation**: This command returns the number of keys currently stored in the database. It is implemented by locking the database for reading and returning the length of the map containing the data store.
  • **FLUSHDB Implementation**: This command deletes all keys from the database. The instructor emphasizes performance by avoiding a loop that deletes keys one by one; instead, the entire database store is reset by assigning it to a new, empty map.
  • **Concurrency Handling**: Both commands utilize mutex locks to ensure thread safety—RLock for the read-only `DBSIZE` operation and a full Lock for the write-intensive `FLUSHDB` operation.

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
Hey guys, welcome back to this Go Redis Clone series. We're gonna continue building this Redis database clone in Go. I believe this is part 8 of this series. And in this video, we're going to implement two new commands. The flushdb command, which is going to destroy the entire database, delete all the keys, right? And the dbsize command, which is going to return the size of the database. which is just the you know the length of the map in our database right so both of these are super simple commands to implement so this is going to be a very short and quick video so let's just go over to the handlers file over here and up here in the handlers list let's declare both of these commands. Let's say flushDB and what was it? DB size. There we go. And then let's declare both of these functions down here. And obviously they will take the same parameters and arguments that the other handlers take, right? So that's the flush db command and this will be the db size command. There we go. Awesome. So first let's just implement the db size command, right? So this is supposed to return the size of the database, right? So first of all, let's just lock the database for reading because we're reading the size of the database. We're not writing any new changes to it. So let's just lock it only for reading. Then let get the size of the database which is going to be the length of the database store And once we have that let unlock the reader over here and then let just return a value of type integer and the actual number here will be the size there we go and that's all we need to do for the db size command now let's implement the flush db command this is supposed to destroy the entire database, right? Now there's two approaches to this. One is a very terrible approach and the other is a very good approach. So when you think about destroying the entire database, you might make the mistake of basically just deleting every single key in the database, right? And the way to do that in Go is to just do something like for key in the database store and you run the delete function on it right and i guess over here you're supposed to pass the database store and the key like that this is i mean it gets the job done but you're looping over the entire map to delete each key individually and that's just not a good approach right in terms of performance and stuff if you have thousands of keys tens of thousands of keys or even millions of keys. There are enterprise software out there that use millions of keys in their Redis caches, right? So this is obviously the worst approach you could pick. Instead of that, let's just remove all of this and just set the DB store to a completely new map, right? A completely new and empty map that we initialize over here. And that's going to to like just remove the previous database store completely. Right. And yeah, that's all we need to do. So obviously make sure to lock the mutex over here. And in this case, we're writing new changes to the database, right? So we need to lock it for writing as well and unlock it down here when we done And then let just return a value over here of type string and the value will be okay so just a simple okay success message right and that's all we need to do to implement plush db now we've implemented both handlers and added them to the handlers list up here so now we can actually test this out let's clear everything run the go file or the go server sorry open up a client as well over here and now we can try running the db size command and it's giving me four so this is probably coming from the aof file over here where we have a bunch of different like um values over here backed up right so if i run the flush db command now it returns the ok success message and then i if i try running db size again it should give me zero and yes it does so all of these things work now both of these commands work i can try setting some new values over here like name and age and now the database size should be 2 which it is and I can obviously run flush DB and now it's going to be 0 and if I try getting name or getting the age it's gonna be nil because I flushed or deleted the database so yeah simple short video we just implemented both of these new commands flush DB and DB size in the next video or in the next couple of videos I am planning to implement more complicated features more complex features features of Redis things like database transactions and let me actually just see what I am planning to do I want to implement the hashing and check some protection in the RDB file over here so basically I want to make sure that the bytes that we encode over here are the same bytes that are written to the file right So I want to implement checksum protection over there. I also want to implement database transactions and also implement the expire and the TTL to live commands which basically what they do is and by the way the video is over this is done i'm just discussing future plans for this project i want to implement the expire and the ttl time time to live commands and these are basically going to allow us to expire or delete certain keys after some time right so we can say something like expire the name key over here right after let's say 30 seconds or 30 minutes right and it's going to automatically delete it for us so yeah things like that and also eviction policies like lru and lfu now these are memory management policies in Redis and they basically tell Redis how to handle the database when we run out of memory. So you know that Redis stores all of its data in memory by default, right? So what happens if we run out of memory, right? If the max memory is reached, that's where these eviction policies come into play and that's what we're also going to implement in one of the future parts of this series. So stay tuned for that. And yeah, lots of cool things coming to this project. But yeah, thank you for watching this part. Thank you for supporting the series so far. Lots of cool new stuff coming. And yeah, thank you for watching. Like, share, subscribe, comment, reach out, whatever, right? And I will see you in the next video. Thank you for watching and bye 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. 😊