Describe the bug

Sometimes, a key cannot be retrieve by the scan without explicit reason

Case where all work :

1) "guild:-802250104:invite:add:ec0dd734-bccb-4004-acb5-ec583286ff84"
 2) "guild:-802250104:invite:175d0669-e8a0-48f4-8518-b917586682f6"
 3) "guild:-802250104:invite:add:b4db5573-b3bb-4937-b118-d127d7906c23"
 4) "guild:-802250104:invite:add:69e59558-7a5b-46f8-84ae-9db14a72b949"
 5) "guild:-802250104:invite:4a66c77f-5a17-4890-b48f-03e48227b83a"
 6) "guild:-1028271001:add"
 7) "guild:-802250104:invite:0c183c48-cf88-44ca-be62-37746ed3f5c9"
 8) "guild:-802250104:invite:51888239-ebd8-446e-aca7-2c74ef733907"
 9) "guild:-802250104:invite:add:90ca6c4c-2f19-4519-b17d-75d050282006"
10) "guild:-802250104:member"
11) "guild:-802250104:invite:7cb2f29b-76ee-4ce4-ba31-5facfe15c29b"
12) "guild:-802250104:member:add"
13) "guild:-802250104:invite:add:baf636db-819c-4470-9840-aec7059da054"
14) "guild:-802250104:invite:7540874e-6fcd-4e84-a780-b08f9e0ac8dd"
15) "guild:-802250104:invite:214e4e2f-c9e0-4426-8f64-351ba7ddff00"
16) "guild:-802250104:invite:add:3ff94806-682e-4c8e-94c8-0f89be38ff23"
17) "guild:-802250104:invite:add:deed4a51-5b7f-43b3-a526-96daa2c3784b"
18) "guild:-802250104:invite:add:e46da8b0-140d-4672-a050-30458dde75c0"
19) "guild:-802250104:add"
20) "guild:-802250104:invite:1a8766d4-0521-4371-8cc8-a33ffc0ff0db"
21) "guild:-802250104:invite:add:e7cf849a-f586-4da0-b5d7-a06fc120bcb5"
22) "guild:-802250104:invite:af5b50dc-857f-48b5-85f6-a50657e2a950"
23) "guild:-802250104:invite:add:fc1211ed-95a5-427b-ae78-429bbde6d9d1"
24) "guild:-802250104:invite:dfb208d5-329b-48f6-94a4-6f84a628d87a"

127.0.0.1:6379> scan 0 MATCH guild:-1028271001:*
1) "26"
2) 1) "guild:-1028271001:add"
127.0.0.1:6379> keys guild:-1028271001:*
1) "guild:-1028271001:add"

In this case, the key related to -1028271001 is retrieve using KEYS and SCAN

However, sometimes, with another set of data

1) "guild:-217132932:member:add"
 2) "guild:-217132932:invite:e0cfd0f4-6411-4f50-af81-d19a383a8735"
 3) "guild:-217132932:invite:6d3e2bc1-c825-4a1b-9d84-860715e27b9e"
 4) "guild:-217132932:member"
 5) "guild:-217132932:invite:add:77069006-663b-478d-bf3f-dc88e83255f8"
 6) "guild:-217132932:invite:c5dabd18-1726-4f32-a45e-51a205b2b646"
 7) "guild:-217132932:invite:add:836c4fdf-dc27-4990-bc84-3b8ec0264def"
 8) "guild:-217132932:invite:add:b1611110-3d4d-45c9-8d48-8c26be20ed9f"
 9) "guild:-217132932:invite:8dd73bff-febc-4e1e-acb4-1eb2891edb70"
10) "guild:-217132932:invite:278f0f43-a10d-49ed-ae7c-3abdea5d43ee"
11) "guild:-217132932:add"
12) "guild:-1483063375:add"
13) "guild:-217132932:invite:add:547daaa7-15e5-45d1-aa7e-451329adf8cf"
14) "guild:-217132932:invite:7eb0c270-0537-4c5c-9cec-fd390f52885e"
15) "guild:-217132932:invite:c434b0f8-56fd-4553-9505-ae12fb0e3b3e"
16) "guild:-217132932:invite:e737861e-36dc-45af-b412-28498784156e"
17) "guild:-217132932:invite:34a187ff-af50-4ae3-81ba-cc7e003696c0"
18) "guild:-217132932:invite:add:6664cf11-4611-4cb7-a66c-0dfbb7367cbb"
19) "guild:-217132932:invite:add:b7661ec5-320d-4471-90a0-92076c38c01f"
20) "guild:-217132932:invite:856b56d7-fac3-4db0-82a3-f26b0eb225a1"
21) "guild:-217132932:invite:add:d1946da1-61cb-4d23-a4f8-46588d413c54"
22) "guild:-217132932:invite:add:45638883-ec89-4712-83ae-f275a80dda30"
23) "guild:-217132932:invite:add:e39fc1b8-dd39-4a72-9375-bef843e4bc97"
24) "guild:-217132932:invite:add:bfcd5a1a-b4ec-46fa-9f81-785e15d87739"

127.0.0.1:6379> scan 0 MATCH guild:-1483063375:add
1) "10"
2) (empty array)
127.0.0.1:6379> scan 0 MATCH guild:-1483063375:*
1) "10"
2) (empty array)
127.0.0.1:6379> keys guild:-1483063375:add
1) "guild:-1483063375:add"
127.0.0.1:6379> keys guild:-1483063375:*
1) "guild:-1483063375:add"

The key related to -1483063375 is not returned

To reproduce

Add the keys like above and try the commands

Expected behavior

SCAN should be given the keys corresponding to the MATCH pattern

Additional information

Redis : 7.0.8

Comment From: oranagra

looks like you haven't finished the fully SCAN (you got a cursor in response and you should call SCAN again with that cursor until you get a cursor of 0)

the SCAN command gradually iterates on the keys (without keeping any state at the server and without blocking the server for long like KEYS does). the filtering by your MATCH pattern is done on the result, so if a certain batch of keys scan iterated on doesn't contain any match, you'll get no results, other than a cursor which you should use to continue the scan.

let me know if i'm missing anything and you still think there's a bug.

Comment From: Distractic

Ok thanks, I will try to recall SCAN with 10

However, I don't understand why the cursor returns an empty array if the cursor is not finished.

So if I understand, the SCAN can returns a new cursor (because not finished) and an empty array, and maybe in the next cursor array can be not empty, that's it ?

Comment From: oranagra

yes, it finish the sweep though the whole key space yet, it sampled some 1000 hash slots, possibly found some keys, but nothing that matches the pattern, so it returned an empty array, and should be called again until the cursor that returns is 0