๐จ๐๐ถ๐ป๐ด ๐ฆ๐๐ฟ๐ฟ๐ฒ๐ฎ๐น๐๐ ๐๐ฟ๐ฎ๐ฝ๐ต ๐ค๐๐ฒ๐ฟ๐ถ๐ฒ๐ ๐ณ๐ผ๐ฟ ๐ฉ๐ถ๐ฑ๐ฒ๐ผ ๐ฅ๐ฒ๐ฐ๐ผ๐บ๐บ๐ฒ๐ป๐ฑ๐ฎ๐๐ถ๐ผ๐ป๐
Our recommendation logic used to be a 280-line SQLite query. It used six self-joins to find what a user should watch next.
The goal was simple: find videos watched by people who watched this video. We wanted to weigh these results by recency and region.
We forced a graph problem through a relational engine. This caused problems:
- Queries took 400ms on a warm cache.
- Queries timed out on cold caches.
- Adding new signals like creator affinity meant adding more joins and more latency.
Relational models are not wrong, but they were the wrong shape for our data. Recommendations are graph problems. Users, videos, and tags are nodes. Watching or tagging are edges.
We moved this single subsystem to SurrealDB. We kept our PHP 8.4 and SQLite stack for everything else. The results changed immediately:
- Next-video queries dropped to 12-30ms.
- Adding new signals became a simple traversal instead of a complex join.
How it works: Collaborative filtering via traversal follows these steps: โข Find users who watched video A. โข Find other videos those users watched. โข Rank them by frequency, recency, and region.
In SQL, this requires multiple joins through a table. In SurrealDB, it is a single traversal because the edges act as the index.
We also solved for GDPR compliance: โข We do not store personal identifiers on the graph. โข A viewer is a pseudonymous session token with an expiration date. โข We use Cloudflare to detect region without storing IP addresses. โข This follows the principle of data minimization.
The architecture is lean. We use SurrealDB as a derived, disposable store. If it fails, we replay events from our logs. We use a Cloudflare Worker to cache results for 90 seconds. This prevents a surge of requests from hitting our database.
The lesson is not that graph databases are always faster. It is that modeling graph problems relationally creates a performance tax. If you have a query that uses many joins to follow relationships, consider isolating it.
Source: https://dev.to/ahmet_gedik778845/using-surrealdb-graph-queries-to-power-video-recommendations-303c