Tokyo Tyrant: The magic little database

I’ve been playing around with Tokyo Tyrant master-master replication and I have got to say that it is a magical little database. Here’s some sample code to configure two Tokyo Tyrant instances in master-master replication mode, as well as a script to show off some of Tyrant’s capabilities, including memcached protocol support.

#!/bin/bash
 
HOST=127.0.0.1
PORT=1978
SID=1
UPDATE_LOG_DIR=ulog/$SID
MASTER_HOST=127.0.0.1
MASTER_PORT=1979
 
mkdir -p $UPDATE_LOG_DIR
 
ttserver -host $HOST \
         -port $PORT \
         -ulog $UPDATE_LOG_DIR \
         -sid $SID \
         -mhost $MASTER_HOST \
         -mport $MASTER_PORT
#!/bin/bash
 
HOST=127.0.0.1
PORT=1979
SID=2
UPDATE_LOG_DIR=ulog/$SID
MASTER_HOST=127.0.0.1
MASTER_PORT=1978
 
mkdir -p $UPDATE_LOG_DIR
 
ttserver -host $HOST \
         -port $PORT \
         -ulog $UPDATE_LOG_DIR \
         -sid $SID \
         -mhost $MASTER_HOST \
         -mport $MASTER_PORT
import pytyrant
import time
 
t1 = pytyrant.PyTyrant.open('127.0.0.1', 1978)
t2 = pytyrant.PyTyrant.open('127.0.0.1', 1979)
 
print t2.get('foo', 'nothing here') # 'nothing here'
 
# Writing to t1 replicates to t2
t1['foo'] = 'bar'
print t2.get('foo', 'nothing here') # 'foo'
 
 
# Tokyo Tyrant also speaks the memcached protocol 
 
import memcache
mc = memcache.Client(['127.0.0.1:1978', '127.0.0.1:1979'])
 
print mc.get('foo') # 'bar'
mc.set('yo', 'sup') # Assigns ('yo', 'sup') pair to one of the servers in the pool 
 
# Both Tyrant servers should have ('yo', 'sup') because of master-master replication 
print t1.get('yo', 'nothing in t1') # 'sup'
print t2.get('yo', 'nothing in t2') # 'sup'

For more on Tokyo Tyrant and Tokyo Cabinet, check out this slideshow: