Focused Redis Topic Day 1 - #2

Key Schemas and Object Relational Managers

In this topic …

Based on a brief mention in the official Redis documentation on suggested rules about Redis keys, a consistent key naming schema improves comprehensibility and maintainability of your Redis solution while positioning the datastore for future growth. Redis itself does not have any schema checking or validation functions although some basic validation can be done through the use of the EXISTS and TYPE Redis commands.

Relationship Modeling

A key schema should provide guidance for adding new Redis keys to an existing Redis-based application. An effective Redis Key schema establishes a naming conventions relating keys together. These relationships loosely couple data structures together onto which application and business logic can apply through client code.

Delimiters

The most commonly used delimiters between words used in Redis Keys and one seen through-out Redis documentation and examples is a colon : but any unicode character can be used with other choices for delimiters being a forward slash / and period . .


Book-Film Key Schema

A simple Book-Film Catalog Schema

Name Redis Data Type Description Relationships
book:{counter} Hash Stores title, author, ISBN, format, copyright date, page number, and price metadata for a book Key is stored in different Genre sets and Sales ranking sorted sets
books:{genre} Set A set of Redis keys for books classified as a genre, such as popular fiction, mysteries, science fiction, and technical books Stores all book keys that have been classified as a single genre. Used with other genre sets for calculating books in multiple genres with SINTERSTORE and books that are only in a single genre with SDIFFSTORE
books:sales-rank sorted set Stores sales ranking of each book with the total number of titles sold as the score in the sorted set Stores ranking of all Redis book keys

Example Book & Film Key Schema

all:sales-rank
global:book
book:1
book:2
book:3 
books:genre:popular-fiction
books:genre:sci-fiction
books:format:ebook
books:format:paperback
books:sales-rank
global:film
film:1
film:2
films:genre:comedy
films:genre:drama
films:format:bluray
films:format:dvd
films:sales-rank    
   

Node.js ORMs

Nohm

A node.js implementation of a Redis Object Relations mapper. Project is available at http://maritz.github.com/nohm/

var nohm = require('nohm').Nohm;
var redis = require('redis').createClient();

nohm.setClient(redis);
nohm.setPrefix('catalog'); 

nohm.model('Book', {
  properties: {
    title: {
      type: 'string',
      unique: false,
      validations: [
        'notEmpty'
      ]
    },
    author: {
      type: 'string',
      unique: false
    },
    price: {
      type: 'integer',
      defaultValue: 20
    }
 }
});

nohm.model('Sales', {
  properties: {
    salesDate: {
      type: 'datetime'
    }
  }
});

var infiniteJest = nohm.factory('Book');
var bookSales = nohm.factory('Sales');


infiniteJest.p({
  title: 'Infinite Jest',
  author: 'David Foster Wallace',
  price: 20
});

bookSales.p({
  salesDate: '2016-03-09'
});

bookSales.link(infiniteJest, 'item');

infiniteJest.save();
bookSales.save();
   

Redis Key Structure

127.0.0.1:6379> KEYS *
 1) "catalog:hash:Sales:ilkx1bwy9813lk9wqtrb"
 2) "catalog:meta:idGenerator:Book"
 3) "catalog:meta:version:Sales"
 4) "catalog:meta:version:Book"
 5) "catalog:meta:properties:Sales"
 6) "catalog:relations:Book:itemForeign:Sales:ilkx1bwxls4rf8loq10"
 7) "catalog:meta:idGenerator:Sales"
 8) "catalog:relationKeys:Book:ilkx1bwxls4rf8loq10"
 9) "catalog:relationKeys:Sales:ilkx1bwy9813lk9wqtrb"
10) "catalog:idsets:Book"
11) "catalog:relations:Sales:item:Book:ilkx1bwy9813lk9wqtrb"
12) "catalog:meta:properties:Book"
13) "catalog:idsets:Sales"
14) "catalog:hash:Book:ilkx1bwxls4rf8loq10"

   

Exercise 1 - MONITOR mode

A useful troubleshooting technique is to connect to your running Redis instance with redis-cli and issue the MONITOR command.

127.0.0.1:6379> MONITOR
OK
1457532195.717891 [0 127.0.0.1:50575] "info"
1457532195.722390 [0 127.0.0.1:50575] "get" "catalog:meta:version:Book"
1457532195.722526 [0 127.0.0.1:50575] "get" "catalog:meta:version:Sales"
1457532195.722613 [0 127.0.0.1:50575] "sismember" "catalog:idsets:Book" "-1457531830657"
1457532195.722795 [0 127.0.0.1:50575] "sismember" "catalog:idsets:Sales" "-1457531552414"
1457532195.726865 [0 127.0.0.1:50575] "set" "catalog:meta:version:Book" "a4f06ba1f03cf36e89cf433a523c41736d97c954"
1457532195.726920 [0 127.0.0.1:50575] "set" "catalog:meta:idGenerator:Book" "default"
1457532195.726952 [0 127.0.0.1:50575] "set" "catalog:meta:properties:Book" "{\"title\":{\"type\":\"string\",\"unique\":false,\"validations\":[\"notEmpty\"]},\"author\":{\"type\":\"string\",\"unique\":false},\"price\":{\"type\":\"integer\",\"defaultValue\":20}}"
1457532195.727017 [0 127.0.0.1:50575] "set" "catalog:meta:version:Sales" "c9ba341ce99c8fe91d9ecfe6d10297d46640fd88"
1457532195.727043 [0 127.0.0.1:50575] "set" "catalog:meta:idGenerator:Sales" "default"
1457532195.727060 [0 127.0.0.1:50575] "set" "catalog:meta:properties:Sales" "{\"salesDate\":{\"type\":\"datetime\"}}"
1457532195.730246 [0 127.0.0.1:50575] "sadd" "catalog:idsets:Book" "ilkx1bwxls4rf8loq10"
1457532195.730651 [0 127.0.0.1:50575] "sadd" "catalog:idsets:Sales" "ilkx1bwy9813lk9wqtrb"
1457532195.733823 [0 127.0.0.1:50575] "MULTI"
1457532195.733858 [0 127.0.0.1:50575] "hmset" "catalog:hash:Book:ilkx1bwxls4rf8loq10" "title" "Infinite Jest" "author" "David Foster Wallace" "price" "20" "__meta_version" "a4f06ba1f03cf36e89cf433a523c41736d97c954"
1457532195.733911 [0 127.0.0.1:50575] "EXEC"
1457532195.733917 [0 127.0.0.1:50575] "MULTI"
1457532195.734083 [0 127.0.0.1:50575] "hmset" "catalog:hash:Sales:ilkx1bwy9813lk9wqtrb" "salesDate" "2016-03-09" "__meta_version" "c9ba341ce99c8fe91d9ecfe6d10297d46640fd88"
1457532195.734122 [0 127.0.0.1:50575] "EXEC"
1457532195.736822 [0 127.0.0.1:50575] "MULTI"
1457532195.736939 [0 127.0.0.1:50575] "sadd" "catalog:relationKeys:Sales:ilkx1bwy9813lk9wqtrb" "catalog:relations:Sales:item:Book:ilkx1bwy9813lk9wqtrb"
1457532195.736972 [0 127.0.0.1:50575] "sadd" "catalog:relations:Sales:item:Book:ilkx1bwy9813lk9wqtrb" "ilkx1bwxls4rf8loq10"
1457532195.736996 [0 127.0.0.1:50575] "EXEC"
1457532195.737128 [0 127.0.0.1:50575] "MULTI"
1457532195.737464 [0 127.0.0.1:50575] "sadd" "catalog:relationKeys:Book:ilkx1bwxls4rf8loq10" "catalog:relations:Book:itemForeign:Sales:ilkx1bwxls4rf8loq10"
1457532195.737495 [0 127.0.0.1:50575] "sadd" "catalog:relations:Book:itemForeign:Sales:ilkx1bwxls4rf8loq10" "ilkx1bwy9813lk9wqtrb"
1457532195.737518 [0 127.0.0.1:50575] "EXEC"