High-Concurrency Video Platforms: Multi-Device Synchronization and Real-Time Interaction
This article explains how to design playback history synchronization and high-concurrency danmaku systems for large-scale video platforms. It covers conflict resolution, idempotent writes, cache-first persistence, failure recovery, capacity planning, and the trade-offs involved in handling bursty real-time traffic.
As a video platform grows, playback history, danmaku, and activity feeds all face high-frequency writes at the same time. This article examines the design choices, failure-handling strategies, and capacity estimates for each module so that the patterns can be adapted to other systems.
1. Multi-Device Playback History Synchronization
The core challenge of cross-device resume is that multiple devices may write progress for the same piece of content. The system must define which write takes effect.
1.1 Data Model and Conflict Rules
Playback history uses user_id + content_id as its unique key. The main fields are:
| Field | Type | Description |
|---|---|---|
user_id |
bigint |
User ID |
content_id |
bigint |
Content ID |
position_ms |
int |
Playback position in milliseconds |
quality |
varchar(16) |
Video quality |
device_id |
varchar(64) |
Device that most recently reported progress |
device_seqs |
map |
Highest applied sequence number for each device |
version |
bigint |
Incremented by the server after each accepted write |
updated_at |
datetime(3) |
Time received by the server |
Conflict resolution should distinguish among three mechanisms:
- Last write wins (LWW): Retain the value with the most recent timestamp. Playback position is single-value data: the user’s most recent position is generally the correct answer, so LWW is appropriate. The timestamp must come from the server, because client clocks may be inaccurate.
- Version numbers: The server increments the version after accepting each write. A client includes the version it read when submitting an update. If the version no longer matches, the server rejects the write and requires the client to read the latest value again. This is suitable for data such as membership entitlements, where an incorrect overwrite is unacceptable.
- Vector clocks: These can determine whether two writes occurred concurrently and preserve conflicting copies for business-level merging. They may be useful for multi-field data such as favorites and preferences. For playback progress, concurrent writes only represent different resume positions, so keeping one value is usually sufficient and vector clocks are unnecessary.
The merge rules are as follows:
- Each device maintains an incrementing
device_seqand persists it locally. The server records the highest sequence number applied for each device. Reports with a sequence number less than or equal to that value are treated as duplicates or out-of-order messages and discarded.device_id + device_seqalso serves as the idempotency key. - Regular heartbeat reports, sent every 10 to 15 seconds, update the resume position using LWW.
- When a user actively drags the progress bar, the client sends
type=seek. The server accepts this update unconditionally because the user may intentionally move backward. - If a heartbeat moves backward by more than 30 seconds and comes from a different device than the current record, it is treated as stale and discarded.
- If multiple devices report progress within the same 60-second window, the system gives priority to the device marked as being in the foreground.
def apply_progress(record, report, server_now):# 1. Idempotency and ordering: a device's sequence cannot move backwardlast_seq = record.device_seqs.get(report.device_id, 0)if report.device_seq <= last_seq:return record, "duplicate_or_out_of_order"# 2. Cross-device stale heartbeat: do not allow a large rollbackif report.type == "heartbeat" and record.device_id != report.device_id:if report.position_ms < record.position_ms - 30_000:record.device_seqs[report.device_id] = report.device_seqreturn record, "stale_heartbeat"# 3. Apply all other reports using server-side receive time for LWWrecord.position_ms = report.position_msrecord.quality = report.qualityrecord.device_id = report.device_idrecord.device_seqs[report.device_id] = report.device_seqrecord.version += 1record.updated_at = server_now()return record, "applied"
1.2 Cache-First Writes and Asynchronous Persistence
The write path consists of four steps:
- When the gateway receives a report, it writes the data to a Redis Hash using
play:{user_id}as the key andcontent_idas the field. Each write refreshes the 30-day TTL. - At the same time, it adds
user_id:content_idto a dirty-data set implemented as a Redis ZSET. The score is the time when the record first became dirty. - Every 60 seconds, or when the dirty set reaches 500 entries, a persistence worker processes a batch. It performs an upsert using
user_id + content_idas the primary key and overwrites the database record only when the incoming version is greater than the stored version. - After a successful write, the worker removes the item from the dirty set only if the cache version still matches the version read when the batch was created. If the versions differ, a new write arrived during persistence and the item must be processed again in the next round.
Multiple reports for the same user and content within one batching window are merged, so the number of database writes is much lower than the number of incoming reports.
Failure recovery should cover the following scenarios:
- Redis primary-replica failover: With asynchronous replication, a failover may lose the most recent few seconds of writes. The client should retain the last successfully reported position locally and submit it again after reconnecting. The server then merges it according to the defined rules.
- Persistence failures: Retry batches with exponential backoff. After the retry threshold is exceeded, write the item to a dead-letter table and raise an alert. Once the issue is fixed, replay the failed records. Because the upsert is idempotent, repeated writes do not produce incorrect results.
- Periodic verification: Each day, sample 1% of users and compare the cache with the database. Raise an alert when the difference exceeds the defined threshold.
- Cache expiration: After the TTL expires, read the data from the database. Concurrent read-through requests for the same key should be merged to prevent a cache stampede.
The responsibilities of the three storage layers are:
- Cache: Stores the current resume position, handles frequent reads and writes, and serves online access.
- Relational database or sharded key-value store: Provides durable storage for the current state and supports queries by user.
- Object storage: Stores historical archives. Each day, export the previous day’s playback events as columnar files such as Parquet for analytics and auditing. Object storage should not be part of the online write path because frequent small-file writes are expensive and have unstable latency.
For a capacity estimate, assume 1 million concurrently online users, with each user reporting every 15 seconds. The write rate is approximately:
1,000,000 ÷ 15 ≈ 66,700 QPS
The Redis cluster should be planned for this rate with an additional onefold capacity buffer.
Latency figures must be described with their conditions. For same-region access with a cache hit, resume-position reads are typically in the millisecond range. Evaluation should focus on P99 rather than average latency, and load tests should separately measure network, gateway, and cache time.
2. High-Concurrency Danmaku Systems
Danmaku messages are characterized by bursty traffic, pronounced per-room hotspots, and strict latency requirements. During a popular live sporting event, a single room may produce tens of thousands of danmaku messages per second.
2.1 Message Queue Selection and Traffic Smoothing
Danmaku traffic is better suited to a log-based queue such as Kafka, or an equivalent service from a cloud provider, rather than a traditional task queue.
