접속 상태 및 온라인 상태 추적
Redis 집합, TTL, Pub/Sub 하트비트를 사용해 어떤 사용자가 온라인인지 추적하는 실시간 접속 상태 시스템을 구축해 보세요.
접속 상태 및 온라인 상태 추적은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What Is Presence?
Presence is the real-time indication of whether a user is online, away, or offline. Chat apps, collaborative editors, and games all rely on it. Redis is ideal because presence data is ephemeral and high-churn.
The Naive Approach
A first idea: store a flag per user. The problem is detecting when someone disconnects ungracefully (closed laptop, lost network). A stale flag would mark them online forever.
SET presence:user:42 onlineHeartbeats with TTL
The robust pattern uses heartbeats. The client refreshes a key with a short TTL every few seconds. If heartbeats stop, the key expires and the user is considered offline.
SET presence:user:42 1 EX 15
# client re-issues this every 10 secondsListing Online Users
To list everyone online, keep a set of active users and check membership, or scan presence keys. A sorted set keyed by last-seen timestamp is efficient.
ZADD online 1716900000 user:42
ZADD online 1716900005 user:7Sorted Set by Timestamp
Storing last-seen as the score lets you both list members and prune the stale ones in one query.
ZADD online <now> user:idon each heartbeatZREMRANGEBYSCORE online -inf <cutoff>removes timed-out users
ZADD online 1716900010 user:42
ZREMRANGEBYSCORE online -inf 1716899980Broadcasting Status Changes
When a user comes online or drops off, publish an event so other clients update their UI immediately.
PUBLISH presence.events "user:42 online"
SUBSCRIBE presence.eventsDetecting Going Offline
Combine the sorted set prune with keyspace expiry. When a presence key expires, a keyevent fires, and a worker publishes a went-offline event to subscribers.
CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expiredPer-Room Presence
In a chat app you often need presence per room. Use one set per room and add or remove the user as they join or leave.
SADD room:general:online user:42
SREM room:general:online user:42
SMEMBERS room:general:onlineMulti-Device Presence
A user may be online from several devices. Count active connections so the user only goes offline when the last device disconnects.
INCR conns:user:42
DECR conns:user:42
GET conns:user:42Putting It Together
A complete presence system: heartbeat TTL keys, a last-seen sorted set, connection counters for multi-device, and Pub/Sub events to push changes to clients in real time.
Tuning the Intervals
Choose a heartbeat interval shorter than the TTL (for example heartbeat every 10s, TTL 15s) so a single missed beat does not flap a user offline. Shorter intervals mean faster detection but more traffic.
Quick Check
Test your understanding of presence design.
Recap
You built a real-time presence system using heartbeat TTL keys, a last-seen sorted set with score-based pruning, per-room presence sets, multi-device connection counters, and Pub/Sub broadcasts for instant status updates. Tune heartbeat and TTL intervals to balance detection speed against traffic.
자주 묻는 질문
“접속 상태 및 온라인 상태 추적” 강의는 무료인가요?
네 — “접속 상태 및 온라인 상태 추적” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.
“접속 상태 및 온라인 상태 추적”에서 뭘 배우나요?
Redis 집합, TTL, Pub/Sub 하트비트를 사용해 어떤 사용자가 온라인인지 추적하는 실시간 접속 상태 시스템을 구축해 보세요. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“접속 상태 및 온라인 상태 추적” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 패턴 일치 구독
- 실시간 채팅 설계
- 이벤트 기반 아키텍처
- 접속 상태 및 온라인 상태 추적