Skip to content

Expiry time not accurate #169

philjackson opened this Issue Oct 29, 2011 · 6 comments

2 participants


It's possible that setting an expiry time on a key might take longer than one might expect to expire. Here's some code that proves the expiry time varies between runs:

#!/usr/bin/env coffee

redis = require "redis"
client = redis.createClient()

start = new Date().getTime()

client.setex "foo", 1, "bar", ( err, value ) ->
  f = ( ) ->
    multi = client.multi().ttl( "foo" )
                          .get( "foo" )

    multi.exec ( err, [ ttl, get ] ) ->
      if not get
        end = new Date().getTime()
        console.log( "#{ end - start } ms" )
        return client.quit()

      console.log( "ttl: #{ ttl }" )
      setTimeout f, 10


A variation of ~10 milliseconds seems fair but this can be up to a second out. When you're working at a second granularity it's too far.

antirez commented Oct 31, 2011

Hi, this is a good improvement indeed... it may cost us some memory for 32 bits, but for 64 bits it is completely for free...

I'm working at it right now, news soon. Thanks.


Cool, thanks, antirez.

antirez commented Nov 9, 2011

Hi, there is a branch implementing high res timers, the branch is called 'ttlres' and is pushed on github.
All the previous commands are of course compatible with the new resolution, so now TTL foo 100 will actually
set an expire of 100000 milliseconds. The new resolution is the millisecond basically, whatever command you use, but
now it is also possible to specify sub seconds expires:

EXPIRE foo 250 ms
TTL foo ms (this will get the timeout in milliseconds instead of the default seconds output)
EXPIRE AT foo ..... ms (also works to set an expire in the future with millisecond precision)

SETEX is still missing as a clear API for that is not clear.

The reason I did not used floats like EXPIRE foo 1.235 is because performances would suffer a lot from that in the parsing stage unfortunately. Also I think that to have the unit completely bind to the real max precision is a good idea.



Great news. Thanks for the hard work!

antirez commented Nov 10, 2011

thanks, we are in the process of changing the API probably but the feature will be merged into unstable in very short time.

antirez commented Nov 12, 2011

Hi, ttlres branch merged. Now all the old commands have millisecond resolution, we also have three new commands:

PEXPIRE <key> <milliseconds>
PSETEX <key> <milliseconds> <value>
PEXPIREAT <key> <unix time in milliseconds>

The above command also allow to specify sub-second expires since now they are internally supported so it was a shame not exposing this capability in its full power. We'll add the new commands in the documentation once 2.6 RC1 will be released.

Closing this issue.


@antirez antirez closed this Nov 12, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.