Adventures in Distributed Systems
Who are we?


Scalien is a startup developing open-source, cutting-edge distributed systems.

Go to scalien.com

Keyspace 1.8: Now features key expiry

by Marton Trencseni on 2010/08/11

Keyspace 1.8 is out, featuring key expiry in all the client libs: Python, Java, .NET, PHP, Ruby, Perl, HTTP!

This enables Keyspace to be used instead of Memcached in some use-cases. Unlike Memcached, Keyspace stores key expiries safely on disk, so keys are expired even if servers restart.

Key expiry commands are implemented as an overlay feature. This means that when you set an expiry on a key, Keyspace does not check whether the key exists, it just remembers that it should expire (delete) thay key at the given time. You can create the key at a later time, overwrite it, rename it, delete it, re-create it, all these operations do not affect the expiry.

There are three key expiry commands in Keyspace:

For the actual commands check the section of the documentation corresponding to your programming language.

Key expiry is implemented as an overlay feature to enable developers to mix and match these commands with the regular Keyspace commands to match their desired semantics. For example, if a developer thinks that setting (changing) a key-value pair should automatically remove any expiries, he can create a wrapper library which issues remove_expiry(k) command before issuing set(k, v).

When using key expiry commands in replicated mode, you should use NTP (Network Time Protocol) to synchronize the server's clock. Note that other than key expiries, Keyspace does not require or assume clock synchrony. When the Keyspace master receives a set_expiry(k, t) command, it adds t seconds to the current timestamp and replicates that timestamp. Key expiry will occur at that time by the actual master's system clock. If the master fails and another node becomes the master, key expiry will still occur at that time, but by the new master's system clock.

Finally, some Python sample code which illustrates key expiry in action:

import keyspace
import time
# connect to a single keyspace instance client = keyspace.Client(["localhost:7080"])
# clear all keys client.prune("")
# clear all expiries client.clear_expiries()
# create some keys client.set("foo1", "bar1") client.set("foo2", "bar2") client.set("foo3", "bar3")
# expire foo1 and foo2 in 10 seconds client.set_expiry("foo1", 10) client.set_expiry("foo2", 10)
# changed my mind about foo2 client.remove_expiry("foo2")
# sleep for 10 seconds time.sleep(10)
# now list db kv = client.list_key_values("") for k in kv: print("%s => %s" % (k, kv[k])) # foo2 and foo3 are listed


- Marton Trencseni


blog comments powered by Disqus