Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the characteristics and application scenarios of Redis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the characteristics and application scenarios of Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Characteristics of Redis:

Redis and other key-value cache products have the following three features:

Redis supports data persistence. You can save the data in memory on disk and load it again when you restart it.

Redis not only supports simple key-value type data, but also provides storage of data structures such as list,set,zset,hash.

Redis supports data backup, that is, data backup in master-slave mode.

Advantages of Redis:

Extremely high performance-Redis can read at a speed of 110000 times per second and write at a speed of 81000 times per second.

Rich data types-Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.

Atom-all operations of Redis are atomic, and Redis also supports atomicity after several operations have been merged.

Rich features-Redis also supports publish/subscribe, notification, key expiration, and so on.

How is Redis different from other key-value stores?

Redis has more complex data structures and provides atomic operations on them, which is an evolutionary path different from other databases. The data types of Redis are based on basic data structures while being transparent to programmers without additional abstraction.

Redis runs in memory but can be persisted to disk, so memory needs to be weighed when reading and writing high-speed data sets, because the amount of data cannot be larger than hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, it is very easy to operate in memory, so Redis can do a lot of things with strong internal complexity. At the same time, they are compact and appended in terms of disk format, because they do not need random access.

Redis application scenario 1. Show the latest list of projects

The following statement is often used to show the latest projects, and as there is more data, the query will no doubt get slower and slower.

SELECT * FROM foo WHERE... ORDER BY time DESC LIMIT 10

In Web applications, queries such as "list the latest responses" are common, which usually leads to scalability problems. This is frustrating because projects are created in this order, but sort operations have to be performed to output this order.

Similar problems can be solved with Redis. For example, one of our Web apps wants to list the latest 20 comments posted by users. We have a "Show all" link next to the latest comments, and you can get more comments after clicking on it.

Let's assume that each comment in the database has a unique incremental ID field. We can use paging to make home and comment pages, using Redis templates:

Each time a new comment is posted, we add its ID to a Redis list:

LPUSH latest.comments

We cut the list to a specified length, so Redis only needs to save the latest 5000 comments:

LTRIM latest.comments 0 5000

Every time we need to get the project scope of the latest comment, we call a function to do it (using pseudo code):

FUNCTION get_latest_comments (start,num_items): id_list = redis.lrange ("latest.comments", start,start+num_items-1) IF id_list.length < num_items id_list = SQL_DB ("SELECT... ORDER BY time LIMIT...") END RETURN id_list END

What we do here is very simple. Our latest ID in Redis uses the resident cache, which is always updated. But we have set a limit of no more than 5000 ID, so our get ID function keeps asking Redis. You need to access the database only if the start/count parameter is out of this range.

Our system does not "flush" the cache in the traditional way, and the information in the Redis instance is always consistent. The SQL database (or other type of database on the hard drive) is triggered only when the user needs to obtain "very remote" data, while the home page or the first comment page will not bother the database on the hard disk.

two。 Delete and filter

We can use LREM to delete comments. If there are very few deletions, another option is to skip the entry for the comment entry and report that the comment no longer exists.

Sometimes you want to attach different filters to different lists. If the number of filters is limited, you can simply use a different Redis list for each different filter. After all, there are only 5000 items in each list, but Redis can handle millions of items with very little memory.

3. Relevant to the rankings

Another common requirement is that data from various databases is not stored in memory, so the performance of databases is not ideal in terms of sorting by score and real-time updates, which need to be updated almost every second.

Typically, for example, the rankings of online games, such as a Facebook game, you usually want to:

List the top 100 players with high scores

List a user's current global ranking

These actions are a piece of cake for Redis, and even if you have millions of users, you will get millions of new scores every minute.

The pattern is that every time we get a new score, we use this code:

ZADD leaderboard

You may replace username with userID, depending on how you design it.

Getting the top 100 high-scoring users is easy:

ZREVRANGE leaderboard 0 99

The global ranking of users is also similar, only need to:

ZRANK leaderboard4. Sort by user vote and time

A common variant of a ranking pattern, as used by Reddit or Hacker News, where news is ranked by score according to a formula similar to the following: score = points / time ^ alpha

Therefore, the user's vote will dig up the news accordingly, but the time will bury the news according to a certain index. Here is our model, and of course the algorithm is up to you.

The pattern is that we start by looking at items that may be up-to-date, such as the 1000 news items on the front page are candidates, so we ignore the others first, which is easy to implement.

Each time a new news post is posted, we add ID to the list and use LPUSH + LTRIM to make sure that only the latest 1000 items are taken out.

There is a background task to get this list and continuously calculate the final score for each of the 1000 pieces of news. The ZADD command populates the generated list in the new order, and the old news is cleared. The key idea here is that sorting is done by background tasks.

5. Overdue project processing

Another common way to sort items is to sort by time. We can use unix time as the score.

The pattern is as follows:

Every time a new item is added to our non-Redis database, we add it to the sort collection. In this case, we are using the time properties, current_time and time_to_live.

Another background task uses ZRANGE... The SCORES query sorts the collection and pulls out the latest 10 items. If the unix time is found to have expired, delete the entry in the database.

6. Count

Redis is a good counter, thanks to INCRBY and other similar commands.

I believe many times you want to add new counters to the database to get statistics or display new information, but in the end you have to give them up because of write sensitivity.

Well, now you don't have to worry about using Redis. With atomic increment (atomic increment), you can safely add various counts, reset them with GETSET, or let them expire.

For example, do this:

INCR user: EXPIRE user: 60

You can calculate the number of page views that users have recently paused between pages for no more than 60 seconds, and when the count reaches, for example, 20:00, you can display some banner prompts, or whatever you want to show.

7. Specific projects within a specific period of time

Another thing that is difficult for other databases, but what Redis does easily is to count how many specific users have accessed a particular resource during a specific period of time. For example, I want to know certain registered users or IP addresses, and how many of them have accessed an article.

Every time I get a new page view, I just need to do this:

SADD page:day1:

Of course, you may want to replace day1 with unix time, such as time ()-(time ()% 3600024) and so on.

Want to know the number of specific users? You just need to use the

SCARD page:day1:

Need to test whether a particular user has visited this page?

8. Real-time analysis of what is happening, for data statistics and prevention of spam, etc.

We have only done a few examples, but if you study the command set of Redis and combine it, you can get a large number of real-time analysis methods, which are effective and labor-saving. Using Redis primitive commands, it is easier to implement a spam filtering system or other real-time tracking system.

9. Pub/Sub

Redis's Pub/Sub is very simple, stable and fast. Support pattern matching, can subscribe and cancel channels in real time.

10. Queue

You should have noticed that Redis commands like list push and list pop can easily perform queue operations, but they can do more than that: for example, Redis also has a variant of list pop that blocks queues when the list is empty.

11. Caching

The cache section of Redis is worth writing a new article, and I'm just going to say it briefly here. Redis can replace memcached, changing your cache from just storing data to being able to update data, so you no longer need to regenerate data every time.

This is the end of the content of "what are the characteristics and application scenarios of Redis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report