How do you implement a feature that forcibly logs out a specific user across all of their active sessions and devices?
Short answer
You maintain a set that tracks all the active session identifiers associated with a specific user, and when you need to force a logout across all their devices, you retrieve that set and delete every individual session key it references, effectively invalidating all of that…
Advertisement
AModel answer
You maintain a set that tracks all the active session identifiers associated with a specific user, and when you need to force a logout across all their devices, you retrieve that set and delete every individual session key it references, effectively invalidating all of that user's active sessions at once.
Example
SADD user_sessions:42 'session:abc123' 'session:xyz789' -- To force logout everywhere: DEL session:abc123 session:xyz789 DEL user_sessions:42
Real-world example
A security feature lets a user click a button to log out of all their devices at once, using a tracked set of their active session keys to efficiently invalidate every single one simultaneously.
Likely follow-ups
- How do you keep this tracking set properly updated as sessions naturally expire on their own?
- What happens if a session key expires naturally but is still referenced in the tracking set?
Related topics