Setup Redis Replication

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

  • port 6380
  • dbfilename dump_redis_m.rdb
  • dir /opt/homebrew/var/db/redis
  • logfile "/tmp/redis_m.log"

Redis master server will run on port 6380
config dbfilename
config rdb file location
config log file


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:

  • port 6381
  • dbfilename dump_redis_s1.rdb
  • dir /opt/homebrew/var/db/redis
  • logfile "/tmp/redis_s1.log"

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.

config replica

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.

Ph?m V?n Dan

?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

回复
Th?ng ?ào T?t

? Data Science student at Hanoi University of Science

9 个月

Useful tips

Manh Vu Dinh

?Database Administrator at Wecommit Vi?t Nam

9 个月

Very helpful!

要查看或添加评论,请登录

Bùi Minh Hoàng的更多文章

  • Rank in MySQL

    Rank in MySQL

    Introduction to MySQL RANK() function The RANK() fucntion assigns a rank to each row within the partition of a result…

  • Fuzzy Search in Django with PostgreSQL

    Fuzzy Search in Django with PostgreSQL

    In real life, sometimes we want to search football but have the wrong typo, like footbal or fotball, but Google or…

    2 条评论
  • Why does offset make your query slow?

    Why does offset make your query slow?

    When working with Pagination, whenever i receive an API request with a URL like: /employee?page=10&count=10, my sql…

    4 条评论
  • Composite index and include columns in PostgreSQL

    Composite index and include columns in PostgreSQL

    In PostgresSQL, both composite indexes and indexes with included columns are used to enhance query performance, but…

    7 条评论
  • Yields and List in Python

    Yields and List in Python

    In this article, i will compare two common ways to return a sequence of values from a function is using yield to create…

    1 条评论
  • Django ORM: prefetch_related

    Django ORM: prefetch_related

    In previous article, i mentioned select_related to minimize time hit database in Django, so in this article, i addition…

    1 条评论
  • Django ORM select_related.

    Django ORM select_related.

    In Django, select_related are designed to stop the deluge of database queries that are caused by accessing related…

  • Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    In previous article, i mentioned using select_for_update() to solve concurrent transactions. select_for_update() can be…

    6 条评论
  • Using select_for_update() to solve concurrent transactions

    Using select_for_update() to solve concurrent transactions

    The Bank Account Transfer Problem A classic example of concurrent transaction issues is the bank account transfer…

    5 条评论
  • Transaction in Django ORM

    Transaction in Django ORM

    Django’s default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a…

    1 条评论