Appearance
Server Infrastructure Overview
Context
This document provides a detailed overview of the server-side infrastructure, focusing on the components used within the container and their interactions.
Architecture Diagram
Cloudfront serves as the entry point for all traffic, see more in the AWS Infrastructure Overview.
Purpose of the Infrastructure
The server infrastructure is designed to:
- Facilitate efficient real-time communication using a pub/sub model.
- Manage caching for optimized data retrieval and performance.
- Handle background task processing with a robust queueing system.
- Integrate seamlessly with a MongoDB Atlas database for persistence.
- Provide a scalable and efficient GraphQL API using Apollo Server.
Components and Workflow
1. Elastic Cache (Valkey)
- Purpose: Acts as a central pub/sub system, cache layer, and queue manager.
- Key Features:
- Pub/Sub: Enables real-time updates and communication for GraphQL subscriptions.
- Caching: Improves performance by storing frequently accessed data and reducing database load.
- Queue Management: Powers background task processing using BullMQ.
- Integration: Works closely with Apollo Server for efficient query resolution and with the task processing layer for asynchronous jobs.
2. MongoDB Atlas
- Purpose: Provides a fully managed database for persistent storage.
- Usage:
- Stores structured and unstructured data, such as user information, application settings, and logs.
- Ensures high availability and scalability through its cloud-based architecture.
- Integration:
- Acts as the primary data source for Apollo Server's GraphQL resolvers.
- Data is cached in Elastic Cache to reduce redundant queries.
- Logging:
- Logs user interactions such as gem purchases and usage, allowing tracking and auditing of wallet-related actions.
3. Apollo Server
- Purpose: Serves as the GraphQL API layer for client-server communication.
- Key Features:
- Provides a unified schema for querying and mutating data.
- Supports real-time subscriptions powered by the pub/sub mechanism in Elastic Cache.
- Integration:
- Resolves data from MongoDB Atlas, with caching handled by Elastic Cache.
4. BullMQ
- Purpose: Provides a robust task queue for handling background jobs.
- Usage:
- Manages asynchronous repetitive tasks, such as buffer contributions and user activity logging.
- Integration: Interacts with the server logic to execute tasks independently of user-facing processes, improving API responsiveness.
Additional Information
5. Server Management and Load Balancing
- The server infrastructure is managed through AWS ECS (Elastic Container Service) with AWS Fargate, allowing for easy scaling and management of containerized services.
- A load balancer is configured to ensure high availability and distribute incoming traffic evenly across the server instances.
6. Logging and Monitoring
- All API requests and user activities (e.g., gem purchases, wallet interactions) are logged at the application level.
- Logs are stored and managed in AWS CloudWatch, with logs stored in JSON format for easier querying and filtering.
- A retention policy is applied to the logs, where they are retained for 7 days.
S3 Bucket - Images
Images are sent to the server either via URL or base64 encoding. Upon receiving the image, we use Sharp to resize and optimize it for performance and storage efficiency. If moderation is required, we leverage the AWS Rekognition API to analyze the image for inappropriate content. Once the image is processed and verified, it is stored securely in the S3 bucket for further use.
The images stored in the S3 bucket are accessible through AWS CloudFront, which acts as a content delivery network (CDN). CloudFront does not block access to these images, simplifying the process of retrieving them. Typically, images are stored using an Object ID provided by MongoDB. This Object ID serves as a unique identifier, making it easy to locate and retrieve the corresponding image directly from the S3 bucket.
S3 Bucket - Videos
Due to their large file sizes, videos cannot be sent via URL or base64 encoding to the server. To ensure that only authorized users can upload videos, we utilize pre-signed URLs, which allow users to securely upload videos directly to the S3 bucket without needing to route the file through the server. This process ensures that uploads are handled efficiently and securely.
For more details about video handling and related processes, refer to the Video Process.