- Client → Load Balancer → Stateless App Server → Cache → Database(s)
- All user login sessions are stored centrally in Redis. Centralized sessions let any app server authenticate a user without sticky sessions, enabling seamless scaling and server replacement.
Store sessions in a dedicated Redis cluster configured with persistence (RDB/AOF), eviction policies, and clustering to avoid session loss and bottlenecks. For very high security requirements, couple Redis session data with short TTLs and server-side session validation.
- For frequently accessed (“hot”) data—trending photos, popular profiles, or feed fragments—the application checks the cache (Redis or a dedicated cache layer) before querying the primary database.
- Cache synchronization with Postgres uses TTLs and explicit invalidation when data changes. Common patterns:
- Write-through: write to cache and DB synchronously for strong consistency.
- Write-back: write to cache first and flush to DB later for high write throughput.
- Explicit invalidation: update DB then delete/refresh cache on change.
- Structured entities—users, photos, likes, comments, follows—are stored in Postgres with indexed columns for common queries.
- To scale read throughput, we run one primary that accepts all writes and two read replicas for read traffic. A read load balancer distributes queries to replicas (and optionally the primary).
Read replicas often replicate asynchronously. Expect replication lag: reads served from replicas may be eventually consistent. Design UX and critical reads to tolerate or avoid stale data (e.g., read-your-writes scenarios).
- MongoDB (NoSQL) holds unstructured or semi-structured data such as user preferences, activity streams, and behavioral events where flexible schemas are helpful.
- If dataset size grows beyond a single instance’s capacity, use sharding/partitioning to split data across machines and scale horizontally.
- Configure Redis for persistence and clustering; tune eviction policies for session vs. cache data.
- Monitor replica lag, index usage, and cache hit rates.
- Automate backups and test failover procedures regularly.
References and links
- Redis: https://redis.io/
- Postgres: https://www.postgresql.org/
- MongoDB: https://www.mongodb.com/
