1: Cache - hot data
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 available in all similar tools. Compared with memcached, it also provides rich data types that can be used. In addition, the data in the memory is also provided. You can choose persistence mechanisms such as AOF and RDB, whether they are cold, hot or hot and cold.
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
Of course you are done, 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.
Think about Tencent’s 1 billion users. You need to check whether a user is online within a few milliseconds. What can you do? Don’t say create a key for each user and then record them one by one (you can calculate the memory required, which will be terrifying, and there are many similar needs, how much Tencent will spend just for this...) Okay. Here you need to use in-place operations - use setbit, getbit, bitcount commands.
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"
6: Latest List
For example, the latest news list on the news list page. If the total number is large, try not to use select a from A limit. 10. For this kind of low-quality goods, 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.
The above is the entire introduction to redis usage scenarios.
Related references:Redis Tutorial
The above is the detailed content of What are the usage scenarios of redis?. For more information, please follow other related articles on the PHP Chinese website!

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
