[Instagram] follower/following counter service

请问上课提到的follower/following count service的实现是和这个like services https://forum.acecodeinterview.com/t/topic/87类似吗? 还是有什么细节上有不一样的地方需要额外注意的?

我感觉这个follower/following count service的数据是follower/following table的衍生数据。我想到的简单实现就是:

1.run cronjob去定期更新这个follower/following count根据follower/following table data
2. when user calling follower/following count service
3.1 count service如果返回follower/following的数量少并且timestamp过期, 就每次pull一下follower/following service然后count得一个最新结果, 并更新当前 follower/following count service数据(类似读修正). 因为绝大多数情况显示需要特别准确这里比如 55个follower
3.2 count service如果返回follower/following的数量少并且timestamp没过期, 直接用这个值
3.3. count service如果返回follower/following的数量多(不管timestamp过不过期), 直接使用这个值, 因为绝大多数情况显示不需要特别准确这里比如5M个follower. 并且触发一个async job去更新这个follower/following count为这个用户如果timestamp过期

不知道有没有其他更好的方法?谢谢

1 个赞

先确定下来需求再做设计。我听下来你的需求定义是数量大的时候 count 不需要精确,而且这个 count 读取的 QPS 的比较高,需要 heavily cache。
具体讨论一下需求的话,我觉得 follower count 和 following count 的 QPS 和精度要求可能会有很大区别,可以分开讨论。
按照你的需求定义来考虑,我们需要 heavily cache 这些 count,就用 Redis 加上一定的 TTL 就可以了。TTL 根据精度要求来。

1 个赞