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
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar zxvf redis-3.2.1.tar.gz
mv redis-3.2.1 redis-3.2.1.master
tar zxvf redis-3.2.1.tar.gz
mv redis-3.2.1 redis-3.2.1.slave-1
tar zxvf redis-3.2.1.tar.gz
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:
cd redis-3.2.1.master/src
make
make test
//其他两个目录执行相同的操作
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:
- Port
- LogFile
- slaveof;
- Pidfile;
- 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:
port 6379
pidfile /var/run/redis_6379.pid
# slaveof <masterip> <masterport>
logfile "/data/logs/redis.master.log"
daemonize yes
- To modify the slave1 configuration file:
< Span class= "PLN" >port 6380
< Span class= "PLN" >pidfile / var / run / redis_6380 pid
slaveof 127.0 0.1 6379
< Span class= "PLN" >logfile "/data/logs/redis.slave1.log"
daemonize Yes
< Span class= "PLN" >port 6381
< Span class= "PLN" >pidfile / var / run / redis_6381 pid
slaveof 127.0 0.1 6379
< Span class= "PLN" >logfile "/data/logs/redis.slave-6381.log"
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:
./src/redis-server redis.conf
After successful operation, view logfile can see the following interface:
You can see the slave1 turned on, such as:also see the log for master as follows:
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
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)
FUNCTION get_latest_comments(start,num_items):
id_list = redis.lrange("latest.comments",start,start+num_items-1)
IF id_list.length < num_items
id_list = SQL_DB("SELECT ... ORDER BY time LIMIT ...")
END
RETURN id_list
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.
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.
Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.
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