当前位置:网站首页>How to traverse massive data in redis
How to traverse massive data in redis
2022-07-01 04:59:00 【Java notes shrimp】
Click on the official account , utilize Fragment time to learn
Catalog
Preface
The accident happened
The analysis reason
Solution
summary
Preface
Sometimes we need to know the online redis Usage situation , In particular, we need to know some prefixes key value , How can we check it ? Today, Gu shared a little knowledge
The accident happened
Because our users token Caching is using 【user_token:userid】 Format key, Save the user's token Value . In order to help the development partners check how many login users are on the line, our operation and maintenance department .
Directly with the keys user_token* Way to query , The accident happened . Lead to redis Unavailable , Feign death .
The analysis reason
We have millions of online login users , There's a lot of data ;keys The algorithm is a traversal algorithm , Complexity is O(n), That is, the more data , The more time complexity .
The amount of data is millions ,keys This command will lead to Redis Service carton , because Redis It's a single threaded program , Execute all instructions in sequence , Other instructions must wait until the current keys After the command is executed, you can continue .
Solution
So how do we traverse large amounts of data ? This is also often asked in interviews . We can use redis Another order of scan. Let's see scan Characteristics
1、 The complexity is O(n), But it's done step by step with cursors , Will not block threads
2、 Provide count Parameters , Not the number of results , yes redis The number of slots in a single traverse Dictionary ( About equal to )
3、 Same as keys equally , It also provides pattern matching ;
4、 The server does not need to save state for cursors , The only state of a cursor is scan Cursor integer returned to the client ;
5、 The returned results may be repeated , Need client to repeat , This is very important ;
6、 Just because the result of a single return is empty doesn't mean the end of the traversal , It depends on whether the returned cursor value is zero
One 、scan Command format
SCAN cursor [MATCH pattern] [COUNT count]Two 、 Command interpretation :scan The cursor MATCH < Returns the elements that match the given pattern > count The number of elements returned per iteration
SCAN The command is an incremental loop , Each call returns only a small number of elements . So I won't let redis Feign death
SCAN The command returns a cursor , from 0 To traverse the , To 0 End traversal
3、 ... and 、 give an example
redis > scan 0 match user_token* count 5
1) "6"
2) 1) "user_token:1000"
2) "user_token:1001"
3) "user_token:1010"
4) "user_token:2300"
5) "user_token:1389"from 0 To traverse the , Cursor returned 6, Back to the data , continue scan Traverse , It's from 6 Start
redis > scan 6 match user_token* count 5
1) "10"
2) 1) "user_token:3100"
2) "user_token:1201"
3) "user_token:1410"
4) "user_token:5300"
5) "user_token:3389"summary
This is what interviews often ask , It is also often used by our small partners in the process of work , Small companies in general , There won't be any problem , But when there's a lot of data , Your operation is not right , Your performance will be deducted , ha-ha . thank you !!!
source :toutiao.com/i6697540366528152077
recommend :
Main stream Java Advanced technology ( Learning material sharing )

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !
边栏推荐
- 无器械健身
- STM32扩展板 温度传感器和温湿度传感器的使用
- Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
- Neural network convolution layer
- Pico neo3 handle grabs objects
- Pytoch (III) -- function optimization
- Pytorch convolution operation
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- Overview of the construction details of Meizhou veterinary laboratory
- 线程类的几大创建方法
猜你喜欢
![解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *](/img/88/0b99d1db2cdc70ab72d2b3c623dfaa.jpg)
解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *

Use of dataloader

The design points of voice dialogue system and the importance of multi round dialogue

【硬十宝典】——1.【基础知识】电源的分类

每日一题-LeetCode1175-质数排列-数学

神经网络-最大池化的使用

How to use common datasets in pytorch

Difficulties in the development of knowledge map & the importance of building industry knowledge map

LeetCode1497-检查数组对是否可以被 k 整除-数组-哈希表-计数

pytorch中常用数据集的使用方法
随机推荐
神经网络-卷积层
LeetCode_ 28 (implement strstr())
Pytoch (III) -- function optimization
Print stream and system setout();
Thoughts on the construction of Meizhou cell room
Distributed architecture system splitting principles, requirements and microservice splitting steps
Pytorch convolution operation
神经网络-最大池化的使用
Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
Overview of the construction details of Meizhou veterinary laboratory
[daily question in summer] function of rogu p3742 UMI
[daily question in summer] Luogu p1568 race
Pytoch (IV) -- visual tool visdom
【暑期每日一題】洛穀 P1568 賽跑
Principle, technology and implementation scheme of data consistency in distributed database
分布式事务-解决方案
Global and Chinese market of enterprise wireless LAN 2022-2028: Research Report on technology, participants, trends, market size and share
STM32 expansion board digital tube display
【暑期每日一题】洛谷 P3742 umi的函数
Pytoch (II) -- activation function, loss function and its gradient