With the persistence feature, Redis guarantees that there will be no loss (or small loss) of data even if the server is restarted. However, because the data is stored on a single server, if the server's hard disk fails, it can also cause data loss. To avoid a single point of failure, we want to replicate multiple copies of the database to be deployed on different servers, even if one server fails and other servers continue to serve. This requires that when the database on one server is updated to automatically synchronize the updated data to other servers, Redis provides the process of synchronizing the replication (replication) functionality automatically.
Configuration method
The configuration file is added from the database configuration file slaveof master-ip master-port
, the primary database does not need to be configured
When starting Redis-server with command-line arguments, use the command-line arguments--slaveof master-ip master port
Redis-server--port 6380--slaveof 127.0.0.1 6379
by commandSLAVEOF master-ip master-port
Redis>slaveof 127.0.0.1 6379
SLAVEOF NO ONE
Can be the current database stop receiving synchronization from other databases, and go to the primary database
Redis Master-slave replication
Features of Redis master-slave replication:
1. A master can have multiple slave
2. Multiple slave can be connected to the same master, and can also be connected to other slave
3. Master-slave replication does not block primary, while synchronizing data, Master can continue to process client requests
4. Improve the scalability of the system
Redis master-slave replication process:
1.slave Connect with master, send sync Sync command
2.master starts a background process, saves the DB snapshot to a file, and the master master process starts collecting new write commands
and cached.
3. After the background has finished saving, send this file to slave
4.slave Save this file to your hard disk
Configure the master-slave server:
Configuring the slave server is simple, just add the following configuration to the slave configuration file:
Slaveof 222.27.174.98 6379//Specify the IP and port number of Master's host
Masterauth 888888//password for host database
We can use the info command to see if the local Redis is the primary or the server.
See if role is master or slave via info Replication and related information
Advantages and Application Scenarios
Read-write separation enables read-write separation to increase server load capacity through replication. In a common scenario, the frequency of reading is greater than the write, when a single-machine redis cannot cope with a large number of Read requests (especially the more resource-intensive requests, such as the sort command, etc.) can be established through the replication function from the database, the primary database only write operations, and from the database is responsible for read operations.
Persistent persistence from a database is often relatively time-consuming, in order to improve performance, you can create one (or several) from the database through replication, and enable persistence from the database while disabling persistence in the primary database. Restarting the base database when it crashes from the database automatically synchronizes the data so there is no need to worry about data loss. When the primary database crashes, you need to use the slaveof NO one command from the database to promote the service from the database to the primary database, and then use the slaveof command to set it to the new primary database from the database after the original primary database is started, and then synchronize the data back.
Redis from Database Configuration