First, copy the basic configuration and demo
To avoid a single point of failure, REDIS provides replication capabilities to automate the process of synchronization.
1. Configuration
The data after synchronization is divided into two categories: the primary database (master) and the one from the database (slave). The primary database can read and write, and the data is automatically synchronized to the slave database when a write operation occurs. And from the data is generally read-only, and accept the main database synchronized data.
In Redis using replication, you only need to include the "salveof primary database IP Primary database port" from the configuration file of the database, and no configuration is required for the primary database.
2. Redis Replication Demo
Below we launch two Redis instances on the same server, listening on different ports, one as the primary database and the other as from the database.
First, we want to copy a Redis service configuration file and name it redis6380.conf.
We modify the port number in the redis6380.conf configuration file to 6380, and configure the Redis instance to be the primary database from the database.
Configuration from the database
Next, I made two bat batch files that started Redis to start up two Redis databases:
The contents are:
Redis-server redis.windows.conf //Start the primary database
Redis-server redis6380.conf //start from database
Now we start the client-to-master database:
The client of the primary database is on the Redis installation directory, double-clicking Redis-cli.exe.
From the start of the database, we open cmd, enter the Redis directory, enter
In this way, we have two Redis database server and client, the following to demonstrate the functionality of replication:
First, in the primary database, enter
Then, in the client from the database, we perform the query operation
You can see the value of the Name property that is obtained from the database.
Note: In addition to setting the Slaveof parameter through a configuration file or command line, you can also modify it at run time using the slaveof command:
If the database is already from the other primary database, the slaveof command stops synchronizing with the original database and synchronizes with the new database.
Second, the principle of replication
When a database is started, the Sync command is sent to the primary database, and the master database receives the sync command to start saving the snapshot in the background and caches the commands received during the save. When the snapshot is complete, Redis sends the snapshot file and all cached commands to the database, which is received from the database, loads the snapshot files, and executes the cached commands that are received. This is done again when the master-slave database is disconnected and re-connected.
Iii. persisting from Database values
To improve performance, you can copy several resumes from the database, and enable persistence from the database while disabling persistence in the master data. Restarting the data when it crashes from the database automatically synchronizes the data so there's 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 primary data continuation service from the database, and then start the original primary database by using the slaveof command to set it to the new primary database from the database to synchronize the new data.
Iv. references
1. Redis Getting Started Guide (Li Zi)
Copy of Redis series four