# Design live commenting

## Design live commenting

> Ref: [Infoq - Streaming a Million Likes/Second: Real-Time Interactions on Live Video](https://www.infoq.com/presentations/linkedin-play-akka-distributed-systems/?useSponsorshipSuggestions=true)

> Ref: [FB - Building Real Time Infrastructure at Facebook](https://www.youtube.com/watch?v=ODkEWsO5I30)

> Ref: [FB - Live Commenting: Behind the Scenes](https://www.facebook.com/notes/10158791486967200/)

> Ref: [1p3a 讨论](https://www.1point3acres.com/bbs/thread-621854-1-1.html)

## Requirements

### Functional requirements

* user can leave comment, real-time
* others can see comments/posts in real-time
* only focus on comments

### Non-functional requirements

* high available
* low latency

***

## Traffic Estimates

* 100 M posts, any posts can receive comments
* our system should handle 100k/s comments delivery

***

## High-level Design

### Single host pattern

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-e523b23a1a8b8ba52661617761d82262c538ec0a%2Flive_comment.png?alt=media)

***

## Detailed Design

### scale challenge 1: hard to manage multiple connections

* Linkedin has built AKKA, which has multiple Actors
* each Actor is responsible for communicating via one connection

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-a526cbd4bd2ca6f9c843af0d80a4088ae29bf141%2F1.png?alt=media)

***

### scale challenge 2: watch multiple videos and comment

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-f437c24eba49ef82590f892255977a934f445a71%2F2.png?alt=media)

#### Pub/Sub pattern

* viewers subscribe to live videos
* each host maintain a in-memory subscription table

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-9f3dd25b030171d83287e4f0e6a8f0c66d4dd762%2F3.png?alt=media)

* publish using subscription

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-d85485b7fe16332ce7cf4301dfda1f193902f0fe%2F4.png?alt=media)

***

### scale challenge 3: 10 K Concurrent viewers

#### Add a Real-time Dispatcher Service

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-0987f4241046cbfc003dd01634eb2bc3955c60ba%2F5.png?alt=media)

#### Subscribe Process

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-957c60caad1bc8efb372f65e770bd5634d7448df%2F6.png?alt=media)

#### Publish(Fan-out) Process

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-8198598536cccf1acc8103dd288ce07777d320f6%2F7.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-1c157a5086b8fb2a197daecd9e827fbd283aa294%2F8.png?alt=media)

***

### scale challenge 4: 100 k /s

#### Use a key-value store for subscription (e.g. Redis)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-d4005133219ce75d4126e6585e9e5b84a218145e%2F9.png?alt=media)

#### Subscribe Process

* user publish comments via dispatcher service
* dispatcher service looks up the nodes for those videos
* dispatch comments to frontend nodes
* nodes fanout to subscribed users

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-6907614697374126fffd709c70b21fe8496ac47b%2F10.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-5712cecb659d8485fd261947bfa0648ceff8fc17%2F11.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-d8072ccb06d5365a23e7e5b6bac63689acb500a2%2F12.png?alt=media)

***

### scale challenge 5: 100 messages/s, 10 K viewers, distribution of 1M messages/s

#### Subscription flow

* user first subscribe the video in the frontend
* in the frontend, it maintains a subscription table, noting down the `<topic-connectionId>` entry
* frontend route subscribe requests to dispatcher service
* dispatcher service then register `<topic-node>` entry in Redis

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-14cded53ea6226646730d1024c175bc5cf9cf474%2F13.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-88dd3d03ccc13d46e009cc634e29d5f1e84747e8%2F14.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-2f6c8090b85d5ad71dc1db84ab9f21d9ce9af6af%2F15.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-2a1cbe84ee863b7a0e82e5b1c14e36e4e2968eb0%2F16.png?alt=media)

#### Publish flow

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-6878e30d4f82fd21a4923aec0763d6d876d0f241%2F17.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-3abf618b543aebf9388f910656ee4aaae3e03f85%2F18.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-10b36c8cfa6d10e064c4e98b9cb967ce254ff9bf%2F19.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-d9c8d4c1dbbb00500a373e309848475cd4092d4f%2F20.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-705d074a5b10aeb3d7a25e98cbf74a74be2c4871%2F21.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-9401bd576f2b399d37c1c17962c2467e3ae6554a%2F22.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-9d8a6555bf736e6db73c69bc8c3a450215514782%2F23.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-e338bf39e5fc3a9cf50025debd14ed38e5c784ff%2F24.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-50015596f5be783a41f85cd08a0784944c06f529%2F25.png?alt=media)

![](https://2407442552-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lpv9LvHzpublmUWisvz%2Fuploads%2Fgit-blob-c1a2f7ad388e5422d2d6521b2a6395eece5a5c72%2F26.png?alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/system-design/chapter-2/youtube_scott/live_comment.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
