Redis Application Scenarios

Source: Internet
Author: User

Redis differs from other database solutions in many ways: it uses memory to provide primary storage support, and only uses hard disk for persistent storage; its data model is unique and is single-threaded. Another big difference is that you can use the capabilities of Redis in your development environment, but you don't need to go to Redis. Turning to Redis is certainly desirable, and many developers have made Redis the preferred database from the outset, but it is not easy to replace the database framework if your development environment is already set up and the application is already running on it. In addition, in some applications that require large-capacity datasets, Redis is not suitable because its datasets do not exceed the memory available to the system. So if you have big data applications and mostly read access patterns, Redis is not the right choice.

However, one of the things I like about Redis is that you can incorporate it into your system, which can solve a lot of problems, such as tasks that your existing database is slow to handle. This allows you to optimize with Redis or create new features for your app. In this article, I want to explore how to add Redis to an existing environment and use its primitive commands to solve some of the common problems encountered in traditional environments. In these cases, Redis is not a preferred database.

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

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.

Frequent query database, is certainly very slow, we can when the user inserts a data, lpush into the queue primary key ID, need to keep the latest 20, we truncate the queue, LTrim queue name 0 20,

In this way, always keep the latest 20 primary key IDs, and then hit the database directly based on the primary key, and soon, of course, if it's not big data, we can cache it directly into Redis, hash, or set

2. 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.

It is not possible to use caching for data that changes in real time, but the frequent operation of DB increases the burden on the database, considering memory.

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:

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>.

3 Count

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 been paused for more than 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.

4 Queues

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. For high-performance concurrent operations

Redis Application Scenarios

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.