get Top k YouTube video
Functional Requirement
get top k YouTube video
get the number in 1min, 1 hour, 1 day or all time
Non-functional Requirement
service availability >> consistency(some service down)
We should support a massive number of videos.
We should support a massive incoming request.
search needs to be fast, we need to query top k fast < 200ms with time window setup
Core Entities
- Video
- Count
- Window size(1 second, 1 day, 1 month)
API Design
get view/videos?topK={}&window={}
High Level Design
very simple solution:
这里有个点:
为什么我们不写进db? 因为读写操作没有in-memory system 快,在我们有很多很多的video的时候这个system 更快
这里有点不一样的时候是计算是挺重要的:
Billion级别的 videos
假设10B * (16Byte) = 160 GB,这完全是能用in memory system handle的
Dive Deep
service availability >> consistency(some service down)
we could have multiple snapshots of in memory system, which could be in the s3 storage
如何做data partition?
那我们可以有一些random generator给他们分配一下partition, 然后把它们分配到不同的 in memory system里面。
get the number in 1min, 1 hour, 1 day or all time?
we can use kafka
具体例子:
假设我们有一个1小时的时间窗口,当前时间是 12:00。
"上升沿"消费者:从最新偏移量(比如 10000)读取新消息。
"下降沿"消费者:
计算目标时间:11:00
找到 11:00 对应的偏移量,假设是 9000
从偏移量 9000 开始读取消息
读取到偏移量 9500 时,发现消息时间已经超过 11:00,暂停消费
在 12:01 时,重新计算目标时间(11:01),找到对应的新偏移量(可能是 9010),从这里继续消费。