Wuaki.tv Tech Blog

Why we chose Riak

Alfonso Jiménez

Riak Logo

One of the projects we recently had at Wuaki was developing a service in order to highlight content and promote campaigns in our applications. Because of some technical decisions, we decided to build a micro service that could act as an adserver. This service had to be reliable, light and able to serve ads in multiple devices (web, smart TVs, iOS, Android, Xbox…). It also had to offer filtering options, since different ads would be displayed depending on user state (marketing targeting purposes). After trying out several options, we decided to use Riak as storage system. This was a classic problem for using a NoSQL database. We had a key to ask and a value to retrieve, so having Riak behind our application suited very well.

Some of the reasons why we chose Riak over any other NoSQL solution are High Availability and Key filters.

High availability

Riak distributes the data across multiple nodes through replication and partitioning. Replication makes the data to be fully available for reading and writing if any cluster node is down. It combines both of them to get the maximum data availability as possible without wasting resources. The official documentation suggests using 5 nodes as minimum for a Riak cluster and replicating the data at least into 3 nodes (this is the default replication value).

Riak follows a very clever hashing technique in order to map partitions to the edge of a ring. A Riak ring is divided into 64 partitions by default, being (2^160-1) the last value in the space. This last value is thought to be adjacent to the first value of the ring (0).

Riak Ring

Key filters and MapReduce

As I mentioned above, one of the requirements of our project was enabling filtering options. These filters had to be some kind of flags representing user states. For example whether the user is logged in or the user is a subscriber. Riak offers a fast way of examining keys before loading the object. This feature combined with MapReduce inputs gives us a very powerful method to pre-process and retrieve objects just by looking up at the key.

For example, if we have the following sample keys:

  # Format: country:zone:ad:active
  es:home:banner1:0
  es:home:banner2:1
  es:home:banner3:1

We can retrieve all the active ads in an easy way by using key filters:

{
  “inputs”:{
    “bucket”:“ads”,
    “key_filters”:[[“tokenize”, “:”, 4], [“matches”, “1”]]
  }
  // ... rest of MapReduce job
}

If you are eager to know more about Riak, I would suggest you to have a look at the official documentation. There is also an ebook called A little Riak book written by Eric Redmond, which you can get it for free in multiple formats.