Redis's master-slave replication function is very powerful. One master can have multiple slave instances, and one slave can have multiple slave instances. In this way, a powerful multi-level server cluster architecture is formed. Next I will demonstrate how to perform Redis data master-slave replication on multiple servers. Here I assume there are two servers, one is Windows operating system (LAN IP: 192.168.3.82), the other is Linux operating system (LAN IP: 192.168.3.90), and redis is installed on both operating systems, in Windows, run the following command to install cygwin:
$ tar xzf redis-2.2.2.tar.gz$ cd redis-2.2.2$ make
You can run the "make test" command to determine whether the installation is successful.
Here I use one master and two slave (master is in Windows, one slave is in Windows, and the other slave is in Linux). The basic process is as follows:
1. Create two directories, Demo1 and Demo2, on the Windows server. Demo1 is used to store the Master service and Demo2 is used to store the Slave service,
Modify the configuration file in the Master service:
bind 192.168.3.82
Modify the configuration file in the Slave service:
Port 6381 (Service Port numbers must be separated) bind 192.168.3.82slaveof 192.168.3.82 6379 (set the Host and port of the master)
2. Create a directory, Demo, and Demo on the Linux server to store the Slave service and modify the configuration file in the service:
Bind 192.168.3.90slaveof 192.168.3.82 6379 (set the Host and Port of the master)
In this way, all the configurations are completed.
3. Run the following commands to run these three services:
./redis-server redis.conf
To start the redis service.
Note that when I start the master and then start a Server Load balancer instance, I can find that the Server Load balancer instance is:
A sync request is sent from the Master, and it supports automatic reconnection, that is, when the master is offline, it will be in the waiting state.
On the Master:
The server Load balancer can accept the Server Load balancer response and start the persistence operation. This means that the Server Load balancer will persist the disk each time it connects to the Master node.
4. Write a client program and use the. NET Component of ServiceStack. Redis. dll:
using ServiceStack.Redis;static void Main(string[] args){ IRedisClientFactory factory = new RedisCacheClientFactory(); IRedisClient client = factory.CreateRedisClient("192.168.3.82", 6379); client.Set
("username", "leepy"); string username = client.Get
("username"); client.Save(); Console.WriteLine("username: {0}", username); Console.ReadLine();}
Running result:
When the data is Set, the data is stored in the memory. When the Save method is called, the data is saved in the disk.
Dump. rdb appears in the three service directories, indicating that all Master files are synchronized to Slave.
Use the UE editor to open the file and view it:
From the Redis source code, we can find that the rdb file is implemented using the lzf compression algorithm. By default, the lzf compression algorithm is enabled.
In this way, you can use other client programs or Web platforms to read data from the Slave Disk Database, truly achieving read/write splitting.