How Redis expires keys > stdout.in Ievgen Kuzminov IT blog

How Redis expires keys

Dec 24, 2013, 6:29:49 PM

redis

I am often asked by my "junior" colleagues - what happens with Redis keys after expiry time, are these expired keys automatically being deleted or are they just "hang in RAM till The End Of Times" ? *Dramatic music! :)*
Fine, if you are to lazy to read docs...

Redis keys are expired in two ways: a passive way and an active way.

Active

When client access key and the key is found to be timed out - it is being deleted.

Passive

The most interesting - what happens with keys that are expired are are never accessed after expiry.
10 times per second Redis check 100 random keys with associated expiry time from the keyspace and remove expired keys.
If more than 25 keys were expired, start check again.

This is a probabilistic algorithm, where the percentage of keys that are likely to be expired is under 25%. This means that at any given moment the maximum amount of keys already expired that are using memory is at max equal to max amount of write operations per second divided by 4. That means that about 1/4 of RAM occupied by Redis could be used to store "expired" keys.

comments powered by Disqus
Ievgen
Kuzminov "iJackUA"
Web Team Lead
at MobiDev (Kharkiv, Ukraine)
Code in Ruby and Elixir, but still love PHP. Explore ES6 and Vue.js. Explore databases, use Ubuntu and MacOS, think about IT people and management

Notes


Ansistrano- ansistrano.deploy and ansistrano.rollback are Ansible Galaxy roles to easily manage the deployment process for scripting applications such as PHP, Python and Ruby. It's an Ansible port for Capistrano.