Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Redis: How is the result of KEYS * sorted?

I have a very simple php redis application which creates keys at certain events. All the keys are just counters and have an expire time of 24 hours. Basically a rolling window of 24 hours for every key to gather some statistics.

if ($redis->exists($key)) {
    $redis->incr($key); 
}
else {
    $redis->set($key, '1');
    $now = time(); // current timestamp
    $redis->expireAt($key, $now + 86400);
}

When I extract an overview of all my keys with $list = $redis->keys("*"); (or in a redis-cli console with keys *), I would suspect a chronological order by creation date. However this is not case. Neither are they ordered alphabetically, by value...

So my question is, how is this list sorted?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First of all, don't use keys * it is debug function not designed for a production, you can kill your server... If you need enumerate all keys in DB in safe way, use SCAN function with LIMIT.

Anyway results of keys or scan is not sorted in any manner, order of results related to internal memory structure of redis's hashtable.

About your php script, you can do it by a single command, without exists set expireat just run:

SET key 1 EX 86400 NX

EX 86400 mean expire in 86400 (1 day) seconds from now

NX mean create a key only if it doesn't exists.

If this command return (nil) run regular INCR key its mean that the key already exists. BTW INCR command will not remove your expire settings.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!