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 just such a waste of time to look at those things. But, 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 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 new set commands to the AOF file. We're going to clear the file content. Then we're 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 problem that this guy is talking about is that we write some of the commands to the AOF buffer over here, right? To the 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 function. So we're 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 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, 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 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 AOF 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 AOF 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. I'm going to stop the Redis server that's currently running, and now I'm going to run this one. 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 Redis 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 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 web 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 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 going to put this video in the playlist over here, and you guys can check that out.
And, 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, 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 of 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.