Tokyo Tyrant - part 1. (Backup/Restore)

Tokyo Tyrant is a high concurrency network interface to the underlying database (Tokyo Cabinet).

It has been widely adopted in many web-related applications as backend data store.

This post will introduce Tokyo Tyrant's backup and restore capabilities.

Backup

figure 1.


The following example shows how to hot backup a ttserver, and recover a ttserver using the backup database as illustrated in figure 1.

 - first, start a ttserver.

ttserver /tmp/ex1.tch

 - on another terminal, write some data into it.

tcrmgr put localhost "foo" "bar"

 - check if we have a record (key:"foo", value:"bar").

tcrmgr get localhost "foo"

 - backup the database.

tcrmgr copy localhost /tmp/backup.tch

 - let's simulate the ttserver crashed by press crtl-c in the first terminal.

 - and database file is corrupted.

rm /tmp/ex1.tch

 - to recover the ttserver from previous backup, simply copy the database file and restart ttserver.

cp /tmp/backup.tch /tmp/ex1.tch

ttserver /tmp/ex1.tch

 - check if the data has been recovered

tcrmgr get localhost "foo"

Data loss can be prevented by scheduling periodically backup (once a day or a few minutes, depends on how frequently the writing operations are). But we still gonna lose all the modifications between the last and the next backups. Tokyo Tyrant also provide an approach to restore those modifications.

Restore

figure 2.

The following example shows how to restore the modifications from update log as illustrated in figure 2.

 - create a directory to hold update log.

mkdir /tmp/ulog

 - start a ttserver with update log enabled.

ttserver -ulog /tmp/ulog /tmp/ex2.tch

 - on another terminal, put some data into it.

tcrmgr put localhost "foo2" "bar2"

 - check if we have a record (key:"foo2", value:"bar2")

tcrmgr get localhost "foo2"

 - let's simulate the ttserver crashed by press crtl-c in the first terminal.

 - and the database file is corrupted.

rm /tmp/ex2.tch

 - instead of recovering the ttserver from backup, we can restore the database by replaying the update log.

 - backup the update log if needed.

mv /tmp/ulog /tmp/ulog-back

 - restart ttserver.

ttserver /tmp/ex2.tch

 - restore from update log.

tcrmgr restore localhost /tmp/ulog-back

 - check if the data has been recovered.

tcrmgr get localhost "foo2"