Back to blogYouTube Video

Published December 9, 2025

Building a Redis clone in Go - Part 12-1 (Fixing rewrites)

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

AI Summary

This video provides a critical bug fix for the AOF (Append Only File) rewrite process in a Go-based Redis clone. The author addresses an issue where commands received during a background rewrite were being lost.

Key Takeaways

  • The original implementation correctly rerouted new AOF commands to a memory buffer while the main AOF file was being rewritten to prevent data corruption.
  • The bug was that the author forgot to write the accumulated contents of that memory buffer back into the AOF file after the rewrite process finished.
  • The fix involves using the `WriteTo` method to flush the buffer's contents into the AOF file (`aof.f`) before switching the routing back to the main file.
  • The error handling for this specific write operation is logged rather than returned to ensure the system still completes the essential step of rerouting future records back to the file.

Description

Book a call: https://calendly.com/itshassanaziz/discuss-a-project ==== ==== ==== A viewer of mine pointed out an issue in Part 12 of my Go Redis project series. I recorded this video as a solution to that problem. Hope this helps you guys. If you find any other issues in any future video, please let me know in the comments and I'll do my best to fix them. 💙 ==== ==== ==== 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 Portfolio: https://www.hassandev.me/work YouTube: https://www.youtube.com/@itshassanaziz?sub_confirmation=1 My Book: https://www.hassandev.me/designing-websites X / Twitter: https://x.com/nothassanaziz

Transcript

Auto-generated transcript
What's up boys, how is everyone? I'm inside the GoRedisClone project codebase. This is a project that I built ages ago on my YouTube channel. The reason I'm back in here is because in my video in part 12 over here, I have an issue that I need to fix and one of my viewers was kind enough to tell me about it. I need to actually disable one of these extensions over here. I'm blocking the YouTube feed and comments and everything using this extension because it's it's just such a waste of time to look at those things but uh yeah so this guy over here commented and let me also zoom in so you guys can see this clearly I guess this much should be enough I don't know yeah hey guys wonderful series thank you I followed along really well just a question i get that we are using buffer as a temporary storage for set commands so he's talking about the rewrite function over here this um rewrite function we use this in the bg rewrite aof command over here right to rewrite the aof file in the background right that's where we use this function and let's see what he's saying why aren't we rerouting those buffer commands back into aof file after rewriting yeah so let me walk you through this function we want to basically when the bg rewrite aof command is being run we want to write all of the or route all of the new aof commands to a memory buffer like the one we create over here and then the buffer will hold all of the new aof commands and meanwhile we're going to write all of the um new set commands to the AOF file. We're going to clear the file content. Then we going to write all of the set commands into the AOF file And this is just going to be a much smaller and cleaner AOF file in the end right after this whole rewriting process is finished and we flush the output to the file on disk and everything and then finally we reroute all future AOF records back to the file so instead of writing to the memory buffer we write directly to the file again right all of this is perfectly fine the the problem that this uh that this guy is talking about is that we write some of the commands to the aof buffer over here right to the memory memory buffer over here whilst the actual aof file is busy now once we're once the file has been freed up over here we should also write the commands in the buffer to the aof file so we don't miss anything right and that is one step that i forgot to implement in this in this function so we're going to do that right now. This is going to be very very simple guys. We just need to say if underscore error equals b which is the buffer and then we can use a built-in function over here called write to and then pass in a writer over here which for our for our case is just going to be aof.f that is the file that we want to write to and then let's make sure the error is nil as well and if there is an error then we can just print that and say something like I don't know aof rewrite write buffer error and then just print the error over here and that's all we need to do so now everything is going to work perfectly fine we're going to reroute the um aof records to the buffer clear the file contents write all of the new set commands to the file and then also write the contents of the buffer to the file and then we're pretty much done then we just reroute the future AYF records back to the file so we don't use the buffer anymore and then we're done now notice I'm not I'm only logging the error over here I'm not trying to I not trying to return the function or anything like that I simply logging the error over here why because we still need to reroute the future af records back to the file and not to the buffer right so that is all we needed to do this is going to be quick a very quick and short video guys this is all we needed to do just these three lines of code um and now let's also test this out as well so it's been so long since i opened this project i've kind of just forgotten how to use it uh i'm gonna stop the redis server that's currently running and now i'm going to run this one uh there we go pretty sure that's how you do it right yeah there we go okay and then i guess i'm going to run a client over here say ready cli redis um cli what is all of this is this a new feature in vs code it's giving me some intellisense or something in the terminal that makes no sense anyway okay new connection accepted let's say bg rewrite aof there we go and that works awesome and it should be over here as well there we go yep i can obviously create a bunch of new um keys as well let's say name is aaron i don't know let's say the job is youtuber and i can run the rewrite a left command again and that will store it all over there make sure to write the buffer as well perfect and now while this rewrite process is running we can continue to set new keys and all of that and they will be stored in the buffer right in the memory buffer over here and we will be able to write that buffer to the file once the rewriting process over here is finished so that's all i wanted to talk about today i just wanted to give a quick reply to this guy and to anyone else who might encounter the same issue i'm gonna put this video in the playlist over here and you guys can check that out and uh yeah by the way while i still have your attention Thank you so much I want to thank you so much for giving me so much support in this project series This is the most popular playlist on my channel. It's gotten hundreds of views. And just thank you so much. Because this is the most popular series on my channel. And it's great to see that you guys are loving it. And you guys are getting some sort of value out of it. So thanks for all the love and support. and um yeah if you guys encounter any more issues in this playlist any more like um problems in the code that i wrote or something like that comment like this guy did before where did where do you go where do you go where do you go yeah just post a comment like this guy ask me a question or anything and i will make sure to reply back to you either in the comments or if i need to i'll record a video for you guys just like i did over here right so with that being said this is all we needed to do the video is over thank you so much for watching like comment subscribe all that wonderful stuff and i will see you in the next video actually as i record this react just got hacked like a couple hours ago i guess a day ago or something and i'm actually learning more about this as well basically react and next.js got hacked and the situation is not looking very good so i was just watching this video and i thought i would record one for you guys as well just explaining the fix i made in this go redis project and yeah i'm just rambling at this point you guys go ahead implement this fix and continue watching the project playlist continue building the go Redis clone in your own way learning all about Go and Redis and all those wonderful things. Keep following my channel. I'm gonna go and watch this video and learn more about whatever happened to React and Next.js. Gonna end the video now. Thank you so much for watching. Like, comment, subscribe and all those wonderful things. See ya!

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. 😊