Notification System มีความสำคัญสำหรับแอปพลิเคชันสมัยใหม่ ให้การอัปเดตที่ทันเวลาเช่นการแจ้งเตือนผลิตภัณฑ์ เหตุการณ์ ข้อเสนอ และการแจ้งเตือน
บทนำ
Notification System มีความสำคัญสำหรับแอปพลิเคชันสมัยใหม่ การแจ้งเตือนสามารถส่งผ่าน:
- Push Notifications (Mobile หรือ Desktop)
- SMS Messages
- Emails
ข้อกำหนด
- Push Notifications: 10 ล้าน/วัน
- SMS: 1 ล้าน/วัน
- Emails: 5 ล้าน/วัน
- รองรับ Opt-out สำหรับประเภทการแจ้งเตือนเฉพาะ
ประเภท Notification
- iOS Push Notifications: ใช้ Apple Push Notification Service (APNS)
- Android Push Notifications: ใช้ Firebase Cloud Messaging (FCM)
- SMS Messages: บริการบุคคลที่สามเช่น Twilio หรือ Nexmo
- Emails: บริการอีเมลเช่น SendGrid หรือ Mailchimp
Components
1. Trigger Services
สร้าง Events เพื่อเริ่มต้นการแจ้งเตือน (เช่น การแจ้งเตือนบิล การอัปเดตการจัดส่ง)
2. Notification Server
- ให้ APIs สำหรับ Services ส่งการแจ้งเตือน
- ตรวจสอบ Emails และหมายเลขโทรศัพท์
- ดึงข้อมูลที่จำเป็นจาก Database หรือ Cache
3. Third-Party Services
ส่งการแจ้งเตือนไปยังผู้ใช้
ความท้าทายในการออกแบบ
- Single Point of Failure (SPOF): Notification Server ตัวหนึ่งล้มเหลวทำให้ระบบทั้งหมดล้มเหลว
- Scalability Issues: ยากที่จะขยาย Databases, Caches และ Processing Components อย่างอิสระ
- Performance Bottlenecks: ความต้องการทรัพยากรสูงสำหรับการส่งการแจ้งเตือน
การปรับปรุงการออกแบบ
- ย้าย Databases และ Caches ออกจาก Notification Server
- แนะนำ Horizontal Scaling ด้วย Notification Servers หลายตัว
- ใช้ Message Queues เพื่อแยก System Components
- เพิ่ม Workers ที่ดึง Notification Events จาก Message Queues และส่งไปยัง Third-Party Services
Reliability
ป้องกันการสูญเสียข้อมูล
เก็บข้อมูลการแจ้งเตือนใน Database และ Implement Retry Mechanism
Deduplication
ตรวจสอบ Event IDs เพื่อหลีกเลี่ยงการส่งการแจ้งเตือนซ้ำ
Components เพิ่มเติม
- Notification Templates: Templates ที่จัดรูปแบบไว้สำหรับความสม่ำเสมอ
- Notification Settings: ผู้ใช้สามารถ Opt-in/Opt-out สำหรับ Channels เฉพาะ
- Rate Limiting: จำกัดความถี่การแจ้งเตือนที่ส่งไปยังผู้ใช้
- Retry Mechanism:ลองส่งการแจ้งเตือนใหม่หาก Third-Party Services ล้มเหลว
- Event Tracking:รวบรวม Metrics เช่น Open Rate และ Click Rate
เนื้อหานี้อ้างอิงจาก ByteByteGo - System Design Interview - An Insider's Guide