Redis learning Manual (master-slave replication)

Source: Internet
Author: User

I. redis replication:

The first thing to note here is that it is too easy to configure the master-slave mode in redis. I believe that you can easily read this blog. Here we will first list some theoretical knowledge, and the actual operation cases will be given later.
The following list clearly explains the features and advantages of redis replication.
1). the same master can synchronize multiple slaves.
2). Server Load balancer can also accept connections and synchronization requests from other Server Load balancer instances. This effectively distributes the synchronization pressure of the master node. Therefore, we can regard redis's replication architecture as a graph structure.
3) The master server provides services for slaves in a non-blocking manner. Therefore, during master-slave synchronization, the client can still submit query or modify requests.
4). The slave server also completes Data Synchronization in a non-blocking manner. During synchronization, if a client submits a query request, redis returns the data before synchronization.
5). In order to load the read operation pressure of the master, the slave server can provide the read-only operation service for the client, and the write service must still be completed by the master. Even so, the scalability of the system has been greatly improved.
6) The master can save the data to slaves, thus avoiding the need for an independent process in the master to complete this operation.

Ii. How replication works:

After slave is started and connected to the master, it will send a sync command. After that, the master will start the background storage process and collect all commands received for modifying the dataset. After the background process is executed, the master will transmit the entire database file to the slave, to complete a full synchronization. The slave server saves the database file data and loads it into the memory. After that, the master continues to send all the collected modification commands and the new modification commands to slaves in sequence. slave will execute these data modification commands this time to achieve the final data synchronization.
If the connection between the master and slave is disconnected, slave can automatically reconnect to the master, but after the connection is successful, a full synchronization will be automatically executed.

Iii. how to configure replication:

See the following steps:
1) Start two redis servers at the same time. You can consider starting two redis servers on the same machine to listen on different ports, such as 6379 and 6380.
2) run the following command on the slave server:
/>Redis-cli-P 6380# Assume that the slave port number is 6380.
Redis 127.0.0.1: 6380>Slaveof 127.0.0.1 6379 # Assume that the master and slave are on the same host, and the master port is 6379.
OK
The above method only ensures that after the slaveof command is executed, redis_6380 becomes the slave of redis_6379. Once the Service (redis_6380) is restarted, the replication relationship between them will be terminated.
If you want to ensure the replication relationship between the two servers for a long time, you can make the following changes in the redis_6380 configuration file:
/> CD/etc/redis # Switch the directory where the configuration file of the redis server is located.
/> Ls
6379. conf 6380. conf
/>VI 6380. conf
Set
# Slaveof <masterip> <masterport>
Change
Slaveof 127.0.0.1 6379
Save and exit.
This ensures the redis_6380 service. Program After each startup, the replication connection with redis_6379 will be automatically established.

Iv. Application Example:

Here we assume that the master-slave has been created.
# Start the master server.
[Root @ Stephen-PC redis] # Redis-cli-P 6379
Redis 127.0.0.1: 6379>
# All the keys in the current database of the master.
Redis 127.0.0.1: 6379> Flushdb
OK
# Create a new keys in the master as the test data.
Redis 127.0.0.1: 6379> Set mykey hello
OK
Redis 127.0.0.1: 6379> Set mykey2 world
OK
# Check which keys exist in the master.
Redis 127.0.0.1: 6379> Keys *
1) "mykey"
2) "mykey2"

# Start the slave server.
[Root @ Stephen-PC redis] # Redis-cli-P 6380
# Check whether the keys in the slave are consistent with those in the master. From the result, they are equal.
Redis 127.0.0.1: 6380> Keys *
1) "mykey"
2) "mykey2"

# Delete one of the test keys in the master and view the deleted results.
Redis 127.0.0.1: 6379> Del mykey2
(Integer) 1
Redis 127.0.0.1: 6379> Keys *
1) "mykey"

# Check whether mykey2 has been deleted in slave.
Redis 127.0.0.1: 6380> Keys *
1) "mykey"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.