Redis Learning Notes (iii) Redis master-slave architecture and master-slave architecture (1)

Source: Internet
Author: User
Tags redis
Ready to

Modify the Pidfile to prepare for the following

Turn off RDB persistence to modify the location where the persisted file is saved

Start Redis

Redis-server/etc/redis.conf

Using Client connections Redis

Redis-cli

The connection is successful, then can happily play ~ ~ ~ master Copy (read-write separation)

Redis Master-slave replication is very powerful, a master can have more than one slave, and a slave can have multiple slave, so down, formed a powerful multi-level server cluster architecture. Can avoid Redis single point of failure, build a read-write separation architecture, to meet the read-write less application scenarios.

Several important aspects of Redis replication functionality

1. A master can have multiple slave;
2. Redis uses asynchronous replication. Starting from 2.8, slave initiates an ACK (once per second) to confirm that the replication stream (replication stream) is processed;
3. Not only the main server can have from the server, from the server can also have their own from the server, multiple from the server can form a graph structure;
4. Replication is non-blocking in the master, which means that master can still provide query services even when multiple slave perform the first synchronization;
5. Replication at the slave end is also non-blocking: if you set up in redis.conf, slave can still use the old dataset to provide the query when the first sync is performed, or you can configure the slave to return the client a false hint when master loses contact with slave;
6. When slave deletes the old dataset and reloads the new version of the data, slave blocks the connection request (typically in the recovery phase after disconnecting from master);
7. Replication can be used simply for data redundancy (dataredundancy), or to promote extensibility (scalability) by having multiple requests for read-only commands from the server: for example, the heavy SORT command can be handed over to a subordinate node to run.
8. You can avoid performing persistent operations (Save) at the master end by modifying the master Redis.config to perform persistence on the slave side. Master and slave architecture


Prepare 3 configuration file ports, respectively

6379 (Master)

6380 (Slave)

6381 (Slave)

Modify the original redis.conf file and copy out 2 redis.conf files

mv/etc/redis.conf/etc/redis.6379.conf
cp/etc/redis.6379.conf/etc/redis.6380.conf
cp/etc/ Redis.6379.conf/etc/redis.6381.conf

Modify 6380 and 6381 profiles through VI

Vi/etc/redis.6380.conf

Replace 6379 to 6380 by command

:%s/6379/6380/g
The bottom appears to indicate a successful modification, WQ exit and save

Modify the 6381 configuration file in the same way

Note: The configuration file has previously modified the Pidfile parameter and must be modified to a different value if not modified

To start 3 Redis instances with a configuration file

Viewing the Redis process through the PS command

There are 2 methods of master-slave configuration:

Set up slaveof in redis.conf

Using the REDIS-CLI client to connect to the Redis service, execute the slaveof command this way, the master-slave replication relationship will be lost after the reboot.

Modify the configuration file for 6381, and then restart 6381.

Through REDIS-CLI into the 6379 service

View Master and subordinate information: info replication

Information displayed from the library

Test master-Slave relationship

Writes the data in the main library, then reads the data from the library to the master slave structure

The master-slave architecture is

There's a Slave underneath master, and a Slave under Slave.

Let's 6381 reboot.

Master and subordinate information at this time

There's only one master left.

Then use the REDIS-CLI client to connect to the Redis service and execute the slaveof command

View Master and subordinate information at this time

6379:

6,379 has only one from the library 6380

6380:

6380 has a main library 6379 and a 6381 from the library

6381:

6,381 has only one main library 6380

So the master-slave structure is built.

Test, empty key first

Set up data in the main library

Getting data from a library

6380 and 6381 are already getting the data.

Well, master from the structure to complete! read-only from library

By default the Redis database is read-only when it acts as a slave role and cannot be written.

If you write, you will get an error.

We can modify the configuration parameters of the Redis configuration file slave-read-only, modify to No



the process of data synchronization

① when an MS relationship is established from the library and the main library, the sync command (Sync command) is sent to the primary database;

② The Home library receives the Sync command and then begins to save the snapshot in the background (the RDB persistence process) and caches the write commands received during the period, so even if the RDB persistence is turned off, the Rdb file is generated when they sync

The above main library only does 2 things

RDB the received command for persistence

In Rdb persistence, Cache received commands

① when the snapshot is complete, the primary Redis sends the snapshot file and all cached write commands to the Redis;

When the ② is received from the Redis, it loads the snapshot file and executes the cached command received;

After ③, the main Redis sends commands from the Redis whenever it receives a write command, thus guaranteeing the consistency of the data;

In this process, the master-slave synchronization is through the RDB to synchronize the data, even if the RDB is not used, it will produce IO problems, in this replication process may have bottlenecks. Redis to implement disk-free replication in version 2.8.18

 

Redis is not stored to disk when it is initialized to copy from the library, but is sent directly to the library via the network, avoiding IO performance problems.

Modify Repl-diskless-sync to Yes
Downtime Processing

Downtime in the master-slave architecture

①slave Downtime

In Redis, restarting from the library is automatically added to the master-slave architecture, synchronizing data is automatically completed

Redis 2.8, in the context of the persistence from the library, if from the library in the period of disconnection, the main library has not changed a little, from the library to start again, the main library will not be the data or the RDB operation, but the incremental replication

②master Downtime

One: In the slave executes slaveof NO one command, disconnects the master-slave relationship and promotes the main library to continue the service;

Second: After the main library is restarted, execute the slaveof command, set it to the other Redis from the library, this time the data is synchronized back;

In this way, the manual process of Redis downtime is less, but it is still prone to anomalies, and below we use the Redis Sentinel (Sentinel) function to achieve a highly available architecture





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.