AI Product Recommendation System

Product Suggestions for an Online Store

Design the system that picks which products an online store shows on its home page, product pages, cart and emails, using purchase history, the current session and business rules, in well under a tenth of a second.

Overview

An online store shows product suggestions in many places: 'Inspired by your history' on the home page, 'Frequently bought together' on a product page, add-ons in the cart, and a few picks in a weekly email. This problem is the system that fills those slots.

It differs from a video or feed recommender in three ways. Purchases are rare and strong signals, and many are one-offs (someone who just bought a drill does not need another). What the shopper is doing right now (the product open on screen, the cart) often matters more than their history. And the business has rules the model does not know: out-of-stock items must never appear, returned items should not come back, and some slots are sold to advertisers. The general machinery (two-tower retrieval, nearest-neighbor search, training data) is covered in the Recommendation Engine topic; this one focuses on what a store adds.

Questions to ask first

  • Which surfaces? — Home page, product page, cart, and a nightly email
  • How big is the catalog, and how many shoppers? — 50 million products; 20 million shoppers a day
  • How fresh must picks be? — Within seconds of a click or an add-to-cart in the same session
  • Must logged-out visitors get picks? — Yes, from the session and popular items
  • Who controls business rules? — Merchandising staff, without retraining a model
  • How is success measured? — Clicks, add-to-carts and orders from the widget, in A/B tests

What you will be able to do

  • Split the work into candidate generation, ranking and a business-rule layer, and give each a latency budget.
  • Build a similar-items table the way Amazon described in 2003, and say why it scales.
  • Use the current session so the picks change with the next click, not the next nightly job.
  • Handle new shoppers and new products, and keep out-of-stock and returned items off the page.
  • Measure a change with a proper A/B test, including how to assign users to groups.

Key concepts

  • Item-to-item collaborative filtering
  • Candidate generation
  • Ranking
  • Session context
  • Business-rule layer
  • Cold start
  • Impression logging
  • A/B testing
  • Fallbacks

Functional requirements

  • Return up to 20 products for a surface (home, product page, cart) for a known or anonymous shopper
  • Return similar and frequently-bought-together items for a product
  • Reflect the current session (views, add-to-carts) within seconds
  • Never show out-of-stock items; exclude items the shopper recently bought or returned
  • Let staff add rules (boost, bury, exclude a category) that take effect without retraining
  • Place sponsored items in fixed slots, labeled as sponsored
  • Produce nightly picks for email campaigns
  • Log every impression and click with the request that produced it, for training and experiments

Non-functional requirements

  • p99 latency of 100 ms for the recommendation API (a target we chose; the page renders without the widget if it is late)
  • High availability: a failure degrades to popular items, never to an error on the page
  • Session signals visible to the next request within about 5 seconds (our target)
  • Rule changes live within minutes
  • Experiments assign each user to the same group on every visit

Interview questions

  • Why split recommendations into candidate generation and ranking?
  • How does item-to-item collaborative filtering work, and why does it scale?
  • The shopper just added a laptop to the cart. How do the next recommendations reflect that?
  • How do you handle cold start for new users and new products?
  • Where do business rules like out-of-stock and sponsored slots go?
  • How would you A/B test a new ranking model?
  • What happens when part of the system fails?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio