Architecture and Practices of Danmaku Interactive Video Platforms
This article explains the core technical architecture of danmaku (time-synced bullet comment) platforms. It covers how danmaku data is modeled and fetched in time segments, how traffic spikes are handled, how comment and activity feeds are designed, how login and messaging support these features, and how stability and content moderation are managed.
Architecture and Practices of Danmaku Interactive Video Platforms
1. Background and Problem Definition
Danmaku, or time-synced bullet comments, are real-time comments overlaid on a video. They have become one of the core forms of interaction on video platforms. A single video often runs several interaction subsystems at once: danmaku, comments, posts, favorites, and messages. These subsystems look independent, but they are tightly coupled in terms of data consistency, real-time performance, and concurrency.
A typical interactive video platform must solve four core problems:
- Real-time synchronization: Danmaku must appear at the correct point in the video, and viewers across the network should see a largely consistent timeline.
- Concurrency: When a popular video goes live, danmaku posting and fetching requests can reach dozens of times the daily volume.
- Community governance: Public content such as comments and posts needs matching review, reporting, and risk control mechanisms.
- Account system: Login state, message notifications, and membership benefits support every interactive feature.
The sections below cover each module.
2. Danmaku System: Real-Time Delivery and Timeline Alignment
2.1 Danmaku Data Model
The key difference between danmaku and regular comments is that each danmaku carries a time offset. A danmaku record typically contains the following fields:
{"video_id": "v123456","offset_ms": 73500,"content": "This part is amazing","user_id": "u_889","mode": "scroll","created_at": 1710000000}
offset_ms is the offset of the danmaku from the start of the video. The player renders the danmaku when playback reaches that point. Because of this design, the danmaku service is essentially a message stream indexed by the video timeline, not a simple comment list.
2.2 Fetching Strategy and Segmented Loading
If all danmaku for a video are sent at once, a long video can have tens of thousands of entries. Loading them takes time and wastes bandwidth. A common practice is to split danmaku into time windows:
- The client requests danmaku in segments of 5 to 10 minutes.
- The player preloads the current segment and the next one.
- Each danmaku segment has a version number or hash, so the client does not fetch the same data twice.
# Segmented fetch pseudocodedef fetch_danmaku(video_id, segment_index):start = segment_index * SEGMENT_MSend = start + SEGMENT_MSreturn db.query("SELECT * FROM danmaku WHERE video_id=%s AND offset_ms BETWEEN %s AND %s",video_id, start, end)
2.3 Peak Shaving Under High Concurrency
Common optimizations for popular content include the following:
- Multi-level caching: Danmaku segment results are cached in local memory and in a distributed cache. Hit rates for hot segments are usually high, because most viewers watch the same content.
- Delayed merge after writes: A newly posted danmaku does not need to enter an already cached segment immediately. A delay of a few seconds is acceptable, and the new items are delivered through an incremental interface.
- Client-side rendering: The client calculates danmaku positions and collision avoidance. The server only distributes data, which substantially reduces server load.
3. Comments and Posts: Feed Design for Community Interaction
3.1 Read Fan-Out vs. Write Fan-Out
Comment sections are a typical read-heavy workload. There are two common models:
- Read fan-out: Comments are stored under their content and assembled into pages at read time. This keeps the write path simple and makes deletions take effect immediately. The drawback is that deep pagination costs more to query. Most video comment systems use this model.
- Write fan-out: Each comment is written to the author’s timeline, and followers merge those timelines when they read. This suits follow-driven activity feeds, but the merge logic is complex.
In practice, a hybrid strategy is often used. Comment sections use read fan-out, while user profile and activity pages use pre-aggregated indexes organized by user.
3.2 Lightweight Design for Posts and Favorites
Posts are essentially streams of user behavior records, such as publishing, following updates, and favoriting. They can be modeled as a unified event table:
CREATE TABLE user_activity (activity_id BIGINT PRIMARY KEY,user_id BIGINT,type TINYINT, -- 1 publish, 2 favorite, 3 commenttarget_id BIGINT,created_at DATETIME,KEY idx_user_time (user_id, created_at));
Favorites require idempotency and stable ordering. Favoriting the same item twice does not create a duplicate record. Unfavoriting is implemented as a soft delete. The sort timestamp is refreshed when an item is favorited again.
4. Login Authentication and Messaging
4.1 Common Login State Design
Nearly all interactive features depend on login state. A common approach works as follows:
- The client sends its credentials to the authentication service.
- The authentication service verifies them and issues a token with an expiration time.
- The gateway layer validates the token centrally. Business services get the user identity from the user context that the gateway passes through.
Silent token renewal needs attention. During playback, a user may not interact for hours. If the token expires and danmaku posting fails, the experience suffers. A common solution is for the client to refresh the token automatically before it expires. If the refresh fails, the client falls back to read-only mode, so viewing is not affected.
4.2 Messages and Notifications
The messaging system handles two types of messages: those that require high real-time delivery, such as private messages and @mentions, and those that are not time-sensitive, such as system announcements. The first type is usually pushed through a persistent connection channel. The second type can be retrieved by polling. Key points for the persistent connection service include the following:
- Separate the connection layer from the business layer. The connection gateway handles only keep-alive and message forwarding.
- If a push fails, store the message in the database and let the user fetch it after coming online.
- For users logged in on multiple devices, deduplicate messages and synchronize read status across devices.
5. Stability and Governance
5.1 Degradation Strategy
An interactive system needs a degradation path. If the danmaku service fails, the player shows only the preloaded segments. If the comment section fails, it displays “Comments are temporarily unavailable.” If the persistent connection fails, the client falls back to polling. The core principle is that a failure in interactive features must not affect the main viewing experience.
5.2 Content Safety
All user-generated content should pass through a review pipeline before it is displayed. First, automated systems filter sensitive information. Higher-risk content then goes to a manual review queue. Danmaku are high-volume and low-value per message, so they typically use a “publish first, review later” policy with fast recall. Comments use a “review first, publish later” policy combined with a credit-based trust level.
6. Summary
The main technical challenge of a danmaku interactive video platform is not any single module. It is making four areas work together: timeline consistency, high-concurrency peak shaving, community governance, and the account system. The key trade-off in design is that interactive features can degrade, but the main viewing path must stay stable. Real-time data can tolerate a delay of a few seconds, but data consistency must be recoverable. Applying these principles, along with general infrastructure such as caching, message queues, and persistent connections, can produce an interactive content platform that delivers both a good experience and stable operation.
