Advanced Redis features: security and master-slave Replication

Source: Internet
Author: User

Security

Set the password to be used before other settings are made after the client connects.

Warning because redis is quite fast, an external user can try a K password in one second on a better server, this means you need to specify a very powerful password to prevent brute-force cracking.

 
 
  1. # requirepass foobared
  2. requirepass beijing

Next we will conduct an experiment to illustrate how redis security is achieved.

The connection password is beijing.

Start a client and give it a try:

 
 
  1. [root@localhost redis-2.2.12]# src/redis-cli  
  2. redis 127.0.0.1:6379> keys *  
  3. (error) ERR operation not permitted  
  4. redis 127.0.0.1:6379> 

The permission is too small. We can set a password in the current window.

 
 
  1. redis 127.0.0.1:6379> auth beijing  
  2. OK  
  3. redis 127.0.0.1:6379> keys *  
  4. 1) "name"  
  5. redis 127.0.0.1:6379> 

You can also specify a password when connecting to the server, as shown below:

 
 
  1. [root@localhost redis-2.2.12]# src/redis-cli -a beijing  
  2. redis 127.0.0.1:6379> keys *  
  3. 1) "name"  
  4.  redis 127.0.0.1:6379> 

We can see that a password can be specified during connection.

Master-slave Replication

Redis master-slave replication is easy to configure and use. Master-slave replication allows multiple slave servers to have the same database copies as the master server.

1. redis master-slave replication features:

(1) The master can have multiple slave instances.

(2) Multiple Server Load balancer instances can be connected to the same master or other Server Load balancer instances.

(3) master-slave replication does not block the master. During data synchronization, the master can continue to process client requests.

(4) Improve system scalability

2. Master-slave redis replication process:

After configuring slave, slave establishes a connection with the master and then sends the sync command. Whether it is the first connection or re-connection, the master will start a background process, save the database snapshot to the file, and the master process will start to collect new write commands and cache. After the background process writes a file, the master sends the file to slave. slave saves the file to the hard disk and loads it into the memory. Then the master forwards the cached command to slave, in the future, the master will send the write command received to the slave. If the master node receives synchronous connection commands from multiple slave instances at the same time, the master node starts only one process to write database images and sends them to all slave instances.

3. How to configure

It is easy to configure the slave server. You only need to add the following configurations to the slave configuration file.

 
 
  1. Slaveof 192.168.1.1 6379 # specify the master's ip address and port

The following is an experiment to demonstrate how to build a master-slave environment:

 
 
  1. # slaveof <masterip> <masterport> 
  2. slaveof localhost 6379 

We start the master database (Port 6379) on a machine, slave database (Port 6378)

After the master database is started, the console log is as follows:

 
 
  1. [root@localhost redis-2.2.12]# src/redis-server redis.conf   
  2. [7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12  
  3. [7064] 09 Aug 20:13:12 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  
  4. [7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379  
  5. [7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use  
  6. [7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use  
  7. [7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789  
  8. [7064] 09 Aug 20:13:20 * Slave ask for synchronization  
  9. [7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC  
  10. [7064] 09 Aug 20:13:20 * Background saving started by pid 7067  
  11. [7067] 09 Aug 20:13:20 * DB saved on disk  
  12. [7064] 09 Aug 20:13:20 * Background saving terminated with success  
  13. [7064] 09 Aug 20:13:20 * Synchronization with slave succeeded  
  14. [7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use 

After the startup, the log from the Database Console is as follows:

 
 
  1. [root@localhost redis-2.2.12]# src/redis-server redis.slave   
  2. [7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12  
  3. [7066] 09 Aug 20:13:20 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  
  4. [7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378  
  5. [7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use  
  6. [7066] 09 Aug 20:13:20 * Connecting to MASTER...  
  7. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync started: SYNC sent  
  8. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: receiving 10 bytes from master  
  9. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Loading DB in memory  
  10. [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Finished with success  
  11. [7068] 09 Aug 20:13:20 * SYNC append only file rewrite performed  
  12. [7066] 09 Aug 20:13:20 * Background append only file rewriting started by pid 7068  
  13. [7066] 09 Aug 20:13:21 * Background append only file rewriting terminated with success  
  14. [7066] 09 Aug 20:13:21 * Parent diff flushed into the new append log file with success (0 bytes)  
  15. [7066] 09 Aug 20:13:21 * Append only file successfully rewritten.  
  16. [7066] 09 Aug 20:13:21 * The new append only file was selected for future appends.  
  17. [7066] 09 Aug 20:13:25 - 1 clients connected (0 slaves), 547396 bytes in use 

We set a key-value pair on the master database.

 
 
  1. redis 127.0.0.1:6379> set name HongWan  
  2. OK  
  3. redis 127.0.0.1:6379> 

Obtain this key from the slave database.

 
 
  1. redis 127.0.0.1:6378> get name  
  2. "HongWan"  
  3. redis 127.0.0.1:6378> 

It indicates that the master-slave synchronization is normal.

So how can we determine which is the master and which is the slave? We only need to call the info command to obtain the Master/Slave information. We can execute the info command on the slave database.

 
 
  1. redis 127.0.0.1:6378> info  
  2. .  
  3. .  
  4. .  
  5. role:slave  
  6. master_host:localhost  
  7. master_port:6379  
  8. master_link_status:up  
  9. master_last_io_seconds_ago:10  
  10. master_sync_in_progress:0  
  11. db0:keys=1,expires=0 
  12. redis 127.0.0.1:6378> 

There is a role ID in it to determine whether it is the master database or slave database. For this example, it is a slave database, and there is also a master_link_status to indicate whether the master and slave nodes are asynchronous. If this value is up, synchronization is normal. If this value is down, synchronization is asynchronous;

Db0: keys = 1, expires = 0, which indicates that the database has several keys and the number of expired keys.

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.