Redis Application Scenarios

Source: Internet
Author: User
Tags live chat

In-memory database Redis-primarily to address the acceleration of hotspot data blocks.

Without a doubt,Redis pioneered a new way of storing data, using Redis, instead of focusing on how to put an elephant into a refrigerator when faced with a monotonous database, we use Redis's flexible data structure and data manipulation. Build different refrigerators for different elephants. I hope you like this metaphor.

Here is a fresh article, the author of the Redis author @antirez, describing some of the applications that Redis is more suitable for , Nosqlfan simply listed here for you to list:

1. Take the latest N data operation

For example, the most recent article to get your site, by 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 part from the database

    • Use the Lpush latest.comments<id> command to Insert data into the list collection
    • Insert complete and then use the LTrim latest.comments 0 5000 command to keep the last 5,000 IDs forever
    • Then we can use the following logic (pseudo code) when we get a page comment on the client.
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_listend

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.

2.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.

3. 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.

4.Counter Application

Redis commands are atomic, and you can easily use the INCR,DECR command to build a counter system.

5.Uniq operation to get all data rows 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.

6. Real-time system,anti-spam system

With the Set function mentioned above, you can know whether an end user has performed an operation, can find a combination of its operations to analyze statistical comparisons, and so on. There is nothing to be done, only unexpected.

7.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.

8. BuildQueuing System

Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.

9.Cache

This needless to say, performance is better than memcached, data structure is more diverse.

The actual application of REDIS in e-commerce

It has been a long time using Redis, while a little time, combined with guang.com experience, summarizes the actual application of Redis in the social e-commerce website. Poor writing, you crossing, make a look at it.

1. Various counts, commodity dimension count and user dimension count

Speaking of e-commerce, must be inseparable from the goods, and the goods with a variety of counts (like number, number of comments, identification number, number of views, etc), Redis commands are atomic, you can easily use INCR,DECR and other commands to count.

    • Commodity dimension count (likes number, number of comments, number of appraisals, number of views, etc)

Type of Redis: Hash . If you are not familiar with Redis data types, you can refer to the Http://redis.io/topics/data-types-intro

Define a key for product product: , define HashKey for each value, such as the number of likesxihuan

redis> HSET product:1231233 xihuan 5(integer) 1redis> HINCRBY product:1231233 xihuan 1 //喜欢数+1(integer) 6 redis> HGETALL product:1231233 //获取这key hashkey 和value1) "xihuan"2) "6"
    • User dimension count (dynamic number, number of followers, number of fans, number of likes, number of posts, etc.)

The user dimension count is used with the commodity dimension Count Hash . Defines a key for the user user: , defines hashkey for each value, such as the number of followersfollow

redis> HSET user:100000 follow 5(integer) 1redis> HINCRBY user:100000 follow 1 //关注数+1(integer) 6 redis> HGETALL user:100000 //获取这key hashkey 和value1) "follow"2) "6"
2. Store Social relationships

For example, the user's friends/fans/concerns, can exist sorted set in a, score can be timestamp, so that the two people of the common friends of the operation, you may only need to use the intersection command.

redis> ZADD user:100000:follow 61307510400000 "100001" //score 为timestamp(integer) 1redis> ZADD user:100000:follow 61307510402300 "100002"(integer) 1redis> ZADD user:100000:follow 61307510405600 "100003"(integer) 1redis> ZADD user:200000:follow 61307510400000 "100001"(integer) 1redis> ZADD user:200000:follow 61307510402300 "100005"(integer) 1redis> ZADD user:200000:follow 61307510405600 "100004"(integer) 1redis> ZINTERSTORE out:100000:200000 1 user:100000:follow user:200000:follow //交集命令,获得共同关注(integer) 2redis> ZRANGE out:100000:200000 0 -11) "100001"
3. Use as cache instead of memcached (item list, comment list, @ hint list, etc)

Compared to memcached simple key-value storage, Redis's numerous data structures (list,set,sorted Set,hash, etc) make it easier to cache various business data, as well as memcached performance.

Note:rpush Pagewviews.user:EXPIRE pagewviews.user:60//Note to update timeout

4. Anti-spam system (comments, release items, forum posts, etc)

As an e-commerce site by a variety of spam attacks is less unavoidable (spam reviews, release junk goods, ads, brush their own product rankings, etc.), for these spam a series of anti-spam rules, some of which can be used to do real-time redis analysis, such as: 1-minute review not more than 2 times, 5 minute reviews less than 5 times (more mechanisms/rules need to be combined with drools). Take sorted set the last day of user action record (why not all records?). Save memory, all operations will be logged to log, the subsequent use of Hadoop for more comprehensive analysis of statistics), by ZRANGEBYSCORE user:200000:operation:comment 61307510405600 +inf Obtaining 1 minutes of Operation Records, Redis> Zadd user:200000:operation:comment 61307510402300 "This is a comment"//score for timestamp (integer) 1 redis> zrangebyscore user:200000:operation:comment 61307510405600 +inf//Get the operation record in 1 minutes 1) "This is a comment"

BTW, a more complex real-time calculation can take storm.

5. User Timeline/feeds

There is a similar microblogging in the column I focus on, including the attention of people, themes, brand dynamics. Redis is used primarily as a cache on this side.

redis> ZADD user:100000:feed:topic  61307510400000 <feedId> //score 为timestamp(integer) 1redis> EXPIRE user:100000:feed:topic 24*60*60 //set timeout to one day(integer) 1redis> ZADD user:100000:feed:friend 61307510400000 <feedId> //不同类型feed(integer) 1redis> EXPIRE user:100000:feed:friend 24*60*60 //set timeout(integer) 1
6. Latest Listings & Leaderboards (products that users have just liked, etc)

Redis's List data structures or sorted set structures are used here to facilitate business scenarios such as the latest list or leaderboard.

7. Message Notification

In fact, this business scenario can also be counted on the count, is also used Hash . As follows:

redis> HSET user:<userId>:message:ur system 1//1条未读系统消息(integer) 1redis> HINCRBY user:<userId>:message:ur system 1 //未读系统消息+1(integer) 2redis> HINCRBY user:<userId>:message:ur comment 1 //未读评论消息+1(integer) 1redis> HSET user:<userId>:message:ur system 0//设为系统消息已读(integer) 1redis> HGETALL user:<userId>:message:ur //获取这key hashkey 和value1) "system"2) "0"3) "comment"4) "1"
8. Using Redis as a message queue

When the cluster environment, Java can ConcurrentLinkedQueue not meet our needs, at this time, the Redis list data structure to implement distributed Message Queuing.

Introduction to the usage scenarios of 5 data structures in Redis This article mainly introduces the usage scenarios of 5 kinds of data structures in Redis, this paper explains the 5 types of string, Hash, list, set and Sorted set in Redis, and the friends who need can refer to

I. REDIS data structure usage scenarios

Originally read Redisbook This book, the basic functions of redis are already familiar with, from last week began to see the source of Redis. The current goal is to thoroughly understand Redis's data structure. As we all know, there are 5 kinds of data structures in Redis, what are the usage scenarios for each data structure?

string--string
hash--Dictionary
list--List
set--Collection
Sorted set--ordered Collection

Let's take a brief look at their respective usage scenarios:

1. string--string

The string data structure is a simple key-value type, and value can be either a string or a number (encoding is an integral type when a number type is represented by a Long, and the rest is stored in SDSHDR as a string). With the Strings type, you can fully implement the current Memcached functionality and be more efficient. You can also enjoy the timing persistence of Redis (you can choose RDB mode or AOF mode), operation log and Replication functions. In addition to providing operations like get, set, INCR, DECR, and so on, Redis provides the following Memcached:

Copy CodeThe code is as follows:
1.LEN niushuai:o (1) Gets the string length
2.APPEND Niushuai Redis: APPEND content to string, and intelligently allocates memory (twice times per time)
3. Set and get a section of a string
4. Set and get one of the strings (bit)
5. Batch set the contents of a series of strings
6. Atomic counter
7.GETSET command, please set a new value while emptying the old value, with the atomic counter

2. hash--Dictionary

In Memcached, we often package structured information into HashMap, which is stored as a string value (usually in JSON format) after the client is serialized, such as the user's nickname, age, gender, and points. When you need to modify one of these items, you usually need to remove the string (JSON), then deserialize, modify the value of an item, and then serialize it back into a string (JSON). Simply modify a property to do so many things, the consumption must be very large, also does not apply to some possible concurrent operation of the occasion (such as two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one item property value just as you would Update a property in a database.

Copy CodeThe code is as follows:
Storing, reading, and modifying user properties

3. list--List

The list is plainly a list of links (Redis uses a doubly-linked list), and anyone who learns about data structure knowledge should be able to understand its structure. With the List structure, we can easily implement features such as the latest message ranking (such as the TimeLine of Sina Weibo). Another application of list is the message queue, which can take advantage of the *push operation of list, the task exists in list, and then the worker thread takes out the task with the POP operation to execute. Redis also provides an API to manipulate a section of the list, and you can directly query and delete elements of a section of the list.

Copy CodeThe code is as follows:
1. Weibo TimeLine
2. Message Queuing

4. set--Collection

A set is a set, and the concept of a collection is a combination of a bunch of distinct values. Using the set data structure provided by Redis, you can store some aggregated data. For example, in a microblog application, you can have a collection of all the followers of a user, and a collection of all their fans. Because Redis is very human for the collection to provide the intersection, set, difference sets and other operations, it can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection.

1. Common friends, two degree friends
2. Using uniqueness, you can count all the independent IPs that visit the website
3. When the friend recommends, according to the tag to seek the intersection, is larger than some threshold can recommend

5. Sorted set--ordered Collection

Compared with sets, Sorted sets is to add a set of elements of a weight parameter score, so that the elements in the set can be ordered by score, such as a storage of the class students Sorted sets, its collection value can be the student's school number, and SCO Re can be the test score, so that when the data is inserted into the collection, it has been sorted in a natural way. Also can use Sorted sets to do with the weight of the queue, such as the normal message score is 1, the important message of the score is 2, and then the worker can choose to press score reverse order to get work tasks. Let important tasks take precedence.

1. Elements with weights, such as a game's user score leaderboard
2. More complex data structures, generally used in the scene is not too much

Second, Redis other feature usage scenarios

1. Subscription-Release system

Pub/sub literally is the release (Publish) and Subscription (Subscribe), in Redis, you can set a key value for message publishing and message subscription, when a key value on a message published, all subscribed to its client will receive the corresponding message. The most obvious use of this function is to use it as a real-time messaging system, such as regular live chat, group chat, and other functions.

2. Transaction--transactions

Who says that NoSQL does not support transactions, although Redis's transactions provides not strictly ACID transactions (such as a string of commands executed with EXEC execution, in the execution of the server down, then there will be a part of the command execution, the rest is not executed), but this Transac tions also provides the basic command package execution function (in the case of the server does not have a problem, can ensure that a series of commands are executed together in sequence, there will be other client commands inserted to execute). Redis also provides a watch function, you can watch a key, and then execute transactions, in the process, if the value of this watched is modified, then this transactions will find and refuse to execute.

Redis Application Scenarios

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.