Multi-Device Streaming Architecture: Login Sessions, Playback Sync, and Danmaku at Scale
A practical guide to designing cross-device authentication, reliable playback progress synchronization, and high-concurrency danmaku delivery with Java, Redis, Kafka, and MySQL. It covers session validation, conflict resolution, cache consistency, rate limiting, message ordering, and failure handling. The patterns can be adapted to other technology stacks.
Users often switch between phones, computers, tablets, and TVs. Their login state should remain consistent, a movie paused on one device should resume on another, and danmaku during popular moments should remain stable under heavy traffic. This article examines the design choices, implementation details, and failure-handling strategies for these three problems. The examples use Java, Redis, and Kafka, but the underlying approaches apply to other technology stacks.
1. Designing Cross-Device Login Sessions
Session-Based Authentication vs. Pure JWT
| Dimension | Server-Side Session | Pure JWT | JWT with a Server-Side Session Table |
|---|---|---|---|
| State storage | Server memory or a cache | None | A session for each device stored in Redis |
| Cross-service validation | Requires shared storage access | Local signature verification is sufficient | Requires Redis access |
| Forced logout | Delete the session | Cannot be revoked before expiration | Delete the device field or increase the version |
| Suitable scenarios | Single-device web applications | Read-heavy systems that can tolerate residual validity until expiration | Multi-device applications that need session revocation |
Pure JWT validation is fast, but a token cannot be actively revoked after issuance. Multi-device platforms typically set a relatively short token lifetime, such as 15 minutes, and use server-side device sessions for the final authorization decision.
Organizing the Session Data
Each user has one Redis Hash with the key auth. The field is
{userId}deviceId, and the value is sessionId|tokenVersion.
When a device logs in again, its existing field is overwritten and tokenVersion is incremented. The old token then becomes invalid because its version no longer matches. A phone and a computer use different deviceId values and can therefore remain logged in at the same time.
The platform can also limit the number of simultaneously active devices for each device category. This rule should be checked before the login endpoint writes the new session.
Token Validation
// Dependencies: io.jsonwebtoken:jjwt-api, jjwt-impl, jjwt-jackson, version 0.11.5// Read secretKey from a configuration service or environment variable; do not hard-code it.public boolean validateToken(String token, String deviceId) {if (deviceId == null) {return false;}Claims claims;try {claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody();} catch (JwtException | IllegalArgumentException e) {return false; // Invalid signature, expired token, or malformed token}String userId = claims.getSubject();String sessionId = claims.get("sid", String.class);Integer tokenVersion = claims.get("ver", Integer.class);if (userId == null || sessionId == null || tokenVersion == null) {return false;}String key = "auth:sessions:" + userId;String raw = (String) redisTemplate.opsForHash().get(key, deviceId);if (raw == null) {// On a cache miss, load the session from the database and backfill Redis.// Reject the request if no session is found.// loadSessionFromDbAndBackfill is an illustrative method and must be// implemented according to the application's session table.raw = loadSessionFromDbAndBackfill(userId, deviceId);if (raw == null) {return false;}}String[] parts = raw.split("\\|", 2);return parts.length == 2&& parts[0].equals(sessionId)&& parts[1].equals(String.valueOf(tokenVersion));}
Several details are important:
- Cache misses: Do not allow a request simply because Redis has no matching field. First query the session table. If a record exists, backfill Redis; otherwise, reject the request.
- Forced logout: Delete the field for the device or increment its
tokenVersion. The old token will fail the next validation. - Revoking an individual token: Add a unique
jticlaim when issuing the token. When the token must be revoked, place itsjtion a blacklist. The blacklist TTL should match the token’s remaining lifetime. - Expiration: Keep the Hash TTL aligned with the refresh token’s lifetime and extend it whenever the token is refreshed.
lastActivedoes not need to be written on every request; updating it once every five minutes is usually sufficient to reduce Redis write pressure. - Redis outages: Sensitive endpoints, such as password changes and payments, should reject requests when Redis is unavailable. Read-only endpoints may relax this policy according to business risk, but only for a limited period.
2. Synchronizing Playback Progress Without Conflicts
Data Model
The core progress record answers the question: where did a user stop watching a particular video?
The primary table therefore uses (user_id, video_id) as a composite primary key and stores only the latest record. Relevant fields include position_ms, duration_ms, device_id, and received_at.
If viewing analytics are required, use a separate append-only progress event stream partitioned by user and date. The resume position should be read from the primary table.
Reporting Frequency and Deduplication
During playback, the client reports progress every 15 seconds. It also sends an extra report when the user pauses, seeks, or exits. Multiple reports for the same video within a 15-second window are merged into one request.
When the device is offline, the client keeps only the latest local progress record. Once connectivity returns, it uploads that record instead of replaying all intermediate history.
Each request includes device_id and a monotonically increasing device_seq. The server uses SET NX to record processed device_id:device_seq pairs, with a TTL of 10 minutes. Duplicate requests return success immediately and do not write to the database again.
Conflict Resolution Across Devices
Multiple devices may update the same progress record at the same time. This design uses last-write-wins based on the time when the gateway receives the request, stored as received_at. Client timestamps are not used because device clocks may differ and are not reliable.
INSERT INTO watch_progress(user_id, video_id, position_ms, duration_ms, device_id, received_at)VALUES (?, ?, ?, ?, ?, ?)ON DUPLICATE KEY UPDATEposition_ms = IF(VALUES(received_at) > received_at, VALUES(position_ms), position_ms),device_id = IF(VALUES(received_at) > received_at, VALUES(device_id), device_id),received_at = GREATEST(received_at, VALUES(received_at));
MySQL evaluates assignments in ON DUPLICATE KEY UPDATE from left to right. For this reason, received_at must be updated last. The comparisons in the first two assignments still use the old value.
This rule has a trade-off: when two devices play the same video simultaneously, the progress may jump back and forth. Whether this is acceptable is a product decision. If it is not, the system can use the greater progress value instead, but only within a short time window. Otherwise, a user’s intentional seek backward could be incorrectly overwritten.
Read Path and Cache Consistency
Reads first check progress:{userId}:{videoId} in Redis. On a cache miss, the service queries the database and backfills the cache.
For writes, update the database first, delete the cache, and delete it again after 500 milliseconds. This delayed double deletion reduces the chance that a concurrent read will write an outdated value back into the cache.
For records that do not exist in the database, cache the empty result for 60 seconds to prevent cache penetration. Progress keys are distributed by user and normally do not create hot keys. However, if a video-level statistics key receives requests from many users at once, it requires separate rate limiting or a local cache.
Failure Handling
When Kafka publishing fails, the client or gateway should retry locally with exponential backoff and a maximum retry count. After the limit is reached, write the message to a local disk queue and send it again after the service recovers.
The player should also keep the current progress locally. A failed progress write must not block playback or display an error message to the user.
3. Handling High-Concurrency Danmaku Traffic
Partitioning and Ordering
Use videoId as the Kafka topic partition key so that danmaku for the same video is placed in the same partition and maintains its order.
Use storage keys in the form danmaku:{videoId}:{bucket}, where bucket identifies the 10-second interval on the video’s timeline. When viewers seek backward or forward, the service can retrieve danmaku directly by time bucket.
Ingress Rate Limiting and Sampling
Apply token-bucket rate limiting per user. The allowed messages per second should be determined by product requirements.
When instantaneous traffic for a video exceeds its threshold, the broadcast layer sends only a sampled subset of danmaku. All messages are still written to storage, so the underlying data is not lost; viewers simply see a partial stream during the peak.
Idempotent Consumption
Kafka provides at-least-once delivery by default, so the same message may be consumed more than once. Each danmaku message should include a msgId, generated either as a UUID by the client or as a Snowflake ID by the server.
Before consuming the message, execute SET … to record the message identifier and use that record to prevent duplicate processing.
