Skip to main content

Key-value stores from Redis point of view

This post was supposed to be about graph databases and key-value stores but it's going to be only about key-value stores because I got more interested in trying out Redis than Neo4J.

Redis

Redis is a key-value store that keeps it's database in memory but it also stores it's database on disk after a predefined time and number of changes in database. By default the values are like this:

  • 900 seconds and at least 1 change
  • 300 seconds and at least 10 changes
  • 60 seconds and at least 10 000 changes
More on Redis can be found at their website http://redis.io/ and if your interest to give it a quick try I suggest their online tutorial at http://try.redis.io/.

Important about querying

This is a important detail with key-value stores. In a key-value store the data can be searched only by the key. There are solutions that enable searching by the data, like lucene or solr, but that's a whole different search engine and not the actual key-value store.

It might appear strange or constraining but it just means that key-value stores dont suit everywhere and that the key must be chosen with care.

Key and values

Keys and values sounds simple and actually sounds pretty familiar to software developers. Key-value pair is basically a map, something like this in Java:
Map<String, String> myMap = new HashMap<String, String>();

In the value part one can store simple data like a name of a user "John Doe" or email address "johndoe@foobar.com" but these small bits of information aren't neccesarily enough. Another approach is to store json data that could be something like this:
{ "name":"John Doe", "email":"johndoe@foobar.com", "nick":"JD" }

With this kind of data structure it's possible to save all sorts of stuff but to do it so that the data is searchable the key has to be something meaningful. If the keys are just sequence of numbers like [1,2,3,4,5,6...] to search for "John Doe" from a database with thousands of key-value pairs it wouldn't be efficient as the keys would have to fetched and the data parsed until John is found. 
Let's pretend that the json data above is user data for a online service and users log in by their email address and a password. To choose something unique and searchable I would use the email address and to make it even more specific I would use a key that looked something like this:
"user:email:johndoe@foobar.com"

Now all we need to know is the email address (that we get in the login) and all the users data can be fetched with that.


Values as hash maps

This is something I really like about Redis, the value can be a map of values. Sounds a bit bizarre but is actually pretty simple once you get a hold of it.

Lets say I've created a simple blog platform and the blog posts are in this kind of structure where first is the key, post meaning this is a blog post, email of the user and a random uuid and as a value a json data set:
"post:johndoe@foobar.com:dsada23132" "{"title":"first post", "date":"20130101","text":"lorem ipsum...."}"

As a new feature the platform gets a commenting option and I want the comments be under the same post key so that they can be fetched at the same time as the post but I don't want to put them in the same json data. The new data structure would be something like this:
"post:johndoe@foobar.com:dsada23132" "post" "{"title":"first post", "date":"20130101","text":"lorem ipsum...."}"
"post:johndoe@foobar.com:dsada23132" "comments" "[{"name":"Jane Doe", "date":"20130102","text":"Nice one!"}, {"name":"Jack Doe", "date":"20130102","text":"Boring..."}]"

With the field values post and comments I separated the data from each other but kept it under the same key.


Searching data

Data can be searched only by the keys so if we know the key we can search with it like in the simpler key-value data with the email address. In the blog example the searching could be done with part of the key. If we wanted to get all John's blog posts we would do a search like this: 
"post:johndoe@foobar.com:*"

And after that I could get all the data of a specific entry with a get all command:
"post:johndoe@foobar.com:dsada23132"


Or if I wanted to get just the post not the comments the search would have a field with it:
"post:johndoe@foobar.com:dsada23132" "post"

Summary

There's much more of key-value stores and Redis that I didn't mention here and it can all be found at their web site but these are the important bits of my post.
  • Searching only by the key
  • Choose the key with care
  • Data can be simple... or not
I've done some brief experimenting with Java and Redis and some of the results can be found under my gthub account https://github.com/jorilytter/redis-test

Comments

Popular posts from this blog

Automated browser testing in 2018

Every now and then I do some research on browser testing. More specifically I try to find and evaluate what kind of ready to use solutions or libraries have emerged since my previous research or if some of the previously tested solutions has come up with something new.

Back in September 2017 I had some extra time on my hands and I used a couple of days to see what are the new and interesting solutions at that time. I came across some tools that I have used in the past and found two new interesting options in which one stood out.

Say hello to TestCafe
The most interesting solution that I found was called TestCafe. It's a open source Node.js library from a company called Developer Express Inc. They also have a commercial product called TestCafe (confusing) which I haven't tried as the open source library provided everything I was interested in and it seemed to be under active development.

The thing that caught my interest at first was that this library doesn't depend on WebDri…

Studying and developing as software development professional

As everybody in software development knows, or should know, that studying and experimenting is something one must do to stay on top of the game. That said this time I'm writing about my experiences and ideas of studying. In this post I'll be covering different methods of studying and how I feel about them and what other types of resources are available.

Reading a book Reading a book is probably the most traditional way of studying and I do read a few books every year. To me this is a way to learn theory and principles of something but usually little to nothing to do with the actual implementation. This type book I usually read in a week or two and I like these books when their length is reasonable somewhere between 50 and 250 pages.
Reading a book with exercises These are very common type of books in software development. These usually cover some theory and the exercises bring a pragramatic approach with what one can learn a basic implementation. Some of these books are good i…

DIY home automation v1

For years I've been interested in home automation. I've had remote controllable outlets from a few different manufacturers but I've never been quite satisfied with just the remote. What if I could control my outlets within my local network from any device, now that's something I wanted to have.

Controlling outlets from computer A few years back I bought a three pack of remote controllable nexa outlets. A while ago I discovered that another company manufactured a control unit that's plugged in to a USB port and best of all they provided linux software for it.

To make full use of these I'd need a computer that's always on and that's where I could make use of Raspberry Pi.

Setting up outlets
I had already set up my outlets with the remote that came in the retail pack just follow the manufacturer instructions.
It might be possible to configure the outlets completely via the software at least for some brands but I haven't tried it so I can't be sure.…