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 improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

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 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment