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

How to access the massive data in Redis correctly

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to correctly access the huge amount of data in Redis, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I. Preface

Sometimes we need to know the use of online Redis, especially the key value of some prefixes. How can we check it? And usually the data in Redis is huge, so we access the huge data in Redis? How to avoid accidents! Today, I would like to share with you a small knowledge point. I hope you can spray it lightly.

Second, accidents occur

Because our user token cache is key in [user_token:userid] format, the value of the user's token is saved. Our operation and maintenance staff to help developers find out how many logged-in users are online.

The keys user_token* method was directly used for inquiry, and the accident occurred. Causes Redis to be unavailable, faking death.

Third, analyze the reasons

We have millions of online login users, a large amount of data; keys algorithm is a traversal algorithm, the complexity is O (n), that is, the more data, the higher the time.

When the amount of data reaches several million, the keys instruction will lead to a Redis service stutter, because Redis is a single-threaded program that executes all instructions sequentially, and other instructions must wait until the current keys instruction has been executed before continuing.

IV. Solution

So how do we go through a large amount of data? This is also what the interview often asks. We can use another command of Redis, scan. Let's take a look at the features of scan:

Although the complexity is also O (n), it is done step by step through cursors and does not block threads.

Provide the count parameter, which is not the number of results, but the number of Redis single traversal dictionary slots (approximately equal to)

Like keys, it also provides pattern matching

The server does not need to save the state for the cursor. The only state of the cursor is the cursor integer returned by scan to the client.

It is very important that the returned results may be duplicated and need to be repeated by the client.

A single return does not mean that the traversal is over, but depends on whether the returned cursor value is zero.

4.1. scan command format

4.2. Command interpretation

The number of elements returned by the scan cursor MATCH count per iteration

The SCAN command is an incremental loop that returns only a small portion of the elements per call. So I won't let Redis fake his death.

The SCAN command returns a cursor that traverses from 0 to 0.

4.3, examples

Traversal starts at 0, cursor 6 is returned, and data is returned. To continue scan traversal, start at 6.

The above content is how to correctly access the huge amount of data in Redis. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Servers

Wechat

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

12
Report