What is Redis? Two sentences can be summed up:
1. is a fully open source free Key-value memory Database
2. It is generally considered a data structure server, mainly because of its rich data structure strings, map, list, sets, sorted sets
Redis Five data types : String,hash (hash type), list (linked list structure), set (unordered set), and Zset (sorted set-ordered set).
Redis Application Scenario:
1. Display the latest project list
The following statement is often used to show the latest items, and with more data, the query will undoubtedly become slower.
- SELECT * from foo WHERE ... ORDER by Time DESC LIMIT 10
In a web app, queries such as "list up-to-date replies" are common, which often leads to extensibility issues. This is frustrating, because the project was created in this order, but it had to be sorted in order to output it.
A similar problem can be solved with redis. For example, one of our web apps wants to list the latest 20 reviews posted by users. We have a "show all" link on the side of the latest comment, and you can get more comments when you click on it.
We assume that each comment in the database has a unique incrementing ID field.
We can use pagination to make page and comment pages, use the Redis template, and each time a new comment is published, we'll add its ID to a redis list:
- Lpush latest.comments <ID>
We crop the list to a specified length, so Redis only needs to save the latest 5,000 comments:
LTRIM latest.comments 0 5000
Each time we need to get the scope of the latest review project, we call a function to complete (using pseudocode):
- 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 a resident cache, which is always updated. But we've made a limit of no more than 5,000 IDs, so our Get ID function will always ask for Redis. Access to the database is required only if the Start/count parameter is out of range.
Our system does not "refresh" 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 disk) is only triggered when the user needs to get "very far" data, and the home page or the first comment page will not bother the database on the hard disk.
2. Delete and filter
We can use Lrem to delete comments. If the deletion is very small, the other option is to skip the entry of the comment directly and report that the comment no longer exists.
There are times when 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 of the different filters. After all, there are only 5,000 items per list, but Redis is able to use very little memory to handle millions of items.
3. Leaderboard Related
Another common requirement is that data from a variety of databases is not stored in memory, so the performance of the database is not as good as the ability to sort by points and update them in real-time, almost every second.
Typically, for example, the leaderboard for online games, such as a Facebook game, based on the score you usually want:
-List Top 100 high-score contestants
-List A user's current global rankings
These operations are a piece of cake for redis, and even if you have millions of users, there will be millions of new points per minute.
The pattern is this, each time we get a new score, we use this code:
Zadd Leaderboard <score> <username>
You may replace username with a userid, depending on how you designed it.
Getting the top 100 high-score users is simple: Zrevrange leaderboard 0 99.
The global ranking of users is similar, only need: Zrank leaderboard <username>.
4, according to the user vote and time sorting
A common variant pattern of the leaderboard is, like Reddit or hacker news, that the news is sorted according to a score similar to the following formula:
Score = Points/time^alpha
So the user's vote will be the corresponding to dig out the news, but the time will follow a certain index to bury the news. Here's our pattern, of course the algorithm is up to you.
The pattern is this, starting with looking at items that might be up-to-date, such as 1000 of the news on the first page are candidates, so let's just ignore the others, which is easy to implement.
Each time a new news post comes up, we add the ID to the list and use Lpush + LTRIM to ensure that only the latest 1000 items are removed.
There is a background task to get this list and continue to calculate the final score for each of the 1000 news articles. The results are populated by the Zadd command in the new Order, and the old news is cleared. The key idea here is that the sort work is done by the background task.
5. Processing Overdue Items
Another common sort of item is sorting by time. We use Unix time as a score.
The pattern is as follows:
-Each time a new item is added to our non-Redis database, we add it to the sorted collection. Then we use the time attribute, Current_time and time_to_live.
-Another background task using Zrange ... Scores queries the sorted collection and takes out the latest 10 items. If the Unix time is found to have expired, delete the entry in the database.
6. Counting
Redis is a good counter, thanks to Incrby and other similar commands.
I believe that you have tried many times to add new counters to your database to get statistics or display new information, but eventually you have to discard them because of write sensitivity.
Okay, now using redis doesn't have to worry anymore. With atomic increment (atomic increment), you can safely add a variety of counts, reset with Getset, or let them expire.
For example, this action:
INCR user:<id> EXPIRE
User:<id> 60
You can figure out the number of page views that have recently been paused for up to 60 seconds between pages, and when the count reaches like 20 o'clock, you can show some banner hints, or anything else you want to show.
7. Specific projects within a specific time period
Another is difficult for other databases, but the easy part of Redis is to count how many specific users have visited a particular resource during a particular period of time. For example, I want to know some specific registered users or IP addresses, how many of them have visited an article.
Every time I get a new Page view I just need to do this:
Sadd page:day1:<page_id> <user_id>
Of course, you might want to replace day1 with Unix time, such as timing ()-(Times ()%3600*24) and so on.
Want to know the number of specific users? You only need to use SCard page:day1:<page_id>.
Need to test if a particular user has access to this page? Sismember page:day1:<page_id>.
8, real-time analysis of the situation is happening, for data statistics and prevention of spam, etc.
We've only done a few examples, but if you look at Redis's command set and combine it, you'll get a lot of real-time analytics, effective and very labor-efficient. Using the Redis Primitives command makes it easier to implement spam filtering systems or other real-time tracking systems.
9, Pub/sub
Redis's pub/sub is very, very simple, stable and fast to run. Supports pattern matching and enables real-time subscription and cancellation of channels.
10. Queue
You should have noticed that Redis commands such as List push and list pop are easy to perform queue operations, but can do more than that: for example, Redis also has a variant command of list pop, which blocks the queue when the list is empty.
Message Queuing (Messaging) is used extensively in modern Internet applications. Message Queuing is used not only for communication between components within the system, but also for interaction between the system and other services. The use of Message Queuing can increase the scalability, flexibility, and user experience of the system. A system that is not message-queuing-based, whose speed depends on the speed of the slowest component in the system (note: short-board effect). Message Queuing allows the components in the system to be decoupled so that the system is no longer constrained by the slowest components, and the components can run asynchronously to perform their work faster.
In addition, when the server is in high concurrency, such as writing log files frequently. You can use Message Queuing to implement asynchronous processing. This enables high-performance concurrent operations.
11. Cache
The cache portion of Redis is worth writing a new article, and I'm just saying it briefly. Redis can replace memcached, so that your cache can only store data to be able to update data, so you no longer need to regenerate the data every time.
Redis data types and scenarios