Real-time recommendation systems for session-based user activities

The difference between batch and real-time recommendation systems can be dramatic for the user experience. Is it really that hard to build a responsive system?
Blog
E-commerce

Table of Contents

When we have won the 2019 RecSys competition we built a huge system for extracting features. As it often is with the competitions they don’t always produce immediate practical results.

When I say “practical” the system we constructed was quite complicated – we had hundreds of features and the models were slow. It is not the first time such a thing happened. During the Netflix Challenge where they awarded 1 million dollars to the winning solution – the solution wasn’t used because it was too complex (https://www.wired.com/2012/04/netflix-prize-costs/). But the ideas survived and the competition itself sparked research interest in this domain.

Going back to the RecSys – after the competition finished we wanted to implement our ideas in a live system. When we talked with potential clients we lacked some arguments that we can deal with designing and implementing a working realtime solution.

Competitions vs real world scenarios

People often say that the competitions don’t always give you the experience needed to implement something in the production system. I tend to have a different opinion on this. Sure, the solutions that win are often too complex to be considered in a production setting. However if a person or a team is capable of creating a complex and good solution to a problem then the same person knows how to compromise and achieve slightly worse results with simpler methods. What I mean is that the competitions give a spectrum of possibilities. When you squeezed the last drop of water from a rock than it will be much easier to squeeze it from a lemon.

Why real-time recommendations are important?

As it turns out real-time recommendations matter. Compared to nightly recommendations Pixie (a system designed at Pinterest) improved user engagement by up to 50%. Read an article about it here.

Back to the drawing board – Architecture

We wanted the system to be fast and responsive. The technology stack we went for:

  1. Python
  2. Sanic web-framework (https://github.com/huge-success/sanic)
  3. Redis (https://redis.io/)

Why Python?

Although Python rarely is the first choice for performance-critical systems it turned out to be quite good for prototyping. Quickly testing new ideas is more important for us at this point than having 2-3 times more throughput. As we are quite good at Scala – we can rewrite it any time.

Why Sanic?

First, we implemented the web service in Flask but soon realized that it is not that fast. To make it fast you must rely on gunicorn to spawn many processes. Sanic at its core is asynchronous and running it using many workers is as easy as adding one parameter workers.

app.run(host="0.0.0.0", port=8001, workers=8)

Switching from Flask to Sanic was painless since both frameworks have almost the same API.

Why redis?

As it stands redis is the most popular key-value store in the rankings (https://db-engines.com/en/ranking).

In our solution, we rely on “accumulators” which track the statistics about the users, items, and interactions between them. Each accumulator has a state that must be updated many times per second. Redis seemed like an ideal choice because each accumulator has its distinct information schema.

There is another thing about redis that makes it even more interesting – it can store many data types that are very useful for recommendations: sets, bloom filters, maps. It would be possible to build a working recommendation system using only the commands this database engine provides – which is quite amazing. Here the author goes through all the scenarios how redis can be used in this context – http://nosqlgeek.blogspot.com/2019/02/building-recommendation-engine-with.html.

Some of the ideas from the blog:

  1. Content-Based Filtering (using Sets)
  2. Collaborative Filtering (using Sets)
  3. Ratings based Collaborative Filtering (using Sorted Sets)
  4. Social Collaborative Filtering (using RedisGraph)

Benchmarks

When testing the system on a local machine we achieve a throughput of about 2.5k events per second – which translates to about 30k writes to redis database. Some of the events like transactions spawn a separate process which saves the state of the user for later use. Based on those saved states we are able to build a scoring model.

Real-time item rescoring

When designing our system we wanted it to have the same quality as our solution in the RecSys competition. Collaborative filtering is not enough when it comes to personalization based on the activity on the website. Users do many things that are not considered very helpful to traditional recommendation systems.

Some examples of activities that can be important:

  1. Filtering the product results
  2. Sorting the product results
  3. Interacting with images, ratings, comments
  4. Does the user interact with many products or fewer specific ones?

Our system can extract features from all those activities and build a model that can analyze a set of items.

Cold start

Having a real-time system is useful when users have never been seen on your website. As it was for Trivago most of the users visit the website 1-2 times a year and many of them are not even logged in.

The system like ours can be used:

  1. Recommend products based on the last interactions with other products
  2. Boost search results to present better items to the user

Summary

It turned out that designing a realtime system for handling session context was not that hard for us. Batch predictions are of course different than streaming predictions but the same rules apply.

We spent many months on the RecSys competition and eventually we used this experience to build a fully functional system. Schedule a meeting with us to talk about how you can benefit from real-time recommendations.

Topics: Python | Redis

CTO / Data Scientist / Kaggle Grandmaster

CTO / Data Scientist / Kaggle Grandmaster

Other stories in category

Fraud detection for Polish Ministry of Health

Fraud detection for Polish Ministry of Health

Fraud detection for Polish Ministry...

GovTech Polska is using the competition formula to involve tech startups in solving state-scale technological challenges through Artificial Intelligence and Data Science. The central entity is the public sector, which reports challenges and looks for modern ways to solve them but the indirect beneficiaries are of course citizens. 

LogicAI Team

22 Nov 2022