- Primary Database: PostgreSQL (storing messages, users, metadata)
- PostgreSQL is generally better than MongoDB when handling entries in chat apps, such as users, msgs, chat rooms. PostgreSQL has cloud support in AWS RDS.
- Real-time Messaging: Kafka or Redis Pub-Sub (low-latency chat delivery)
- Kafka:
- Pro: High throughput, ensures that msgs are not lost even if consumers are offline, highly scalable, guarantees msg ordering.
- Con: Higher latency, more complex to setup and manage, might be overkill for simple apps.
- Redis Pub-Sub:
- Pro: Low latency, simpler to setup and use, fast and resource efficient, well-suited for realtime notifications and event.
- Con: Msgs will be lost if consumers are offline (solution: use Redis Lists to store unread messages temporarily.), limited scalability.
- Cache: Redis (caching recent messages and online users)
- Storing recent or frequently accessed messages in Redis reduces database load (caused by querying) and improves retrieval speed (impact on fetching).
- Redis Pub-Sub also allows real-time message broadcasting across servers.
- Use TTL (Time-To-Live) to expire old messages (say only cache 100 latest msgs per chatroom).
- Redis also handles Distributed Session Management, where it stores session tokens, authentication details, and active chat states (since users may connect from multiple devices).
- Use fallback to query from main database (e.g. PostgreSQL) if requested data is not cached.
- Search: Elasticsearch (full-text message search)
- Full-text msg search: Searching for a particular msg with supports of fuzzy matching, auto-suggestions, synonyms, and filters. (not quite useful in this project)
- Logging and monitoring: Storing and analyzing system logs, error logs, and performance metrics. Detecting unauthorized access (e.g. suspicious login attempts based on unusual activity), monitoring network traffic.
- Real-Time Data Analytics: Can analyze millions of records in seconds. (not quite useful in this project)
- Autocomplete & Recommendation Systems: Supports prefix matching and phrase suggestions.
- Might be an overkill for this project, if features like fast msg search, autocomplete words, real-time analytics, etc. are not needed.
- Key Management: AWS KMS or HashiCorp Vault (managing encryption keys securely)
- AWS KMS: Requires money for keys, key rotations, API requests.
- HashiCorp Vault: Requires money for Enterprise edition.
- File Storage: Amazon S3 (for media file storage)
- Storing large file in SQL databases is inefficient.
- PostgreSQL/MongoDB → Users & messages.
- Redis → Message caching.
- Elasticsearch → Search.
- Amazon S3 → File storage.
