Setup Redis Replication
In this article, i will setup Redis replication with 1 master and 3 replicas.
First, create the project folder:
$ mkdir RedisTutorial
$ cd RedisTutorial
$ mkdir RedisM RedisS1 RedisS2 RedisS3
RedisM is the master folder, RedisS1, RedisS2 and RedisS3 are replica folders.
Thus, we need to add config file in each folder
In the master redis.conf file, we need to modify port, dbfilename, dir, logfile. For instance, my master server will run on
Now, I have setup three replicas. These three run on ports 6381, 6382, and 6383. I will show how to create a sample replica, and the rest will be done similarly.?
The config of RedisS1:
For replica, we need to configure the replica section to modify replicaof <ip server> <port>. In my case, I setup it on my local machine, so the IP is 127.0.0.1 and the port is 6380.
I had successfully set up four instances, now I start with the command:
$ redis-server ./redis.conf &
We must cd to each folder and execute the above command, with the "&" indicating that it should run in the background. After a successful run, the log file of replicas will look like this:
Let's try some operations on the Redis Master
Go to the replica and retrieve the database size. I already had 250 keys, so I created a new key called color with some colors. Then I will check on one replica.
We can see that the color has successfully synced from the master to the replica instance.
To ensure that the replica is read-only, I try to set a new key, and the result is:
Conclusion
In this article, I explained how to set up a replica in Redis. I hope this helps anyone who needs it.
Thanks for reading.
?Chuyên gia t? v?n gi?i pháp ph?n m?m hàng ??u cho doanh nghi?p
8 个月r?t hay
--
8 个月tks
? Data Science student at Hanoi University of Science
9 个月Useful tips
Good to know!
?Database Administrator at Wecommit Vi?t Nam
9 个月Very helpful!