Frank DENIS random thoughts.

Redis as a recommendation engine

"You might be interested in red toilet paper, because you bought blue and green toilet paper, and people who also bought blue and green toilet paper tend to also buy red toilet paper".

Recommendation engines are now everywhere, from e-commerce to social networks.

Graphs databases like Neo4j are probably the best way to tackle the problem. But as an alternative, let's try building a recommendation engine on Redis.

Who bought what or who is following who could be stored in Redis sorted sets.

Let's take an example: a user buys A, and also B. We increment the score of the B item in the sorted set associated to key A. And we possibly also do it the other way round.

This is a trivial way to keep a list of what items have been bought, what are the related items and how many of them there are.

On a social network, the score associated to a relationship may reflect the level of trust.

Let's suppose we have the following relationships:

B -> C
D -> C
E -> F
A -> B
A -> D
A -> E

C is referenced by B and D. B and D are referenced by A. But C isn't referenced by A yet. This is presumably something to suggest.

F might also be a good candidate as a suggestion for A. However, since there's only a single path from A to F (through E), it's probably less relevant than C.

In order to limit the number of non-relevant results, we may want to add a threshold: nodes that have less possible paths than a cutoff value shouldn't be suggested.

How to do that with Redis, and only Redis?

Here's one possible way:

  • Use ZUNIONSTORE in order to build a temporary aggregate of all related items (scores are summed up), and remove the reference item from the set (cylic links).

  • Remove everything from the aggregate that is already directly related to the reference item. One way to achieve this on a sorted set is to use ZUNIONSTORE AGGREGATE MIN with a negative or null weight for the set we want to delete, followed by ZREMRANGEBYSCORE in order to actually remove everything from the reference set and everything below the cutoff score.

  • Sort and optionally trim the new set with ZREVRANGE et voila!

Maybe code speaks louder than words, so here we go for some code:

require "redis"

class Recommendation attr_reader :db

def initialize @db =

def bought(id), id) end

def suggestions_for(id, options = { }), id).suggestions(options) end

class Item def initialize(recommendation, id) @db, @id = recommendation.db, id end

<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">key</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span> <span class="s2">&quot;{rec}:items:</span><span class="si">#{</span><span class="nb">id</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">end</span>

<span class="k">def</span> <span class="nf">key</span><span class="p">()</span> <span class="no">Item</span><span class="o">.</span><span class="n">key</span><span class="p">(</span><span class="vi">@id</span><span class="p">)</span> <span class="k">end</span>

<span class="k">def</span> <span class="nf">tmp_key</span><span class="p">(</span><span class="n">n</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span> <span class="s2">&quot;{rec}:_tmp:</span><span class="si">#{</span><span class="vi">@id</span><span class="si">}</span><span class="s2">:</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">end</span>

<span class="k">def</span> <span class="nf">and_also_bought</span><span class="p">(</span><span class="n">linked_id</span><span class="p">,</span> <span class="n">weight</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span>
  <span class="vi">@db</span><span class="o">.</span><span class="n">zincrby</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">weight</span><span class="p">,</span> <span class="n">linked_id</span><span class="p">)</span>
<span class="k">end</span>

<span class="k">def</span> <span class="nf">suggestions</span><span class="p">(</span><span class="n">options</span> <span class="o">=</span> <span class="p">{</span> <span class="p">})</span>
  <span class="n">cutoff</span> <span class="o">=</span> <span class="n">options</span><span class="o">[</span><span class="ss">:cutoff</span><span class="o">]</span> <span class="o">||</span> <span class="mi">1</span>
  <span class="n">limit</span> <span class="o">=</span> <span class="n">options</span><span class="o">[</span><span class="ss">:limit</span><span class="o">]</span> <span class="o">||</span> <span class="mi">50</span>
  <span class="n">tmp</span><span class="p">,</span> <span class="n">tmp2</span> <span class="o">=</span> <span class="n">tmp_key</span><span class="p">,</span> <span class="n">tmp_key</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  <span class="n">related_keys</span> <span class="o">=</span> <span class="vi">@db</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="p">)</span><span class="o">.</span><span class="n">collect</span> <span class="p">{</span> <span class="o">|</span><span class="n">item</span><span class="o">|</span> <span class="no">Item</span><span class="o">.</span><span class="n">key</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="p">}</span>
  <span class="k">return</span> <span class="o">[</span> <span class="o">]</span> <span class="k">unless</span> <span class="n">related_keys</span>

  <span class="n">res</span> <span class="o">=</span> <span class="vi">@db</span><span class="o">.</span><span class="n">multi</span> <span class="k">do</span> <span class="o">|</span><span class="n">m</span><span class="o">|</span>
    <span class="n">m</span><span class="o">.</span><span class="n">zunionstore</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="n">related_keys</span><span class="p">,</span> <span class="ss">:aggregate</span> <span class="o">=&gt;</span> <span class="ss">:sum</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">zrem</span><span class="p">(</span><span class="n">tmp</span><span class="p">,</span> <span class="vi">@id</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">zunionstore</span><span class="p">(</span><span class="n">tmp2</span><span class="p">,</span> <span class="o">[</span> <span class="n">tmp</span><span class="p">,</span> <span class="n">key</span> <span class="o">]</span><span class="p">,</span> <span class="ss">:weights</span> <span class="o">=&gt;</span> <span class="o">[</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span> <span class="o">]</span><span class="p">,</span>
                                      <span class="ss">:aggregate</span> <span class="o">=&gt;</span> <span class="ss">:min</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">del</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">zremrangebyscore</span><span class="p">(</span><span class="n">tmp2</span><span class="p">,</span> <span class="s1">&#39;-inf&#39;</span><span class="p">,</span> <span class="n">cutoff</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">zrevrange</span><span class="p">(</span><span class="n">tmp2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">limit</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">m</span><span class="o">.</span><span class="n">del</span><span class="p">(</span><span class="n">tmp2</span><span class="p">)</span>
  <span class="k">end</span>
  <span class="n">res</span><span class="o">[</span><span class="mi">5</span><span class="o">]</span>
<span class="k">end</span>


# Example

r =

r.bought('b').and_also_bought('c') r.bought('d').and_also_bought('c') r.bought('e').and_also_bought('f') r.bought('a').and_also_bought('b') r.bought('a').and_also_bought('d') r.bought('a').and_also_bought('e')

puts r.suggestions_for('a')