Search code examples
databasekey-valueredisriak

What is the easiest way to simulate a database table with an index in a key value store?


What is the easiest way to simulate a database table with an index in a key value store? The key value store has NO ranged queries and NO ordered keys.

The things I want to simulate (in order of priority):

  1. Create tables
  2. Add columns
  3. Create indexes
  4. Query based on primary key
  5. Query based on arbitrary columns

Solution

  • If you use Redis (an advanced key-value store that supports strings, lists, sets, etc.) Then this is quite easy. I have already developed a C# redis client that has native support for storing POCO's data models. These exact same POCO's can be used by OrmLite to store it in a RDBMS.

    By the way Redis is fast, I have a benchmark that stores and retrieves the entire Northwind Database (3202 records) in under 1.2 seconds (running inside a UnitTest on a 3yo iMac).

    I store entities in two ways

    • Distinct entities, where I combine the Class type name and Primary Key to create a unique key e.g. urn:user:1
      • I then maintain a separate set of primary keys (in a Redis Set) to keep track of all my entities, using a key like: ids:user
    • In a Redis server side list - which acts very much like a table with support for paging, using a key like: lists:user