What redis that? What are the usage scenarios?

First, what is redis

First thing to say redis, should first talk about NoSQL,
NoSQL (Not Only SQL NoSQL =), which means "not only SQL",
refers to non-relational database. With the rise of the Internet web2.0 websites, traditional relational databases in dealing with web2.0 sites, particularly large scale and high concurrent SNS type of web2.0 pure dynamic site has appeared to be inadequate, exposed a lot of problems difficult to overcome, rather than relational databases due to its own characteristics has been very rapid development. NoSQL database is generated in order to solve large-scale data collection of multiple data types of challenges brought about, especially in big data application problems, including the ultra-large-scale data storage.
(Such as Google or Facebook every day to collect trillion bits of data for their users). These types of data storage does not require a fixed pattern, no extra operation can be extended laterally.

Redis: REmote DIctionary Server (remote dictionary server) are completely free open source, written in C, and comply with the BSD license, is a high-performance (key / value) distributed in-memory database, based on memory to run and support the persistence of NoSQL database, one of the most popular NoSql current database, also known as a data structure server.

Two, redis usage scenarios

1, hot data cache

Since redis block access speed, data types supported by relatively abundant, so it is suitable for storing hot redis data, combined with additional expire, we can set an expiration time and then cache update operation, this feature is most common, almost all of our projects some use.

2, limit the use of business

redis expire command can be used to set the lifetime of a key, the time after redis will delete it. Using this feature can be used in limited-time promotions information, phone verification code and other business scenarios.

3, counter-related issues

Since the command can be realized redis incrby atomic incremental, can be applied to high concurrent spike activity, generating the distributed sequence number, but also in specific business phone number such as restrictions on how many a send SMS, a request to limit how much the interface one minute , an interface to call one day limit how many times and so on.

4, ranking issues

In terms of ranking relational database query speed Pianman general, it can make use of SortedSet redis sort of hot data.

In the tea activities, we need to show thumbs rankings of various departments, so I made a SortedSet for each department, and then the user's openid as above username, points to the number of users of praise as the top score, and then for each user to make a hash, you can get ranking points according to the number of praise by zrangebyscore, then username obtain information based on the user's hash, this time in the practical application of performance experience is also pretty good.

  5, distributed lock

The main use of setnx redis command, setnx: "set if not exists" is that if there is no success then set the cache and returns 1, otherwise it returns 0, this feature in your Ben Yu in the distant background has been used, because our server is a cluster, the timing of the task may all run on two machines, so the timing of tasks by setnx first set a lock, if successful set is executed, if not successfully set, it indicates that the scheduled task has been executed. Of course, combined with the specific business, we can add a lock to the expiration time, say 30 minutes to perform a task timing, then the expiration time is set to be less than a time of 30 minutes can be, this cycle with regular tasks and the timing of task execution the relevant time-consuming.

Of course, we can apply this feature will require a distributed lock another scene, the combined expiration time mainly to prevent deadlock.

6, delay in operation

I did this at present relevant tests, but has not yet applied to our actual project, I give the following scenarios that property. For example, when we take up the inventory-to-order, 10 minutes to test the user is really enough to buy, do not buy if the document set is invalid, while reducing inventory. Since redis since the 2.8.0 version provides Keyspace Notifications feature that allows customers to subscribe to Pub / Sub Channel, Redis data set in order to receive event influence in some way. So we For the above needs can use the following solutions, we are in the order, set a key, and set 10 minutes expired, we implement a listener in the background, monitoring key effectiveness, listening to the follow-up when the key failure plus logic. Of course, we can also use rabbitmq, activemq such as messaging middleware delay queue service to meet the requirement.

7, paging, fuzzy search

redis collection set in a zrangebylex method provides the following syntax:

ZRANGEBYLEX key min max [LIMIT offset count]

Can collate data inquiry + LIMIT 0 10, wherein - - represents ZRANGEBYLEX zset + by acquiring all the data

zrangebylex key min max that can return data dictionary intervals, use of this feature can be fuzzy query function, I found that this is currently the only one in redis support features for store content fuzzy queries.

A few days ago I passed this feature, the school was simulated test data, about 600,000 school data, response time is around 700ms, than like mysql query slightly faster, but because it can avoid a lot of io database operations, so the overall or query performance guarantee system more conducive than direct mysql.

8 storage relationship, thumbs up, friends, etc.

Redis set of external functions provided with the list function is similar to a list of special is that is can be set to automatically de-duplication, when you need to store a list of data, do not want to duplicate data set is a good choice and set provides important to determine whether a member interfaces within a set collection, this list is not offered. Or in the micro-blog application, everyone concerned about the presence of a user in the collection, it is easy to find two people realize mutual friend function.

This activity has used in tea, is the use of the correlation between the thumbs of the user set storage, prior to further thumbs determines whether a point on the use of flattery sismember method, when the response time of the interface control within 10 milliseconds, very efficient.

9, queue

Since such a redis command list push and list pop, it is possible to easily perform the operation queue.  

Guess you like

Origin www.cnblogs.com/jxxblogs/p/12234723.html