The actual application scenario of REDIS in e-commerce (RPM)

Source: Internet
Author: User

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> ZaddUser100000:follow61307510400000"100001"Score to timestamp (integer)1redis> ZaddUser100000:follow61307510402300"100002" (integer)1redis> ZaddUser100000:follow61307510405600"100003" (integer)1redis> ZaddUser200000:follow61307510400000"100001" (integer)1redis> ZaddUser200000:follow61307510402300"100005" (integer)1redis> ZaddUser200000:follow 61307510405600   "100004" (integer)  1redis> zinterstore < Span class= "hljs-string" >out:100000:200000 1 user:100000:follow user:200000:follow //intersection command for common attention ( 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> ZaddUser100000:Feed:topic61307510400000 <feedId>Score to timestamp (integer)1redis> EXPIREUser100000:feed:topic 24*60*60 //set timeout to one day (integer)  < Span class= "Hljs-number" >1redis> zadd user: 100000:feed:friend 61307510400000 <feedid>  //different types of feeds (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> HsetUser:<userid>:Message:ur system11 Unread system messages (integer)1redis> HincrbyUser:<userid>:Message:ur system1Unread system Message +1 (integer)2redis> HincrbyUser:<userid>:message:ur comment 1  //unread Comment message +1 (integer)  1redis> hset user:<userid>:message:ur system  0//set to System message read (integer)  user:<userid>:message: Ur //get this key hashkey  and 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.

The actual application scenario of REDIS in e-commerce (RPM)

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.