High-Concurrency Interaction Design for Danmaku and Comment Systems
This article examines the business characteristics and architecture of high-concurrency interaction systems on video platforms, focusing on danmaku (bullet comments) and comments. It covers layered architecture, time-axis sharding and caching for danmaku, two-level comment structures and pagination, content moderation and risk control, multi-device consistency, and availability degradation. Readers will learn practical design patterns for handling bursty writes, hot data, UGC safety, and eventual consistency in interaction systems.
High-Concurrency Interaction Design for Danmaku and Comment Systems
1. Business Characteristics of Interaction Systems
The interaction capabilities of a video platform mainly include danmaku (bullet comments), comments, likes, favorites, and dynamic posts. Among these, danmaku and comments are the most typical high-concurrency write and read scenarios. Their business characteristics determine the choice of technical solutions:
- High write-to-read ratio: During peak playback periods, popular videos generate massive volumes of danmaku and comment writes while also serving many user reads. Read and write pressure coexist.
- Strong temporal locality: Danmaku is tightly bound to the video timeline, and reads often only require data within a certain time window. This provides a natural basis for sharding and caching.
- Obvious hotspot aggregation: Head content may receive hundreds of thousands of interaction messages within a minute, while long-tail content has almost no traffic. The cold/hot difference is extremely large.
- Strict moderation requirements: User-generated content (UGC) must go through content safety moderation. The design of the moderation pipeline directly affects the publishing experience.
Understanding these characteristics is the starting point for the subsequent architecture design.
2. Overall Architecture Layers
A mature interaction system is usually divided into four layers:
- Access layer: Responsible for request authentication, rate limiting, and anti-abuse. For high-frequency write operations such as danmaku, requests must pass user identity, device fingerprint, and behavior feature checks before entering the subsequent pipeline.
- Service layer: Split by business domain into danmaku service, comment service, counter service, etc. Services are decoupled through message queues. For example, after a successful publish, counter updates and push notifications are triggered asynchronously.
- Storage layer: Usually adopts a combination of “database + cache.” Core data is stored in distributed storage that supports horizontal scaling, and hot data is accelerated through in-memory cache.
- Asynchronous pipeline: Non-real-time operations such as moderation, index building, and data statistics are fully asynchronous to ensure low latency on the main path.
3. Key Designs for Danmaku Scenarios
3.1 Time-Axis Sharded Storage
Danmaku is naturally suitable for storage sharded by video playback time. For example, with 60 seconds as one shard, the client only needs to request one or two shards near the current progress when fetching danmaku:
GET /danmaku/{video_id}?segment=5&count=100Returns the danmaku list for the 300–360 second interval
This design breaks the read pressure of “all danmaku” into multiple small-grained requests. Combined with caching, it can significantly reduce backend load.
3.2 Shard Caching and Preloading
For popular videos, each shard can be serialized and cached as a whole. After a video is published, the first few shards are usually accessed most densely, so the first-screen shards can be preheated when upload completes:
def cache_segment(video_id, seg_index):key = f"dm:{video_id}:{seg_index}"if cache.exists(key):return cache.get(key)data = db.query_danmaku(video_id, seg_index)cache.set(key, serialize(data), ttl=86400)return data
For videos with sustained high traffic, background jobs can periodically refresh the cache to avoid jitter caused by mass expiration and rebuild.
4. Key Designs for Comment Scenarios
4.1 Two-Level Structure: Root Comments and Replies
Comments generally use a two-level model of “root comments + nested replies.” In database table design, root comments and replies can be merged into one table, with the hierarchy expressed through the root_id and parent_id fields:
CREATE TABLE comment (id BIGINT PRIMARY KEY,video_id BIGINT NOT NULL,root_id BIGINT DEFAULT 0,parent_id BIGINT DEFAULT 0,user_id BIGINT NOT NULL,content TEXT,status TINYINT,created_at DATETIME,KEY idx_video_root (video_id, root_id, created_at));
When querying, first paginate root comments, then batch-fetch the first few replies for each root comment. This avoids the performance problems caused by loading the full hierarchy at once.
4.2 Sorting and Pagination Strategy
There are two common comment sorting methods: by time and by popularity. Popularity is usually calculated as a weighted combination of likes, reply count, and a time decay factor. In engineering practice, it is recommended to:
- Use precomputed sorting results for default display pages to avoid real-time aggregation of statistical fields on every request.
- Use cursor-based pagination instead of offset-based pagination for deep pagination, preventing large-offset queries from overwhelming the database.
5. Content Moderation and Risk Control
The publishing pipeline for UGC content must embed a moderation step. A common approach is to mix “publish first, moderate later” and “moderate first, publish later” based on risk levels:
- Low-risk users publishing at normal frequency: display first and moderate asynchronously; if a violation is detected, automatically hide the content and notify the user.
- High-risk features (newly registered accounts, high-frequency publishing, sensitive-word candidates): enter synchronous moderation or delayed display.
- All content retains original records and moderation conclusions to support appeals and traceability.
At the same time, the API layer needs rate limiting and behavior verification to prevent machine-generated spam. Rate limiting can use token bucket thresholds set by both user and content dimensions.
6. Multi-Device Sync and Consistency
When users switch between multiple devices, they need to see consistent interaction records (publishing history, favorites, like status). Design points include:
- Read-time merge: User-state data is authoritative on the server. After the client fetches it, it merges and renders it with the local cache.
- Idempotent writes: Operations such as likes and favorites use unique keys for deduplication, ensuring that retries and duplicate requests do not produce dirty data.
- Eventual consistency: Cross-device push is distributed asynchronously through message queues. Short delays are allowed, but convergence is guaranteed.
7. Availability and Degradation
Degradation plans for interaction systems should be designed in advance:
- When the danmaku service is abnormal, video playback does not depend on danmaku. The danmaku entry can be disabled to protect the main playback path.
- When the counter service is delayed in updating, cached approximate values can be displayed and marked as “approx.”
- When the comment publishing pipeline is blocked, it can degrade to a “published successfully, pending moderation” prompt and place write requests into a queue for peak shaving.
The core principle is: interaction capabilities can be degraded, but main paths such as video playback and the account system must not be affected.
8. Summary
Danmaku and comment systems appear simple, but they concentrate classic distributed-system challenges such as high-concurrency writes, hot-data caching, UGC risk control, and multi-device consistency. The core design approach can be summarized as: partition data according to business characteristics (time-axis sharding and hierarchical structure), make non-critical paths asynchronous, cache hot content, and prepare degradation plans for every stage. On this basis, by combining general cloud capabilities such as object storage, message queues, and monitoring and alerting, an elastically scalable architecture can support interaction needs across all scenarios, from long-tail content to breakout hits.
