Advanced applications for Redis-transaction processing, persistence, publish and subscribe messages, virtual memory usage

Source: Internet
Author: User
Tags configuration settings

Third, transaction processing

The transaction processing of Redis is relatively straightforward. Only the commands in the client-initiated transaction can be guaranteed to execute consecutively, and no other client commands are inserted, and when a client issues the multi command in the connection, the connection enters the context of a transaction, and the subsequent command of the connection is not executed but is stored in a queue. When the EXEC command is executed, Redis executes all the commands in the queue sequentially. If the execution occurs incorrectly, the correct execution is not rolled back, and the second differs from the relational database transaction.

The value of age has changed since the rollback.

Cancels a transaction with discard, cancels the transaction queue, and the data is all rolled back.

Redis optimistic Lock: Using Watch to monitor the specified key, the entire transaction will fail when exec has changed from the time the watch is called. You can call watch to monitor multiple keys more than once. Watch and transaction are valid for the entire connection, if connection monitoring is disconnected and transactions are cleared (Exec,discard,unwatch is no exception).


Iv. persistence mechanism

Redis is an in-memory database that supports persistence, and Redis needs to frequently synchronize the in-memory data to disk to ensure persistence.

Redis supports two persistence modes:

1, snapshotting (snapshot), the data stored in the file, the default way.

is to write a snapshot of the in-memory data into a binary file, the default file Dump.rdb, which can be automatically persisted by configuration settings. Configurable Redis in n seconds if more than M

A key is modified to automatically save the snapshot.


Save 1 #900秒内如果超过1个key被修改, initiates a snapshot save

Save #300秒内如果超过10个key被修改, this snapshot is saved

Save 10000 #60秒内如果超过10000个数据被修改, initiate snapshot save


2, Append-only file (abbreviated as AOF), the read and write operations are stored in the file.

Because the snapshot is done at a certain interval, if Redis is accidentally down, all modifications after the last snapshot are lost.

AOF is more persistent than snapshot mode, because when using aof, Redis writes each received write command through the Write function to the file when Redis Qidong is saved by re-executing the file

Write command to re-establish the contents of the entire database in memory.

Because the OS caches write modifications in the kernel, it may not be written to disk immediately, so the persistence of aof mode may also result in the loss of some data.

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 #启用aof持久化方式

#appendfsync always//Receive write commands to write to disk immediately, the slowest, but ensure the complete persistence of the data

Appendfsync everysec//writes to disk once per second, making a good compromise in performance and persistence

#appendfsync no//full reliance on OS, best performance, no guarantee of persistence


After opening, in the Bin folder will be more than two storage files, as follows:


V. Publication and subscription information

A publish Subscription (PUB/SUB) is a message communication pattern that is primarily designed to decouple the coupling between a message publisher and a subscriber, and Redis acts as a pub/sub server, with the ability to route messages between subscribers and publishers. A subscriber subscribes to a message type of interest to redis through the subscribe and Psubscribe commands, and Redis makes the information type channel. When a publisher sends a specific type of information to Redis server through the close Publish command, all clients subscribing to that message type receive this message.

For example, open three clients, one publishing message, and two other subscription messages, as follows


Vi. use of virtual memory

Redis's virtual memory is temporary. Infrequently accessed data is exchanged from memory to disk, freeing up memory space for other access data, especially for memory databases such as Redis, where memory is not always sufficient. In addition to separating multiple Redis servers, the way to increase the capacity of a database is to use virtual memory to exchange infrequently accessed data to disk.

By configuring the VM-related redis.config configuration:

vm-enable Yes #开启vm功能

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

Vm-max-memory 10000000 #redis使用的最大内存上线

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

Vm-pages 123217729 #最多使用多小个页面

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




Advanced applications for Redis-transaction processing, persistence, publish and subscribe messages, virtual memory usage

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.