Redis is an open source log-type Key-Value database written in ANSI C language, supports network, can be memory-based and persistent, and provides APIs in multiple languages.
Redis has many application scenarios. This is a simple list of 7 application scenarios:
1: Caching—— Hot data
For hot data (data that is often queried but not often modified or deleted), the first choice is to use redis cache. After all, the powerful bubbling QPS and extremely strong stability are not All similar tools are available, and compared to memcached, it also provides a wealth of data types that can be used. In addition, the data in the memory also provides persistence mechanisms such as AOF and RDB to choose from, whether it is cold, hot or cold and sometimes. Hot ones are optional.
It should be noted based on specific applications: Many people use spring's AOP to build automatic production and clearing of redis cache. The process may be as follows:
Select Query redis before the database, and use redis if necessary Data, give up the select database, if not, select the database, and then insert the data into redis
update or delete database, query redis to see if the data exists, if it exists, delete the data in redis first, and then update or delete the database The data in
The above operation is basically no problem if the amount of concurrency is very small, but in the case of high concurrency, please pay attention to the following scenario:
Delete the redis in order to update At this time, another thread executed a query and found that it was not in redis. It immediately executed the query SQL and inserted a piece of data into redis. Returning to the update statement just now, this miserable thread did not know the damn select just now. Thread made a huge mistake! So the erroneous data in redis will exist forever until the next update or delete.
2: Counter
Applications such as counting clicks. Due to the single thread, concurrency issues can be avoided, error-free guaranteed, and 100% millisecond performance! Cool.
Command: INCRBY
Don’t forget persistence, after all, redis only stores memory!
3: Queue
is equivalent to the messaging system, ActiveMQ, RocketMQ and other tools are similar, but I personally think it is okay to use it simply, if the data consistency requirements are high. Or use professional systems such as RocketMQ.
Since redis adds data to the queue by returning the number of the added element in the queue, it can be used to determine the number of users accessing this business.
The queue can not only change concurrent requests into into serial, and can also be used as queue or stack
Four: Bit operation (big data processing)
Used in scenarios with hundreds of millions of data, for example System check-ins for hundreds of millions of users, statistics on the number of duplicate logins, whether a user is online, etc.
The principle is:
Construct a long enough array in redis. Each array element can only have two values 0 and 1, and then the subscript index of this array is used to represent what we have above The user ID in the example (must be a number), then it is obvious that this large array with a length of hundreds of millions can build a memory system through subscripts and element values (0 and 1). The scenarios I mentioned above are also It can be achieved. The commands used are: setbit, getbit, bitcount
5: Distributed lock and single-thread mechanism
Verify repeated requests from the front end (similar situations can be freely expanded) , can be filtered through redis: each request uses the request IP, parameters, interfaces and other hashes as keys to store in redis (idempotent request), set the validity period, and then search in redis for the next request. This key is used to verify whether it is a repeated submission within a certain period of time
The flash kill system is based on the single-threaded feature of redis to prevent database "explosion"
Global incremental ID generation , similar to "flash sale"
Six: Latest list
For example, the latest news list on the news list page, if the total number is large , try not to use cheap stuff like select a from A limit 10, try the LPUSH command of redis to build a List, and just insert them one by one in order. But what if the memory is cleared? It’s also simple. If you can’t query the storage key, just use mysql to query and initialize a List into redis.
Seven: Ranking
Whoever scores higher will be ranked higher. Command: ZADD (with sequel, sorted set)
The above is the detailed content of What application scenarios is redis suitable for?. For more information, please follow other related articles on the PHP Chinese website!