Notification Realtime
Dạ, để thiết kế hệ thống notification realtime, em triển khai theo kiến trúc sau:
1. Mục tiêu:
Gửi thông báo cho người dùng ngay lập tức (popup/bell), ví dụ: có đơn hàng mới, tin nhắn mới...
2. Thành phần chính:
🧩 Notification Service (tách riêng service):
Nhận yêu cầu gửi noti từ các service khác (qua REST hoặc message queue).
Lưu noti vào DB (để user xem lại lịch sử).
Gửi noti realtime đến client.
3. Cơ chế realtime:
✅ WebSocket (hoặc Socket.IO, nếu frontend dùng JS):
Client kết nối WebSocket sau khi đăng nhập.
Server gửi noti trực tiếp qua socket theo userId.
✅ Message Broker (Redis Pub/Sub hoặc Kafka):
Các service publish noti → Notification Service nhận và xử lý.
4. DB Design (MySQL/Mongo):
notification
table:
id
BIGINT
Khóa chính
user_id
BIGINT
Người nhận
title
VARCHAR
Tiêu đề
content
TEXT
Nội dung
type
ENUM
Loại (order, chat,...)
is_read
BOOLEAN
Đã đọc chưa
created_at
TIMESTAMP
Ngày tạo
5. Flow tổng quát:
Service khác gửi noti qua Kafka/Redis hoặc REST API.
Notification Service lưu DB → gửi realtime qua WebSocket.
Client frontend nhận và hiển thị ngay (bell icon, toast...).
Khi người dùng mở xem → call API đánh dấu
is_read = true
.
6. Tối ưu & Mở rộng:
Dùng Redis lưu socket session mapping (userId → socketId).
Dùng Push Notification (FCM, OneSignal) cho mobile app khi offline.
Scale Notification Service theo chiều ngang.
Thiết kế này đảm bảo realtime, phân tán tốt, tách biệt rõ ràng và dễ mở rộng.
Last updated