Account Session Management and Cross-Device Progress Sync: An Engineering Guide
A practical guide to managing login sessions, synchronizing playback progress across devices, and enforcing permissions in content applications. Learn how token refresh, device sessions, conflict resolution, idempotent updates, security controls, and monitoring fit together while keeping authentication and business services separate.
1. Requirements and System Boundaries
Content applications often support web, mobile, desktop, and TV clients. Users expect their account identity and viewing history to be consistent across devices, and they expect to comment on or save content after signing in. These requirements involve three related subsystems:
- Identity and session management: Who is making the request?
- User activity synchronization: How does user state remain consistent across devices?
- Authorization: Is this user allowed to perform this action?
Separate these concerns before designing the system. They share a user identifier, but their consistency, latency, and security requirements differ. Combining them too tightly can cause authentication logic to spread into business code or frequent progress updates to overload the authentication service.
2. The Basic Session Model
Login sessions typically use two types of credentials:
- Access tokens accompany each request and have relatively short lifetimes, usually measured in minutes or hours.
- Refresh tokens are used only to obtain new access tokens. They have longer lifetimes and should be exchanged only between the client and the authentication service.
This limits the time an attacker can use a leaked access token.
Tokens should contain only necessary claims, such as the user identifier, issuance time, expiration time, and permission scope. Do not include sensitive information such as phone numbers, email addresses, or shipping addresses. Before trusting any claims, the server must verify the token’s signature and expiration.
A simplified refresh flow looks like this:
function request_with_token(request):token = storage.get_access_token()response = send(request, auth=token)if response.status == UNAUTHORIZED:new_token = refresh(storage.get_refresh_token())if new_token is None:redirect_to_login()returnstorage.save(new_token)return send(request, auth=new_token)return response
Prevent concurrent requests from triggering duplicate refresh operations. When several requests receive unauthorized responses at the same time, the client should perform one refresh while the other requests wait for its result.
Otherwise, refresh-token rotation may cause subsequent refresh attempts using the old token to fail, unexpectedly signing the user out.
3. Multi-Device Login Policies
Three common policies govern concurrent device sessions:
- Single-device access: Signing in on a new device immediately invalidates the previous device’s session. This is straightforward to implement and suits stricter account-security requirements, but offers a less convenient user experience.
- Independent concurrent sessions: Each device maintains its own session without affecting the others.
- Limited concurrent sessions: The account has a device limit. When the limit is exceeded, the user chooses which device to sign out.
Whatever policy you choose, maintain a server-side device session table. Each record should include at least:
- Session identifier
- User identifier
- Device type
- Last active time
- Session status
A device-management page can use this table to display signed-in devices and let users sign them out remotely. Signing out a device should revoke its refresh token as well as invalidate its session. Already-issued access tokens may remain valid until they expire.
The session table is generally read-heavy, making it suitable for acceleration through an in-memory cache, with persistent records stored in a relational database. If cached data becomes unavailable, use the database as the source of truth.
4. A Data Model for Playback Progress
Playback progress generates frequent writes. A client may report its position every few seconds while content is playing. Writing every report directly to the primary database can quickly increase write load.
Instead, clients should record progress locally and batch updates at fixed intervals or when playback pauses, the user switches content, or the client exits.
Use a unique key based on the user, content, and episode. A progress record should include:
- Current playback position
- Total duration
- Last update time
- Source device
- Version number
The version number helps resolve conflicts. Increment it with each write, and have the server accept only records with a higher version.
When users watch on multiple devices simultaneously, those devices may submit different positions. Define the merge policy in advance. Common options are:
- Use the most recently updated record.
- Use the furthest playback position.
- Retain all records and let the presentation layer decide which to show.
For viewing history, the most recently updated record is usually preferred because users generally expect to resume where they last stopped.
The following pseudocode illustrates version-based merging, with update time used to break ties:
function merge_progress(local, remote):if remote is None:return localif local.version > remote.version:return localif local.version < remote.version:return remotereturn local if local.updated_at >= remote.updated_at else remote
The reporting endpoint should be idempotent. Network retries may submit the same record more than once, and repeated submissions should not change the result. The server can deduplicate requests using a request identifier or use version numbers to reject stale updates.
5. Authorization for User Interactions
Commenting, liking, and saving content depend on authentication, but being signed in is not enough to authorize an action.
An account may be restricted from commenting, comments may be disabled for a particular item, or regional and age-related policies may affect visibility. Handle these checks consistently within the business service rather than scattering them across individual endpoints.
Organize authorization into three layers:
- Identity validation: Confirm that the token is valid.
- Account-state validation: Check whether the account is banned, subject to a cooldown, or otherwise restricted.
- Resource-rule validation: Check whether the content permits the requested action, such as commenting.
Each layer should return a distinct error code so the client can display an appropriate message.
6. Security Boundaries
Authentication and synchronization features are common attack targets. Basic safeguards include:
- Store passwords only as salted hashes.
- Rate-limit login attempts and require an additional verification challenge after repeated failures.
- Require additional verification before sensitive actions, such as changing a password or linking a new device.
- Use encrypted channels for all data transmission.
Never log tokens, passwords, or complete phone numbers. For troubleshooting, a hashed session identifier can help correlate requests without exposing the credential itself.
7. Observability and Resilience
Four metrics are especially useful:
- Login success rate
- Token refresh failure rate
- Progress-reporting latency
- Merge conflict count
A falling login success rate often indicates a problem with the authentication service or its storage dependencies. An unusual increase in conflicts may point to differences in client versions or clock skew.
The authentication service should support graceful degradation. If the cache is unavailable, fall back to database queries. Noncritical functions such as progress synchronization can use local buffering and delayed retries so playback itself remains unaffected.
8. Implementation Sequence
A practical implementation sequence is:
- Standardize the token model and refresh mechanism so login behavior is consistent across clients.
- Build the device session table and support viewing and remotely signing out devices.
- Define the progress data model, including versioning and merge rules, and implement idempotent reporting.
- Add layered authorization checks, monitoring, and alerts.
Session management, progress synchronization, and authorization share the same user identifier, but should be designed and evolved separately. Clear boundaries keep the impact of future changes manageable.
