Redis configuration Redis master-slave replication

Source: Internet
Author: User

Read Catalogue
  • Simple Introduction
  • Chapter 1: Download and install
  • Section 2: Modifying a configuration file
  • Chapter 3: Enabling the master-slave Redis Service
  • Chapter 4: Client Connection-Test synchronization
  • Chapter 5: Application Scenarios
  • Chapter 6: Reference links

Simple IntroductionThe role of Redis, can be self-search online, the following will introduce Redis master-slave replication. Configure Redis master-slave IP and port:
Master and slave Ip Port
Master 127.0.0.1 6379
Slave1 127.0.0.1
6380
Slave2 127.0.0.1
6381

Back to TopFirst, download the installation
    • Download file:
 
   
  
  1. wget http://download.redis.io/releases/redis-3.2.1.tar.gz

    • Unzip:
  
 
  1. tar zxvf redis-3.2.1.tar.gz
  2. mv redis-3.2.1 redis-3.2.1.master
  3. tar zxvf redis-3.2.1.tar.gz
  4. mv redis-3.2.1 redis-3.2.1.slave-1
  5. tar zxvf redis-3.2.1.tar.gz
  6. mv redis-3.2.1 redis-3.2.1.slave-2

    • Execute make and make test
Enter the folder separately:Redis-3.2.1.master,Redis-3.2.1.slave-1 andRedis-3.2.1.slave src directory, execute command:
  
    
 
  1. cd redis-3.2.1.master/src
  2. make
  3. make test
  4. //其他两个目录执行相同的操作
After successful execution, you will be prompted:
Back to TopSecond, modify the configuration file: redis.confThe configuration file is located at: (3 configuration files need to be modified)
Main changes 4 parameters:
    1. Port
    2. LogFile
    3. slaveof;
    4. Pidfile;
    5. Daemonize (configuration runs in daemon mode)
    • To modify the Master file: redis.conf:
Do not change other parts of the configuration file, modify the following:
 
   
  
  1. port 6379
  2. pidfile /var/run/redis_6379.pid
  3. # slaveof <masterip> <masterport>
  4. logfile "/data/logs/redis.master.log"
  5. daemonize yes
    • To modify the slave1 configuration file:
 
   
  
  1. < Span class= "PLN" >port 6380
  2. < Span class= "PLN" >pidfile / var / run / redis_6380 pid
  3. slaveof 127.0 0.1 6379
  4. < Span class= "PLN" >logfile "/data/logs/redis.slave1.log"
  5. daemonize Yes

 
   
  
  1. < Span class= "PLN" >port 6381
  2. < Span class= "PLN" >pidfile / var / run / redis_6381 pid
  3. slaveof 127.0 0.1 6379
  4. < Span class= "PLN" >logfile "/data/logs/redis.slave-6381.log"
  5. daemonize Yes

Back to TopThird, open master and slave1, Slave2
    • Turn on master (Salve is turned on like this)
Go to directory: redis-3.2.1.master (slave then go to the appropriate directory), execute:
 
   
  
  1. ./src/redis-server redis.conf
After successful operation, view logfile can see the following interface:
    • Open slave1
You can see the slave1 turned on, such as:also see the log for master as follows:

    • Open Slave2
You can see the slave2 turned on, such as:
Also see the log for master as follows:
    • View the running situation


Back to TopIv. Client Connection-Test synchronization
    • Connect the master client and create the data
 
   
  
  1. redis-cli -h 127.0.0.1 -p 6379

    • Connect the slave and view the data synchronization situation:

Here's another slave:
Slave can not write, only can read when the slave of Redis, can only read data, can not write data:

Back to Topv. Application Scenarios
    • Operations that take the latest n data
For example, the most recent article to get your site, through the following way, we can put the latest 5,000 reviews of the ID in the Redis list collection, and will be out of the collection section from the database to obtain the use of the Lpush latest.comments<id> command, Insert data into the list collection after inserting it, then use the LTrim latest.comments 0 5000 command to keep the last 5,000 IDs forever and then we can use the following logic when we get a page comment on the client (pseudo code)
  
 
  1. FUNCTION get_latest_comments(start,num_items):
  2.    id_list = redis.lrange("latest.comments",start,start+num_items-1)
  3.    IF id_list.length < num_items
  4.    id_list = SQL_DB("SELECT ... ORDER BY time LIMIT ...")
  5.    END
  6. RETURN id_list
  7. END
If you have a different filter dimension, such as the newest n for a category, you can build a list that is categorized by this category, and Redis is very efficient if you save the ID .
    • Leaderboard application, Top n operation
This requirement differs from the above requirements in that the preceding operation takes the time as the weight, this is the weight of a certain condition, such as the number of times by the top, then we need our sorted set to go, set the value you want to sort into the score of sorted set, Set the specific data to the corresponding value, each time only need to execute a zadd command.
    • Applications that require precise setting of expiration time
For example, you can set the score value of the sorted set to the timestamp of the expiration time, then you can simply sort through the expiration time, and periodically purge out-of-date data, not only to clear the expired data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly.
    • Counter Application
Redis commands are atomic, and you can easily use the INCR,DECR command to build a counter system.
    • Uniq operation, get all data row weight values for a certain period of time
This is the most appropriate set data structure to use Redis, just to constantly throw it into set, set means set, so it automatically takes weight.
    • Pub/sub Building a real-time messaging system
Redis's pub/sub system can build real-time messaging systems, such as many examples of real-time chat systems built with Pub/sub.
    • Building a queue system
Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.
    • Cache


Back to TopVi. Reference Links"Configuring Redis High Availability" http://www.veritas.com/community/blogs/configuring-redis-high-availability " Redis Sentinel Deployment under Windows http://bbs.redis.cn/forum.php?mod=viewthread&tid=715




From for notes (Wiz)

Redis configuration Redis master-slave replication

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.