
SSE, WebSocket, pushSSE, WebSockets, push
200.000 tab đang mở200,000 open tabs.Tối thứ sáu, 200.000 khách đang mở web theo dõi đơn hàng, bình luận và khuyến mãi. Cao điểm có khoảng 1.500 thông báo mỗi giây cần tới tab trong dưới 1 giây.On Friday evening 200,000 shoppers have the site open, following orders, comments and deals. At peak about 1,500 notifications per second must reach a tab in under 1 second.
Hãy theo khách u-42 đang mở tab theo dõi đơn #5521 lúc 19:30, đúng lúc đơn được giao cho shipper: Order Service phát sự kiện lên Kafka, Notification Service lưu thông báo vào Postgres trước rồi mới publish vào kênh Redis user:{id}. Chỉ node đang giữ luồng SSE của khách nhận tin và ghi xuống trình duyệt, nên các node scale ngang mà không cần sticky session. Chạy luồng trực tiếp trước, rồi thử “Tab kết nối lại” để thấy phát lại theo Last-Event-ID và “User offline” để thấy thông báo chuyển sang push điện thoại.Follow shopper u-42, who has a tab open tracking order #5521 at 19:30, just as it is handed to the courier: the Order Service emits an event to Kafka, and the Notification Service stores the notification in Postgres first and only then publishes to the Redis channel user:{id}. Only the node holding the shopper's SSE stream receives it and writes it to the browser, so nodes scale out without sticky sessions. Play the live flow first, then try “Tab reconnects” to see replay by Last-Event-ID and “User offline” to see the notification switch to a mobile push.
119:30, đơn #5521 chuyển sang SHIPPED. Order Service chỉ phát sự kiện với key là user_id để giữ thứ tự theo người dùng; nó không cần biết ai đang online hay mở mấy tab.At 19:30 order #5521 moves to SHIPPED. The Order Service only emits an event keyed by user_id to keep per-user order; it does not need to know who is online or how many tabs are open.
Luồng nằm ở một trong nhiều stream node. Node SUBSCRIBE user:{id} khi luồng mở và UNSUBSCRIBE khi đóng; Notification Service chỉ cần PUBLISH. Trên Redis Cluster 7 dùng sharded pub/sub (SSUBSCRIBE/SPUBLISH) để không broadcast qua mọi shard.The stream lives on one of many stream nodes. A node SUBSCRIBEs to user:{id} when a stream opens and UNSUBSCRIBEs when it closes; the Notification Service only PUBLISHes. On Redis Cluster 7 use sharded pub/sub (SSUBSCRIBE/SPUBLISH) so messages are not broadcast across every shard.
Redis Pub/Sub không lưu tin. Lưu thông báo vào DB trước khi publish, gửi id tăng dần trong trường id của SSE, và khi kết nối lại thì đọc id > Last-Event-ID. Subscribe trước rồi mới phát lại, bỏ qua id đã gửi.Redis Pub/Sub keeps nothing. Store the notification before publishing, send an increasing id in the SSE id field, and on reconnect read id > Last-Event-ID. Subscribe first, then replay, and skip ids already sent.
ALB mặc định idle timeout 60 giây, nginx proxy_read_timeout 60 giây. Gửi comment “: ping” mỗi 15 giây, tăng timeout, tắt buffering (X-Accel-Buffering: no) và đặt retry: 3000 để client tự nối lại.An ALB defaults to a 60-second idle timeout and nginx to proxy_read_timeout 60s. Send a “: ping” comment every 15 seconds, raise the timeouts, disable buffering (X-Accel-Buffering: no) and set retry: 3000 so clients reconnect on their own.
EventSource gốc không gửi được header Authorization. Dùng cookie HttpOnly cùng site, hoặc polyfill fetch-event-source để gửi Bearer token; không đặt token trong query string vì nó vào access log. Token hết hạn thì đóng luồng, client làm mới token rồi nối lại.Native EventSource cannot send an Authorization header. Use a same-site HttpOnly cookie, or a fetch-event-source polyfill to send a Bearer token; never put the token in the query string, where it lands in access logs. When the token expires close the stream; the client refreshes it and reconnects.
Stream node giữ key online:{id} với TTL 90 giây, gia hạn mỗi heartbeat. Không có key thì đẩy vào hàng push; worker pool gọi FCM/APNs, retry 429/5xx với backoff và xoá token khi nhận UNREGISTERED.Stream nodes keep online:{id} with a 90-second TTL, refreshed on every heartbeat. With no key the notification goes to the push queue; a worker pool calls FCM/APNs, retries 429/5xx with backoff and deletes tokens that come back UNREGISTERED.
Đếm từ cột read_at với index một phần WHERE read_at IS NULL, hoặc giữ bộ đếm trong Redis và cập nhật cùng lúc. Đánh dấu đã đọc cũng publish một sự kiện để các tab khác giảm số.Count from read_at with a partial index WHERE read_at IS NULL, or keep a Redis counter updated alongside. Mark as read also publishes an event so other tabs drop their count.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
notification | id (bigserial), user_id, type, title, body, link, source_event_id, created_at, read_at | unique(user_id, source_event_id); index (user_id, id) cho phát lại; partial index (user_id) WHERE read_at IS NULL cho số chưa đọcunique(user_id, source_event_id); index (user_id, id) for replay; partial index (user_id) WHERE read_at IS NULL for the unread count |
online:{user_id} (Redis) | value = node_id, TTL 90s | Stream node gia hạn mỗi heartbeat 15–30 giây; hết hạn nghĩa là gửi pushRefreshed by the stream node on every 15–30-second heartbeat; expiry means send a push |
user:{user_id} (Redis channel) | payload = { id, type, title, link } | Chỉ để đánh thức luồng đang mở; nguồn sự thật là bảng notificationOnly wakes open streams; the notification table is the source of truth |
device_token | user_id, platform, token, last_seen_at | Một user nhiều thiết bị; xoá token khi FCM trả UNREGISTEREDMany devices per user; delete a token when FCM answers UNREGISTERED |
NotificationStreamController (Java) và Server.Stream (Go) là hộp Stream Node (SSE) ở sơ đồ: đọc Last-Event-ID, phát lại thông báo đã lỡ từ hộp Postgres, sau đó nhận sự kiện trực tiếp từ kênh user:{id} của hộp Redis. heartbeat() (Go: nhánh ping) gửi ping 15 giây để hộp Load Balancer không cắt kết nối và gia hạn online:{id}.NotificationStreamController (Java) and Server.Stream (Go) are the Stream Node (SSE) box in the diagram: they read Last-Event-ID, replay missed notifications from the Postgres box, then stream live events from the user:{id} channel in the Redis box. heartbeat() (Go: the ping branch) sends a ping every 15 seconds so the Load Balancer box keeps the connection open, and refreshes online:{id}.
package com.example.notify;
import java.io.IOException;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@RestController
@RequiredArgsConstructor
public class NotificationStreamController {
private final NotificationRepository repo;
private final EmitterRegistry emitters; // SUBSCRIBE user:{id} on first emitter
@GetMapping(path = "/notifications/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter stream(@AuthenticationPrincipal Jwt jwt,
@RequestHeader(value = "Last-Event-ID", required = false) Long lastId) {
String userId = jwt.getSubject();
SseEmitter emitter = new SseEmitter(Duration.ofMinutes(30).toMillis());
emitters.add(userId, emitter); // live events from now on
emitter.onCompletion(() -> emitters.remove(userId, emitter));
emitter.onTimeout(emitter::complete);
if (lastId != null) {
repo.findTop100ByUserIdAndIdGreaterThanOrderById(userId, lastId)
.forEach(n -> send(emitter, n)); // replay what was missed
}
return emitter;
}
@Scheduled(fixedRate = 15_000)
public void heartbeat() {
emitters.forEach((userId, e) -> {
try {
e.send(SseEmitter.event().comment("ping"));
emitters.touchOnline(userId, Duration.ofSeconds(90));
} catch (IOException ex) {
emitters.remove(userId, e);
}
});
}
static void send(SseEmitter e, Notification n) {
try {
e.send(SseEmitter.event()
.id(Long.toString(n.getId()))
.name("notification")
.data(n.toDto())
.reconnectTime(3_000));
} catch (IOException ex) {
e.completeWithError(ex);
}
}
}package main
import (
"fmt"
"io"
"net/http"
"strconv"
"time"
"example.com/notify/auth"
)
func (s *Server) Stream(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
userID := auth.UserID(ctx) // set by the JWT middleware
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("X-Accel-Buffering", "no") // nginx: do not buffer
sub := s.redis.Subscribe(ctx, "user:"+userID)
defer sub.Close()
if _, err := sub.Receive(ctx); err != nil { // wait for the SUBSCRIBE ack
return
}
live := sub.Channel()
lastID, _ := strconv.ParseInt(r.Header.Get("Last-Event-ID"), 10, 64)
if lastID > 0 { // reconnect: replay what was missed
missed, _ := s.store.After(ctx, userID, lastID, 100)
for _, n := range missed {
writeEvent(w, n)
lastID = n.ID
}
}
flusher.Flush()
ping := time.NewTicker(15 * time.Second)
defer ping.Stop()
for {
select {
case msg := <-live:
n := decode(msg.Payload)
if n.ID <= lastID {
continue // already sent during replay
}
writeEvent(w, n)
lastID = n.ID
case <-ping.C:
fmt.Fprint(w, ": ping\n\n")
s.presence.Touch(ctx, userID, 90*time.Second) // SET online:{u} EX 90
case <-ctx.Done():
return // tab closed or the client went away
}
flusher.Flush()
}
}
func writeEvent(w io.Writer, n Notification) {
fmt.Fprintf(w, "id: %d\nevent: notification\ndata: %s\n\n", n.ID, n.JSON)
}| SSESSE | WebSocketWebSocket | |
|---|---|---|
| Chiều dữ liệuDirection | Server → client; client gọi REST khi cần ghiServer to client; the client uses REST to write | Hai chiều trên một kết nốiBoth ways on one connection |
| Nối lại, phát lạiReconnect and resume | Có sẵn: EventSource tự retry và gửi Last-Event-IDBuilt in: EventSource retries and sends Last-Event-ID | Tự viết: backoff, gửi id cuối, phát lạiWrite it yourself: backoff, send the last id, replay |
| Hạ tầngInfrastructure | HTTP thường, qua mọi proxy; HTTP/1.1 giới hạn 6 kết nối mỗi domain, HTTP/2 thì ghép kênhPlain HTTP through any proxy; HTTP/1.1 allows 6 connections per domain, HTTP/2 multiplexes | Cần Upgrade được LB và proxy hỗ trợ; không có giới hạn 6 kết nốiNeeds Upgrade support in the LB and proxies; no 6-connection limit |
| Chọn khiPick it when | Chỉ đẩy thông báo, cập nhật trạng tháiYou only push notifications and status updates | Đã có chat, cộng tác hoặc cần độ trễ hai chiều thấpYou already have chat or collaboration, or need low-latency both ways |
ALB và nginx mặc định đóng kết nối không có dữ liệu sau 60 giây, nên người dùng ít thông báo bị nối lại liên tục và tạo bão reconnect. Gửi comment “: ping” mỗi 15 giây, đặt proxy_buffering off hoặc X-Accel-Buffering: no, tăng idle timeout và theo dõi tỉ lệ reconnect mỗi phút theo node.ALB and nginx close idle connections after 60 seconds by default, so users with few notifications reconnect constantly and cause reconnect storms. Send a “: ping” comment every 15 seconds, set proxy_buffering off or X-Accel-Buffering: no, raise the idle timeout and track reconnects per minute per node.
Tắt một stream node giữ 20.000 luồng khiến tất cả nối lại sau đúng 3 giây và cùng đọc DB để phát lại, làm Postgres tăng vọt. Rút node từ từ trong vài phút, gửi retry ngẫu nhiên 2-10 giây trước khi đóng, và giới hạn phát lại 100 tin với index (user_id, id).Stopping a stream node holding 20,000 streams makes them all reconnect after exactly 3 seconds and hit the DB for replay together, spiking Postgres. Drain nodes over a few minutes, send a randomized retry of 2-10 seconds before closing, and cap replay at 100 messages with an (user_id, id) index.
Publish rồi mới INSERT nghĩa là tab có thể nhận id chưa tồn tại trong DB, và nếu INSERT lỗi thì lần phát lại sau sẽ thiếu tin. Luôn commit thông báo trước, publish sau, và dùng unique (user_id, source_event_id) để Kafka giao lại không tạo bản trùng.Publishing before INSERT means a tab can receive an id that does not exist in the DB yet, and if the INSERT fails a later replay misses it. Always commit the notification first, publish second, and use unique (user_id, source_event_id) so Kafka redeliveries never create duplicates.
Vì EventSource không gửi được header, nhiều đội đặt ?access_token=... trên URL, và token đó nằm luôn trong access log của LB lẫn proxy. Dùng cookie HttpOnly SameSite cùng domain hoặc fetch-event-source với header Authorization, và đóng luồng khi token hết hạn để client làm mới rồi nối lại.Because EventSource cannot send headers, many teams put ?access_token=... on the URL, and the token ends up in LB and proxy access logs. Use a same-domain HttpOnly SameSite cookie or fetch-event-source with an Authorization header, and close the stream when the token expires so the client refreshes and reconnects.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
SSE trong đa số trường hợp: một chiều, HTTP thường, có sẵn retry và Last-Event-ID. WebSocket khi đã có kênh hai chiều (chat). Long polling là phương án dự phòng cho proxy chặn streaming, đổi lại mỗi sự kiện tốn một request mới và độ trễ cao hơn.SSE in most cases: one-way, plain HTTP, with retry and Last-Event-ID built in. WebSocket when a two-way channel (chat) already exists. Long polling is the fallback for proxies that block streaming, at the cost of a new request per event and higher latency.
Pub/Sub không lưu gì: tab đang nối lại sẽ lỡ sự kiện. DB cho id tăng dần để phát lại, cho danh sách chuông và số chưa đọc. Publish chỉ là lời đánh thức.Pub/Sub stores nothing, so a reconnecting tab would miss the event. The DB gives an increasing id for replay, the bell list and the unread count. The publish is only a wake-up call.
50k client nối lại cùng giây và cùng truy vấn phát lại. Gửi retry ngẫu nhiên 2–10 giây trước khi tắt node, drain từ từ, giới hạn phát lại 100 bản ghi và dùng index (user_id, id).50k clients reconnect in the same second and all run the replay query. Send a randomised retry of 2–10 seconds before shutting the node down, drain gradually, cap replay at 100 rows and use an (user_id, id) index.
Cookie HttpOnly SameSite cùng domain là cách gọn nhất. Nếu bắt buộc Bearer token thì dùng fetch-event-source. Tránh token trong URL; nếu buộc phải dùng thì là ticket ngắn hạn một lần, đổi từ API trước khi mở luồng.A same-domain HttpOnly SameSite cookie is the simplest. If a Bearer token is required, use fetch-event-source. Avoid tokens in the URL; if you must, use a short-lived one-time ticket fetched from the API just before opening the stream.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.