Redis advanced feature 2

Source: Internet
Author: User

I. Optimistic lock complex transaction control

Optimistic lock: Based on the Data version. Add a version ID for the data. In the database table-based version solution, when you add a version field to the database table to read the data, read the version number together, and Add 1 to the version number when updating later. In this case, the submitted data version number is compared with the current version number of the record corresponding to the database table. If the submitted data version number is greater than the current version number, it is updated. Otherwise, the data is deemed to have expired.

How to Implement optimistic lock in redis: The watch command monitors a given key. When exec, if the monitored key has changed since the watch call, the entire transaction will fail. The key of watch is valid for the entire connection, and the transaction is the same. If the connection is disconnected, monitoring and transactions are cleared automatically. The exec, discard, and unwatch commands clear all monitoring information in the connection.

For example:

Open two session sessions of redis:

Session1 is as follows:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H1F19-0.png "alt =" 223618267.png"/>

Session2 is as follows:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H1F34-1.png "alt =" 223713853.png"/>

Then, when session1 executes exec, it will fail.

Ii. Persistence Mechanism

Redis supports two persistence methods:

1. snapshow.snapshot) Default Mode

Write Data in the memory as a snapshot to the binary file. The default name is dump. rdb.

The configuration is as follows:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H11G9-2.png "alt =" 224930582.png"/>

2. Append-onlyfile (aof) Method

Aof is better than snapshot persistence because when aof is used, redis will append each write command to a file, when redis is restarted, the entire database will be rebuilt in the memory through this file.

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H1EF-3.png "alt =" 225844117.png"/>

3. Publish and subscribe to messages

Publishing and subscription pub/sub) is a message communication mode. It is designed to remove the coupling between message publishers and message subscribers. redis is a pub/sub server, message routing is enabled between the subscription and publisher.

You can subscribe to the redisserver by using the subscribe and psubscribe commands. When the publisher uses publish

When the Command sends a specific type of information to the redisserver, clients that subscribe to this type of information will receive messages.

Example: Open redis3 sessions

Session1 and session2 are used to subscribe to messages, and session3 is used to publish messages, for example:

Session1:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H13359-4.png "alt =" 231109944.png"/>

Session2:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H1BV-5.png "alt =" 231151683.png"/>

Session3:

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H15257-6.png "alt =" 231234562.png"/>

Iv. Use of virtual memory

Swap infrequently used data from the memory to the disk to release the memory for the data to be accessed.

650) this. width = 650; "title =" QQ20130922223126.png "src =" http://www.bkjia.com/uploads/allimg/131229/205H11231-7.png "alt =" 232123981.png"/>

This article is from the "phper" blog, please be sure to keep this source http://janephp.blog.51cto.com/4439680/1300291

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.