First, Redis master-slave replication features:
1.master can have multiple slave
2. Multiple slave can connect to the same master, and can also connect 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
5. You can disable data persistence in master, comment out all save configurations in the master profile, and simply configure data persistence on slave
Second, the Redis master-slave replication process:
When slave is configured, slave establishes a connection with master and then sends the Sync command. Whether it is the first connection or reconnection, master initiates a background process, saves the DB snapshot to a file, and the master master process starts collecting new write commands and caches. After the background process finishes writing the file, master sends the file to Slave,slave to save the file to the hard disk, then loads it into memory, then master forwards the cached command to the slave, and the subsequent master sends the received write command to slave. If master receives multiple slave simultaneous connection commands at the same time, master initiates only one process to write database mirroring and then sends it to all slave.
Third, Redis master-slave configuration process
1). Start two Redis servers at the same time, consider starting two Redis servers on the same machine, listening to different ports, such as 6379 and 6380, respectively. Start./redis-server local_6379.conf &./redis-server local_6380.conf & 2). Execute the command on the slave server:
/> redis-cli-p 6380
Redis 127.0.0.1:6380>
slaveof 127.0.0.1 6379
OK Redis 127.0.0.1:6380>config set Masterauth redis
#假设Master的密码为redis
OK of course master Set password for config set Requirepass Redis
The above method only guarantees that after executing the slaveof command, redis_6380 becomes the slave of redis_6379, and once the service (redis_6380) restarts, the replication relationship between them terminates.
If you want to guarantee the master-slave relationship between the two servers over the long term, you can make the following changes in the redis_6380 configuration file:
/>
VI 6380.conf
Will
# slaveof <masterip> <masterport>
Switch
slaveof 127.0.0.1 6379
Masterauth Redis
Save exit. Remove Master-Slave relationship: slaveof no one remark:
-
SLAVEOF host port
The slaveof command is used to dynamically modify the behavior of the copy (replication) feature at the Redis runtime.
By executing a SLAVEOF host port
command, you can turn the current server into a subordinate server (slave server) for the specified server.
If the current server is already a secondary server for a primary server, execution SLAVEOF host port
will cause the current server to stop synchronizing with the old primary server, discard the old data set, and start synchronizing the new primary server instead.
In addition, executing a command on a subordinate server SLAVEOF NO ONE
causes the secondary server to turn off replication and transition from the subordinate server back to the primary server, and the original data set that was synchronized will not be discarded.
By using the " SLAVEOF NO ONE
do not discard synchronous data Set" feature, you can use a secondary server as a new primary server for non-disruptive operations when the primary server fails.
complexity of Time:
SLAVEOF host port
, O (N),
N
Is the number of data to synchronize.
SLAVEOF NO ONE
, O (1).
return Value:Always return
OK
。 Iv. Example 127.0.0.1:6379> incr AB
(integer) 1 127.0.0.1:6380> Get AB
"1" 127.0.0.1:6379> keys *
1) "A"
2) "AB" 127.0.0.1:6380> keys *
1) "A"
2) "AB" 127.0.0.1:6379> del A (integer) 1 127.0.0.1:6379> keys *
1) "AB" 127.0.0.1:6380> keys *
1) "AB" 127.0.0.1:6380> del ab (Error) READONLY you can ' t write against a read only slave.
Reference:
Http://redis.readthedocs.org/en/latest/server/slaveof.html
Http://redis.io/topics/replication
Http://www.cnblogs.com/stephen-liu74/archive/2012/02/23/2364717.html
http://sofar.blog.51cto.com/353572/861276/
Redis Master-Slave configuration