Sub topic 12
Catalog, Search and Recommendations
This chapter assumes you know the metadata storage design from the previous chapters, can read short Python, and understand basic database indexing and message queues.
The catalog holds metadata, not video
The catalog is the database of everything a user can browse. Each record holds a title, description, tags, duration, thumbnail URLs, a publish status, and the URL of the HLS or DASH manifest. The catalog never holds media bytes.
Browsing and playback have different load profiles. Browsing is many small, read-heavy queries. Playback needs only the manifest URL, and the CDN then serves the segments. Keep the catalog in a relational or document database, cache hot pages, and treat it as the source of truth for everything else in this chapter.
Search
A LIKE '%term%' query on the catalog database scans rows and cannot rank results or tolerate typos. Instead, copy searchable fields into a dedicated search index such as OpenSearch or Elasticsearch. It provides full-text matching, typo tolerance, filters and ranking.
Feed the index from the catalog's change events, not from dual writes in the application. Dual writes drift apart when one of the two writes fails.
The index is eventually consistent, so a new video appears in search a few seconds after it is published. Two rules keep it correct:
- Index a video only when its status is
ready, meaning transcoding and packaging have finished. - Handle deletes and takedowns as events too. A removed video must leave the index quickly.
Recommendations
Recommendation systems usually run in two stages. Candidate generation narrows millions of videos to a few hundred. Ranking then orders those candidates with richer features such as recency, language and device.
The input is watch events from the player: starts, completions, and how long people watched. A simple candidate generator counts how often two videos appear in the same viewer's history.
from collections import defaultdict
from itertools import combinations
def co_watch_counts(histories):
counts = defaultdict(lambda: defaultdict(int))
for videos in histories:
for a, b in combinations(sorted(set(videos)), 2):
counts[a][b] += 1
counts[b][a] += 1
return counts
def recommend(counts, watched, k=3):
scores = defaultdict(int)
for v in watched:
for other, n in counts[v].items():
if other not in watched:
scores[other] += n
return sorted(scores, key=lambda x: (-scores[x], x))[:k]
histories = [["a", "b", "c"], ["a", "b"], ["b", "c", "d"], ["a", "d"]]
counts = co_watch_counts(histories)
print(recommend(counts, {"a"})) # ['b', 'c', 'd']Run the counting as a periodic batch job and write results to a key-value store keyed by user or video. The request path then does one lookup and stays fast.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Indexing videos before packaging finishes | Users click a result that cannot play | Index only at `ready` status |
| Dual writes to database and index | Index drifts from the catalog | Use change events |
| Computing recommendations per request | Slow pages and heavy load | Precompute in batch |
| No fallback for new users or videos | Empty rows (the cold-start problem) | Fall back to popular or recent titles |
Exercise
A video's status changes to ready, but it does not appear in search for several seconds. Is this a bug? Which design choice causes it?
Show answer
It is expected. The search index is fed asynchronously through a change-event queue, so it is eventually consistent with the catalog database. The delay is the price of keeping the index decoupled from catalog writes. It becomes a bug only if the delay grows without bound, which monitoring of the queue lag should catch.