Skip to content
← All work

AuraFlix

A streaming backend where the two interesting problems are getting playback to start quickly and counting views without writing to Postgres on every play.

Start playing before the file has arrived

Serving a video file as one response means the viewer waits for the whole thing. The fix is HTTP range requests: the player asks for a byte range, the server returns that slice with a 206, and playback starts on the first chunk while the rest streams in behind it. Seeking works for the same reason, because the player can jump straight to the range it needs.

Playback starts in a second or two, which is the number that decides whether someone watches or leaves.

Counting views without hammering the database

A view counter is a deceptively expensive feature. Written naively, every play becomes a write to Postgres, and the hottest video generates the most contention on a single row at exactly the moment the system is busiest.

So counts go into Redis on the request path, and a background worker flushes them into Postgres in batches. Postgres sees a periodic batched update instead of a write per play. The count is eventually consistent, which for a view counter is a correct trade rather than a compromise.

Schema

SQLAlchemy 2 models with Alembic migrations, and hand-written SQL for the queries where the ORM was producing something worse than what I would write directly.