search
HomeDatabaseRedisWhere is redis generally used?

Where is redis generally used?

Jun 05, 2019 pm 05:36 PM
redis

Summary of redis application scenarios. We usually use redis in many places. Here is a summary of the application scenarios we understand:

Where is redis generally used?

1. Cache of hotspot data

Because redis has fast access speed and supports rich data types, redis is very suitable for storing hotspot data. In addition, combined with expire, we can set the expiration time and then proceed. Cache update operation, this function is the most common and is used in almost all our projects.

2. Application of limited-time business (Recommended learning: Redis video tutorial)

You can use the expire command to set the survival time of a key in redis , redis will delete it after the time is up. This feature can be used in business scenarios such as limited-time promotional information and mobile phone verification codes.

3. Issues related to counters

Since the incrby command can achieve atomic increment, redis can be used for high-concurrency flash sales activities and distributed sequence number generation. , Specific services are also reflected in, for example, limiting the number of text messages sent by a mobile phone number, limiting the number of requests per minute for an interface, limiting the number of calls per day for an interface, etc.

4. Issues related to rankings

The query speed of relational databases in rankings is generally slow, so you can use the SortedSet of redis to sort hot data.

In the milk tea activity, we need to display the like ranking list of each department, so I made a SortedSet for each department, and then used the user's openid as the username above, and the user's number of likes as For the score above, a hash is made for each user. Through zrangebyscore, the ranking can be obtained according to the number of likes, and then the user's hash information is obtained according to the username. The performance experience of this was quite good in actual application at that time.

5. Distributed lock

This is mainly performed using the setnx command of redis. setnx: "set if not exists" means that if it does not exist, the cache will be successfully set and returned at the same time. 1, otherwise it returns 0. This feature is used in the background of Yunibenyuanfang. Because our server is clustered, the scheduled task may run on both machines, so in the scheduled task, first set a lock through setnx. If the setting is successful, it will be executed. If it is not set successfully, it means that the scheduled task has been executed. Of course, based on the specific business, we can add an expiration time to this lock. For example, if a scheduled task is executed every 30 minutes, then the expiration time can be set to a time less than 30 minutes. This is related to the period of the scheduled task and the execution of the scheduled task. Time consumption related.

Of course we can apply this feature to other scenarios that require distributed locks. Combined with the expiration time, it is mainly to prevent the occurrence of deadlock.

6. Delay operation

I have done relevant tests on this so far, but it has not been applied to our actual projects. Here I will give an application of this feature. Scenes. For example, after the order is produced, we occupy the inventory. After 10 minutes, we will check whether the user has actually purchased it. If there is no purchase, the document will be invalid and the inventory will be restored at the same time. Since redis has provided the Keyspace Notifications function since version 2.8.0, customers are allowed to subscribe to Pub/Sub channels in order to receive events that affect the Redis data set in some way. So we can use the following solution for the above needs. When we order production, we set a key and set it to expire after 10 minutes. We implement a listener in the background to monitor the effectiveness of the key. When the key expires, we will follow up. Logic added. Of course, we can also use the delay queue service of message middleware such as rabbitmq and activemq to achieve this requirement.

7. Paging, fuzzy search

The set collection of redis provides a zrangebylex method, the syntax is as follows:

ZRANGEBYLEX key min max [LIMIT offset count]

Page data query can be performed through ZRANGEBYLEX zset - LIMIT 0 10, where - means to obtain all data

zrangebylex key min max This can return the data in the dictionary interval, use this feature Fuzzy query function can be performed. This is also the only feature I have found in redis that supports fuzzy query on stored content.

A few days ago, I used this feature to conduct a simulation test on school data. The school data is about 600,000, and the response time is about 700ms, which is slightly faster than mysql's like query, but because it can avoid a large number of Database IO operation, so overall it is more conducive to system performance guarantee than direct mysql query.

8. Storage of mutual relationships such as likes and friends

The external functions provided by Redis set are similar to those of list. The special thing is that set can automatically eliminate duplicates. When you need to store a list of data and do not want duplicate data to appear, set is a very useful tool. A good choice, and set provides an important interface for determining whether a member is in a set collection, which list cannot provide. Or in the Weibo application, the people that each user follows exist in a collection, so it is easy to implement the function of finding common friends of two people.

This is used in the milk tea activity, which uses set to store the like relationship between users. In addition, the sismember method is used to determine whether the like has been received before the like. At that time, the response time of this interface was controlled. Within 10 milliseconds, very efficient.

9. Queue

Since redis has commands such as list push and list pop, it can easily perform queue operations.

For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!

The above is the detailed content of Where is redis generally used?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Redis: Exploring Its Data Model and StructureRedis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AM

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: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

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.

Why Use Redis? Benefits and AdvantagesWhy Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AM

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,

Understanding NoSQL: Key Features of RedisUnderstanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AM

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.

Redis: Identifying Its Primary FunctionRedis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AM

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: A Guide to Popular Data StructuresRedis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AM

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.

How to implement redis counterHow to implement redis counterApr 10, 2025 pm 10:21 PM

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.

How to use the redis command lineHow to use the redis command lineApr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),