High-Concurrency Video Platform Architecture: Login and Playback Practices

    This article explains how to translate video platform features into high-concurrency engineering problems and build a layered architecture around them. It covers stateless dual-token authentication, idempotent cross-device playback progress synchronization, separate designs for danmaku and comments, signed playback URLs with adaptive bitrate delivery, membership policy checks, event-driven messaging, and observability with capacity governance. The goal is to keep login smooth, playback clear, progress synchronized, and interactions real-time under traffic peaks.

    1. From Product Intent to Technical Problems

    A typical video platform homepage presents login/registration, HD playback, cross-device playback history, danmaku and comments, membership benefits, messages and activity feeds, and favorites side by side. These entries look like product features, but they map to a set of technical problems that combine high concurrency, strong consistency, and real-time requirements. Implementing them feature by feature without abstraction leads to authentication storms, inconsistent progress, message backlogs, and cache stampedes at peak traffic. The first step is to translate product language into engineering language:

    • Login and registration → identity authentication, session and token management, risk control and anti-abuse;
    • Free HD playback after login → access control, content delivery, adaptive quality, and anti-hotlinking;
    • Cross-device playback history → cross-device state synchronization, idempotent writes, and conflict resolution;
    • Danmaku and comments → real-time message distribution, ordering control, and content moderation;
    • Membership and benefits → entitlement model, billing state machine, and canary releases;
    • Messages, activity feeds, and favorites → event-driven design, asynchronous decoupling, and eventual consistency.

    Once this abstraction is done, the architecture has a shared coordinate system.

    2. Overall Layered Architecture

    A five-layer structure is recommended, with explicit contracts between layers to avoid cross-layer coupling:

    1. Access layer: domain routing, TLS termination, rate limiting, and canary traffic splitting;
    2. Application layer: authentication, content, interaction, entitlement, and messaging services;
    3. Domain service layer: aggregates such as playback progress, comment ordering, and favorite relationships as independent boundaries;
    4. Data layer: relational databases, caches, object storage, message queues, logs, and monitoring;
    5. Observability and governance layer: metrics, tracing, alerting, and capacity governance.

    The key principles are “separate reads from writes, tier hot and cold data, minimize synchronous paths.” Any path that can be made asynchronous (such as persisting watch history or distributing activity updates) stays out of the user’s critical path.

    3. Authentication and Session Management

    Login is the entry point for nearly every capability and must balance security and experience. A dual-token model with a short-lived access token and a long-lived refresh token is recommended; the refresh token is bound to a device fingerprint and supports revocation.

    1. # Example: dual-token issuance and verification (pseudocode)
    2. def login(user_id, device_id):
    3. access = jwt.sign({"sub": user_id, "dev": device_id}, ttl="15m")
    4. refresh = jwt.sign({"sub": user_id, "dev": device_id, "jti": uuid4()}, ttl="30d")
    5. store.bind_refresh(refresh, device_id) # supports per-device revocation
    6. return access, refresh
    7. def authorize(token):
    8. claims = jwt.verify(token) # return 401 on failure
    9. if revoked(claims["jti"]):
    10. raise Unauthorized()
    11. return claims["sub"]

    Engineering notes:

    • Token verification must be stateless to avoid a database lookup on every request;
    • Apply risk scoring and step-up verification for abnormally frequent or unfamiliar-location logins;
    • Logout and password changes must support bulk revocation so old tokens stop working.

    4. Cross-Device Playback Progress Synchronization

    Cross-device playback history is a classic eventual consistency scenario. Three things matter: idempotency, ordering, and conflict resolution.

    The recommended data model uses (user_id, content_id) as a unique key, with fields position_ms, updated_at, device_id, and revision. Writes use both a version number and a timestamp, and the server accepts only newer versions:

    1. UPDATE play_progress
    2. SET position_ms = :pos, updated_at = :ts, revision = revision + 1
    3. WHERE user_id = :uid AND content_id = :cid
    4. AND (:ts, :revision) > (updated_at, revision);

    Client strategy:

    • Report every 5–10 seconds during playback and on key events (pause, episode switch, exit);
    • Fetch progress before resuming; if the local copy is newer, treat local as authoritative and write it back;
    • On weak networks, persist locally first and batch-upload after reconnecting to avoid a request storm.

    5. Real-Time Interaction: Danmaku and Comments

    Danmaku is a real-time stream that is high-frequency, short-message, and tolerant of dropping a small amount; comments are a write stream that is low-frequency, strongly persistent, and requires moderation. The two must be designed separately.

    • Danmaku: the access layer maintains a long-lived connection gateway and broadcasts by room shard; the server keeps only the most recent N messages in a window and applies token-bucket rate limiting to prevent flooding from exhausting bandwidth;
    • Comments: written asynchronously through a message queue, entering a moderation queue first and the index only after approval, ensuring “safe before visible”;
    • Ordering: assign each message a monotonically increasing sequence number so clients can deduplicate and sort, avoiding out-of-order jitter.
    1. Client → long-lived gateway → room shard → broadcast queue → edge node → Client
    2. ↘ moderation queue → persistence → search index

    6. Content Delivery and Adaptive Quality

    “Free HD playback after login” is a balance between access control and delivery efficiency. Recommended practices:

    • Use time-limited signed playback URLs bound to the user and content to prevent hotlinking;
    • Use multi-bitrate segments with adaptive switching based on real-time bandwidth and buffer level;
    • Pre-warm popular content to edge caches and fetch cold content from object storage, forming hot and cold tiers;
    • Optimize first frame: preload the first segment, reuse connections, and shorten the redirect chain.

    Example logic:

    1. def issue_play_url(user_id, content_id, quality):
    2. token = sign({"uid": user_id, "cid": content_id, "q": quality}, ttl="10m")
    3. return f"https://cdn.example/{content_id}/{quality}/index.m3u8?auth={token}"

    (The example only conveys the signing and expiry idea; actual domains and paths depend on the deployment environment.)

    7. Membership Entitlements and Message Delivery

    Entitlements are a typical “state machine plus canary” problem. Abstract entitlement checks behind a policy interface so playback, downloads, and danmaku privileges all query through one entry point instead of scattering checks:

    1. def can_play(user_id, content_id):
    2. return policy.check(user_id, "play_hd", content_id)

    Messages and activity feeds are event-driven: content updates, interaction alerts, and system notifications produce events that a message queue distributes to in-app messages, push notifications, and activity feeds, ensuring “produced once, consumed in multiple places, without blocking each other.”

    8. Observability and Capacity Governance

    Without measurement there is no optimization. Cover at least four metric categories: authentication success rate and latency, playback stall rate and first-frame time, end-to-end message latency, and cache hit rate. Use tracing to locate cross-service bottlenecks and set tiered alerts on critical paths. For capacity governance, load-test the three peak types—login, playback start, and danmaku—separately, and define rate-limiting thresholds and degradation plans (such as disabling non-core activity updates or lowering danmaku frequency).

    9. Conclusion

    Translating product entries into technical problems, then implementing them with layered architecture, idempotent writes, asynchronous decoupling, and observability governance, is what keeps login smooth, playback clear, progress synchronized, and interaction real-time under traffic peaks. Building these layers on general capabilities such as object storage, message queues, container platforms, log services, and monitoring and alerting from Baidu AI Cloud can significantly reduce infrastructure overhead and let teams focus on domain models and experience optimization.