From Page Skeleton to High Concurrency: Technical Architecture and Best Practices for Video Platform Danmaku Systems
A danmaku system may look like a simple text overlay, but supporting real-time interaction at scale requires coordinated solutions for traffic spikes, read amplification, timeline synchronization, storage, distribution, and client-side rendering. This article explains a layered architecture using long-lived connections, message queues, time-based sharding, multi-level caching, batched delivery, Canvas rendering, and operational safeguards.
1. Why Danmaku Systems Are Simple in Appearance but Complex in Practice
Danmaku—the scrolling comments displayed over video—is one of the most recognizable forms of real-time interaction on video platforms. On the surface, the feature appears to do little more than accept a text message and display it over the video. In practice, it involves message ingestion, validation, storage, distribution, and rendering.
When the number of online viewers reaches tens or even hundreds of thousands, a danmaku system must address three core challenges:
- Write spikes: When popular content goes live, message volume can surge rapidly. Writing directly to a database can put immediate and significant pressure on the database and its tables.
- Read amplification: Every viewer continuously receives danmaku updates. Read traffic can be dozens of times larger than write traffic, creating a typical read-fan-out problem.
- Real-time synchronization: Danmaku must align with the video timeline. Excessive latency causes comments to appear out of sync with the video and significantly harms the user experience.
A mature danmaku system must therefore solve three problems at the architectural level: high-concurrency writes, large-scale distribution, and timeline synchronization.
2. Layered Architecture
A typical danmaku system can be divided into four layers.
1. Ingestion Layer
The ingestion layer establishes and maintains client connections. Common approaches include long-lived connections such as WebSocket and long polling. Long-lived connections allow the server to push new danmaku messages to clients directly, avoiding the unnecessary requests caused by frequent polling.
After a connection is established, the ingestion layer also performs authentication and preliminary risk checks. For example, it can validate the user’s session and apply initial rate limits to sending operations, preventing obviously abnormal traffic from entering the rest of the system.
2. Message Processing Layer
After a danmaku message reaches the server, it passes through a unified validation pipeline:
def handle_danmaku(msg):if not check_auth(msg.user_id):return reject("UNAUTHORIZED")if not rate_limiter.allow(msg.user_id):return reject("RATE_LIMITED")if sensitive_filter.hit(msg.text):return reject("CONTENT_BLOCKED")enqueue(msg) # Send the message to the message queuereturn accept(msg)
A message queue is recommended for smoothing traffic spikes. When a write surge occurs, messages are placed in the queue first, and downstream persistence services consume them at a steady rate based on their processing capacity. This prevents the database from being overwhelmed and is a widely used practice for high-concurrency write workloads.
3. Storage Layer
Danmaku has a distinctive access pattern: it is read by video timeline segments and is rarely modified. Common storage designs include:
- Partitioning danmaku by video ID and time range, such as using one segment for every five seconds;
- Appending new messages to the corresponding segment, which naturally fits an append-oriented write model;
- Separating hot and cold data: danmaku for live or currently popular content is kept in cache, while historical content is archived in object storage or a database.
Time-based sharding is useful because when a client seeks to any position in the video, it only needs to retrieve the relevant segments. This keeps the read range small and improves cache efficiency.
4. Distribution Layer
The distribution layer addresses the read-fan-out problem. A common approach combines two strategies:
- Room-level aggregation: The server aggregates new danmaku from the same time window and pushes them in batches every one to two seconds, reducing the number of pushes and overall network overhead.
- Multi-level caching: Historical danmaku segments for popular videos are placed in a distributed cache. Cache hit rates can exceed 90 percent, substantially reducing pressure on the origin storage.
3. Key Details of Timeline Synchronization
The defining difference between danmaku and ordinary chat messages is that danmaku must align with playback progress. Several implementation details are important:
- Use playback time rather than message arrival time as the reference. When a client sends a danmaku message, it should include the current playback timestamp. The server then assigns the message to the appropriate time segment.
- Allow for errors at the scale of seconds. Network jitter can create a difference between the playback time and the message arrival time. The frontend can use smooth interpolation during rendering to prevent danmaku from appearing to jump.
- Rebuild context quickly after seeking. When a user drags the progress bar, the client should immediately request the segments around the new position and clear the current rendering queue.
4. Frontend Rendering Optimization
The primary performance bottleneck in danmaku rendering is often the DOM. When hundreds of comments appear on the screen simultaneously, frequent DOM operations can trigger repeated layout recalculation and repainting.
Common optimization techniques include:
- Using Canvas to render danmaku, moving the drawing workload away from the browser’s layout engine and toward bitmap rendering that is more suitable for GPU acceleration;
- Assigning danmaku to tracks to prevent messages in the same track from overlapping:
function allocateTrack(trackHeights, duration) {// Find a track that is not currently occupiedfor (let i = 0; i < trackHeights.length; i++) {if (trackHeights[i].freeAt <= performance.now()) {trackHeights[i].freeAt = performance.now() + duration;return i;}}return -1; // Drop or merge messages when all tracks are occupied}
- Using offscreen buffering to pre-render danmaku text into bitmaps, so that scrolling only requires bitmap translation and consumes less CPU.
5. Observability and Fault Tolerance
Deployment is only the beginning. A danmaku system also needs comprehensive operational safeguards:
- Monitor core metrics: Integrate push latency at P99, message submission success rate, cache hit rate, the number of long-lived connections, and other key metrics into monitoring and alerting systems.
- Prepare degradation strategies: When the push pipeline is under excessive pressure, the system can automatically fall back to periodic client-side retrieval of danmaku segments. This sacrifices some real-time performance in exchange for overall availability.
- Conduct capacity testing: Regularly load-test popular-content scenarios to verify the scaling capabilities of the message queue, cache, and long-lived connection gateway.
6. Conclusion
A danmaku system is a typical real-time interaction workload. Its architecture relies on several complementary techniques:
- Message queues absorb write spikes;
- Time-based sharding and multi-level caching reduce read amplification;
- Long-lived connections and batched pushes preserve real-time delivery;
- Canvas rendering and track allocation keep the frontend responsive.
These techniques operate independently while reinforcing one another, forming a complete technical loop from the backend to the client. The same architectural principles can also be applied to live-stream comments, online collaboration, real-time market data, and other high-concurrency interactive scenarios.
