Redis Master Slave Replication using Docker Compose | Redis

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello people welcome back to my channel and let's learn about reddis Master Slave replication using Docker compose so redis Master Slave is a system design Paradigm where basically you have a redish master DB and then a sleeve is connected to it so that you can replicate the data from the redis master and in case of any crash or let's say the full system shutdown of your master or some issue in your master server the data is still safe and you can use that data from the slave system in your hobby project also you need a redis Master Slave if you want to design it in that way so this video is for that one like how to quickly instantiate a redis Master Slave application using Docker compose so there are some prerequisites to it so first one is Docker must be installed and then Docker compose must be installed I've already created this block post installed Docker on your machine where I have listed down different operating systems on which you can install Docker the list is small now but I'll add later on like whenever a new version is released or for older versions I'll add it here and if your system is satisfying the condition like you have a Windows 11 and you want to install Docker then you can follow this block post so what we are basically going to do is there is a master redis instance where the data is being inserted and then we will establish a continuous data replication to your slave instance so basically two redis containers or you can say two redis instances will be deployed one will be called as m master and the second will be called as slave and there will be a replication of data so let's say I insert something into the database one that is Master instance so same data will automatically get replicated to the slave instance and there's a service which is basically writing the data and reading the data from this master instance so what we're going to do here is there is a host operating system let's say you are you are using a Windows machine and that is your main host operating system once you install talker so it creates a vir environment inside your host operating system and you can deploy containers within it so what we are going to do we'll be deploying a redis master container and that Master container will be exposed on the port 6379 inside the docker envirment and same will be mapped to the 6379 port on the host operating system we'll install another redish slave that will be same red instance but it will be called a slave and this will be exposed on the port 6479 so the default Port of redis is 6379 so we are uh changing it because one port can be taken up by only one process or only one application so that's why we are changing the port to 6479 inside the docker environment and same outside the docker environment you can change this outside Port whatever you want to put it is up to your will but here what we are saying we'll put 6479 outside also on the host operating system and inside also 6479 then we'll establish a replication within the these two redis instances that is whenever a new data is pushed to the redis master same will be replicated to the slave I have also created a full block post here that is how to do redis Master Slave application so if you want to copy the data something from here so you can use it like everything has been explained in detail here so what we are seeing here is first we need to create a new file that will be redis Master Slave replication Docker compos the name can be anything but I am giving this name but the yaml suff is important that is the uh extension of the file should be a yaml file let's go to the terminal here and here I'll just paste the command and it will create a new file for me so this is if I do LS you can see that the new file is present here now let's go to the intellig I'm using this as my ID integrated development environment you can use anything let's say Eclipse Visual Studio code anything now what I'm going to do is I will come here inside the YouTube playground I will open this folder so if I open this folder you can see the file is listed here that is redis Master Slave Docker compos so file is listed here and we will start writing the docker compos configurations so what will be the uh thing the first thing that we need to Define is the version of the docker compose syntax that we are going to use so the version will be equal to three so we will be using the version three of Docker compos next we will be defining the services that is slave service and the master service so I'm writing it as services and below this we will Define the services so the first service that we are going to Define is redis master and the image we are going to use is redis and the tag is 6.0.7 you can use uh the latest version also it is up to your will you can increase the version if there's a new uh release from the reddish side you can do it let's go to this one like how I found out this image so for that we need to go to the browser here and inside the browser I'll just say dockerhub so Docker Hub is the repository where basically all Docker images images are hosted so I'll just say uh redis and it will give you the redis uh repository and you can use this Docker pull radi or anything like you can go through the different tags that are present like they have released 7.2.4 so but what we are using is the 6.0.7 version the container name I'm again giving it as redis master start always like even if there is a crash it will restart and then I'm defining a new volume then it is a name given Rus master and that will basically be the slash data directory which is present inside the container that when we will deploy this redis master inside the docker environment a container will be created and the redis master volume on the host operating system will be mounted to this the port here defined is 6379 on the host same inside the docker environment now I'm going to define the reddish slave so I'll just paste the configuration here and you can see that uh the reddish slave is the second service that we are going to Define we can put a enter here so that it is clearly visible so the second service is redish slave and the image again we are going to use the same one Rus 6.0.7 it will again create the radius instance using 6.0.7 version the container name will be R slave it will always restart same way the volume name will be R slave on on the top it was redis Master and this will again mount to the container of the slave like there will be two containers and this one redish slave volume will be mounted to the slash data of the reddish slave container this time we are changing the port as 6479 the default Port is 6379 but only one port is available for only one application or one container so we need to change the port 6479 and again inside here it will be 6379 so I think we have done a mistake here also it will be 637 79 which is inside because it will be a container uh so all containers which red is Define exposes Port 6379 so this will be 6379 so what I'm going to do here is like 6479 is 79 is on the outside host operating system and 6379 is inside now the most important command here is redis server slave of redis Master 6379 so when this container will start or like when it will try to create itself so it will run a command that is redish server is the binary and then hyphen hyph slave off which is a option and then we need to provide the red uh host name and the port so red is Master since inside the docker container we can call a container with the name of the service that becomes the host name so that's why redis master and 6379 so once this command runs so what will happen this red is slave will automatically get connected to the redis master as a slave and now we are also defining the volumes that is red is master and red is slave so this is the configuration that we need to do and let's now start the container so I'll go to the terminal here I'll do a LS let us see that the file is updated so I'll say cat and this is okay working fine now what I'm going to do is I'll start the docker compose with this file so what we can do is we'll say Docker compose hyphen f the file name and up I'm using only up because I want to see the logs at the front end but before doing this let's see the state of our Docker so Docker PS there is no Docker container and then Docker PS no Docker container running Docker PS hyph a there's no Docker container which is exited also so everything looks fine so Docker compose hyph F the name of the file and up and what it will do it will basically try to create the container since I have already downloaded this image previously if I go to uh the second terminal I say Docker images ready so you can you can see red is 6.0.7 image is already downloaded so that's why during this Docker compose up we didn't pull any image it was it was present in the local and that's why created it from there itself so this is the log for redis master this is the log for redis slave and I think it is saying Master replica sync finished okay so I think the it is ready to accept connections also so everything is working fine now let me clear it okay I'll divide the terminal into two parts left part will be for master and the right part will be for the slave so let's now connect to this one the red is master so for connecting what we are going to do redish slave hyphen Edge and then the host name sorry the IP address of my local machine because this is from the host machine I'm going to connect to my Docker container so 127.0.0.1 and the port will be 6379 because the port which we are exposing on the host machine for the mastery 6 379 and for the slave is 64 uh uh 6479 so that's why for the left side is Master I'm putting it 6379 now on the right side I will again do the same thing but this time I'm going to put the port as 6479 so you can see the red is is connected okay now let's say we will say key star so it is a empty set and here also on the right side if I do key star which is empty set so in radius if you to uh give the command key star it will list down all the keys now let's try to create a new key and that will be set name equal to code with Raj ranen okay so I'll hit enter and what it will do it will give me a okay like okay the command is successful now let's do get name and here you can see that code with Raj ranan is coming because we did a set now what will happen here because it is a slave of this this the right hand side 6479 uh redish container is a slave of the left hand side we didn't ran any command on the slave but still if we do key star so you can see the key is already present here name and if I do get name it will give me code with Raj ranan so see the magic this is the magic that happens when you have slave so what happens why we create this slave and this is very important as a system design Paradigm in deploying the production application because you don't want to lose your data in production so what what you need to do is like you need to have multiple slaves of your master so and also in multiple reasons so that if your master goes down then the slave can be promoted to a master and still the application will run without any data loss now let's do flush all which will do what which will remove all the keys so again if I do key star on the left hand side which is on the master so it will do keyar and if I do key star again here it will be empty now let's do a set on the right hand side that is on the slave so set name equal to test so what it will do it will say read only you can't write against a readon replica so you can't set anything or update anything in the replica or you can say the slave redus whatever thing that are present on the master will automatically get replicated so I think you have understood the redish replication like setting up a master and slave within minutes you uh using the docker compose and this can help you in your hobby projects also and in the production system also so hope you like this video hope you like my Channel please share these videos with other people and if you want to help me if you want to support me then I given the link in the description you can go and support me thank you
Info
Channel: CodeWithRajRanjan
Views: 197
Rating: undefined out of 5
Keywords: redis master slave, redis slave, redis replication, redis master slave replication, redis master slave docker, redis master slave docker compose, system design
Id: 4MMtuDzxmuE
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Sun May 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.