Preliminary Exploration of redis

Source: Internet
Author: User
1 Data Type

As a key-value database, redis also provides ing between keys and values. However, in addition to common values or strings, The redis key value can also be one of the following forms:

L lists (list)

L Sets)

L sorted Sets)

L hashes (hash table)

 

The data type of a key value determines the operations supported by the key value. Redis supports advanced atomic operations such as list, set, or sorted set intersection, union, and query. If the key value type is a common number, redis provides auto-increment and other atomic operations.

2.3.2 Persistence

Generally, redis stores data in memory or is configured to use virtual memory. Data persistence can be achieved through two Methods: Using the method, the data in the memory is constantly written to the disk; or using a log method similar to MySQL to record the log of each update. The former has high performance, but may cause a certain degree of data loss; the latter is the opposite.

33.3 Master-slave Synchronization

Redis supports synchronizing data to multiple slave databases. This feature is very helpful for improving reading performance.

43.4 Performance

Compared to databases that depend on disks to record each update, the memory-based features undoubtedly bring excellent performance to redis. There are significant performance differences between read/write operations.

5.3.5 API Language

L c

L c ++

L c #

L Clojure

L Common Lisp

L Erlang

L Haskell

L Java

L Javascript

L Lua

L Objective-C

L Perl

L PHP

L Python

L Ruby

L Scala

L Go

L Tcl

6.3.6 applicable scenarios

There is no doubt that Redis has created a new way of data storage. Using Redis, we don't have to focus on how to put elephants in the refrigerator in the face of monotonous databases, instead, Redis uses flexible data structures and data operations to build different refrigerators for different elephants. Hope you like this metaphor.

 

The following are some applicable scenarios for Redis:

1. Operations for retrieving the latest N data

For example, if we use the following method to retrieve the latest article on your website, we can place the ID of the latest 5000 comments in the List set of apsaradb for Redis, and retrieve the part that exceeds the set from the database.

 

Use the LPUSH latest. comments <ID> command to insert data to the list set.

Use the ltrim latest. Comments 0 5000 command to save only the last 5000 IDs.

Then we can use the following logic when the client obtains a certain page of comment

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 different filtering dimensions, such as the newest n entries of a specific category, you can create another list based on this category and store only IDs. redis is very efficient.

 

2. Ranking application, top n operations

The difference between this requirement and the above requirement is that the previous operation takes the time as the weight, and this is based on a certain condition as the weight, such as sorting by the top number of times, at this time, our sorted set will be out of service. Set the value you want to sort to the score of sorted set and set the specific data to the corresponding value, you only need to execute a zadd command each time.

 

3. Applications requiring precise expiration time setting

For example, you can set the score value of the sorted set as the timestamp of the expiration time, so you can simply sort the expiration time and regularly clear the expired data, in addition to clearing expired data in redis, You can regard the expiration time in redis as an index of the data in the database, and use redis to find out which data needs to be deleted after expiration, then, the corresponding records are precisely deleted from the database.

 

4. Counter application

Redis commands are atomic. You can easily use the incr and decr commands to build a counter system.

 

5. Perform the Uniq operation to obtain the values of all data records in a certain period of time.

The Set Data Structure of redis is the most suitable. You only need to constantly throw the data to the set. Set indicates the set, so it will automatically remove the weight.

 

6. Real-Time System and Anti-Spam system

With the set function mentioned above, you can know whether an end user has performed an operation, find a set of operations, and perform analysis, statistics, and comparison. You can't do it.

 

7. Build a Real-Time Message System Using Pub/Sub

Redis pub/sub systems can be used to build real-time messaging systems, for example, many examples of real-time chat systems built using pub/Sub.

 

8. Build a queue system

You can use list to build a queue system, or use sorted set to build a queue system with priority.

 

9. Cache

Needless to say, the performance is better than Memcached, and the data structure is more diversified.

Transferred from:

Produced by Hongwan

Http://blog.csdn.net/cdhongwan/article/details/7667874

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.