Redis Learning Lesson Eighth: Redis Advanced Utility features (ii)

Source: Internet
Author: User
Tags redis server

Redis Advanced Utility Features

4. Persistence mechanism

Redis is an in-memory database that supports persistence, which means that Redis often needs to synchronize the in-memory data to the hard disk to ensure persistence.
Redis supports two persistence modes:
(1), snapshotting (snapshot) is also the default mode.
A snapshot is the default persistence method, which writes in- memory data to a binary file in the form of a snapshot, with the default file name Dump.rdb. You can automatically make snapshot persistence by configuring settings. We can configure Redis to take snapshots automatically if more than M key changes are made within n seconds.

To modify the configuration file redis.conf:
Save 1 #900秒内如果超过1个key被修改, the snapshot save is initiated.


(2), append-only file (abbreviated AOF) way.

Because the snapshot is done at a certain interval, if Redis accidentally falls down, all modifications after the last snapshot are lost.  AOF is more persistent than snapshot, because Redis appends each received write command to a file by using the Write function, and when Redis restarts, it rebuilds the contents of the entire database in memory by re-executing the Write command saved in the file. Of course, because the OS caches write modifications in the kernel, it may not be written to disk immediately, so it is still possible to lose some of the changes in aof mode. You can tell Redis through the configuration file that we want to force the OS to write to disk through the Fsync function.

appendonly Yes //enable AOF persistence mode

#appendfsync always//Receive write commands to write to disk immediately, the slowest, but guaranteed full persistence

Appendfsync everysec//write disk once per second, a good compromise in performance and persistence

#appendfsync no//full OS dependent, best performance, no guarantee for persistence

Modify the configuration file redis.conf, and then write to the Redis client, you can see that Redis is located in the bin directory, there are two files,appendonly.aof the Operation command,Dump.rdb Binary data is stored.

[email protected]:/usr/local/redis/bin# ll
Total 12720
drwxr-xr-x 2 root root 4096 2015-06-18 01:25.
drwxr-xr-x 4 root root 4096 2015-06-18 01:21.. /
-rw-r--r--1 root root 2015-06-18 01:26 appendonly.aof
-rw-r--r--1 root root 2015-06-15 02:25 dump.rdb
-rwxr-xr-x 1 Anny anny 566 2015-06-04 02:35 mkreleasehdr.sh*
-rwxr-xr-x 1 anny anny 3759902 2015-06-14 20:09 redis-benchmark*
-rwxr-xr-x 1 anny anny 20295 2015-06-14 20:09 redis-check-aof*
-rwxr-xr-x 1 anny anny 40868 2015-06-14 20:09 redis-check-dump*
-rwxr-xr-x 1 anny anny 3853871 2015-06-14 20:09 redis-cli*
-rwxr-xr-x 1 anny anny 5325229 2015-06-14 20:09 redis-server*


5. Publish a subscription message

A publish Subscription (PUB/SUB) is a message communication pattern that is primarily designed to decouple the coupling between a message publisher and a message subscriber, and Redis acts as a pub/sub server, with the ability to route messages between subscribers and publishers. Subscribers can subscribe to Redis server for the type of message they are interested in by using the Subscribe and Psubscribe commands , andRedis is calling the information type Channel . When a publisher sends a specific type of information to Redis server through the Publish command , all clients subscribing to that type of information receive this message.

A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:

When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:

Publish/Subscribe experiments in Redis:

Open three terminals,

Enter the following command in the first terminal to subscribe to the TV1 and TV2 two channels via subscribe :

[Email protected]:/usr/local/redis/bin$./redis-cli-a Anny
127.0.0.1:6379> Subscribe TV1 TV2
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "TV1"
3) (integer) 1
1) "Subscribe"
2) "TV2"
3) (integer) 2

Enter the following command in the second terminal and subscribe to the TV1 channel via subscribe :

[Email protected]:/usr/local/redis/bin$./redis-cli-a Anny
127.0.0.1:6379> Subscribe TV1
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "TV1"
3) (integer) 1

In the third terminal, enter the following command by publishing the message:

[Email protected]:/usr/local/redis/bin#./redis-cli-a Anny
127.0.0.1:6379> publish TV1 test
(integer) 2
127.0.0.1:6379> publish TV2 ABC
(integer) 1

You can see that the first terminal and the second terminal receive a message from the third terminal at the same time:

The first terminal receives a message as follows:

1) "Message"
2) "TV1"
3) "Test"
1) "Message"
2) "TV2"
3) "ABC"

The second terminal receives a message as follows:

1) "Message"
2) "TV1"
3) "Test"

The following table lists the common commands for Redis publish subscriptions:

Serial Number Command and Description
1 Psubscribe pattern [pattern ...] subscribes to one or more channels that match a given pattern.
2 PUBSUB subcommand [argument [argument ...]] View subscription and release system status.
3 The PUBLISH channel message sends information to the specified channel.
4 Punsubscribe [pattern [pattern ...]] unsubscribe from all channels of a given pattern.
5 SUBSCRIBE Channel [Channel ...] subscribe to the information given for one or more channels.
6 unsubscribe [Channel [channel ...]] means to unsubscribe from a given channel.


6, the use of virtual memory

Redis's virtual memory is not the same as the operating system's virtual memory, but the idea and purpose are the same. is to temporarily swap infrequently accessed data from memory to disk, freeing up valuable memory for other data that needs to be accessed. Especially for memory databases such as Redis, memory is never enough. In addition to splitting data into multiple Redis servers, the ability to increase database capacity is to use virtual memory to exchange infrequently accessed data to disk.

The following is about VM-related configuration items (redis.conf):

vm-enabled Yes #开启vm功能

Vm-swap-file/tmp/redis.swap #交换出来的value保存的文件路径

Vm-max-memory 1000000 #redis使用的最大内存上限

Vm-page-size #每个页面的大小32字节

Vm-pages 134217728 #最多使用多少页面

Vm-max-threads 4 #用于执行value对象换入缓存的工作线程数量

After modifying the configuration file redis.conf, kill the redis-server process and restart the redis-server.
You are prompted to add a configuration entry in the redis.conf file REALLY-USE-VM Yes

Note: I am currently experimenting with the Redis 3.0.2 version does not have a configuration item on virtual memory, after the manual join, start Redis-server will be error.

School Redis Basic Tutorial: http://www.w3cschool.cc/redis/redis-backup.html

Redis Learning Lesson Eighth: Redis Advanced Utility features (ii)

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.