You are tasked with designing and implementing a scalable backend for a messaging system similar to WhatsApp or Slack. The system must allow users to send, receive, and retrieve messages efficiently while ensuring scalability and reliability as the user base grows.
-
Send a Message
- Endpoint:
POST /messages - Accepts:
- Sender ID
- Receiver ID (or Group ID for group messages)
- Message content
- Timestamp
- Stores the message in a database.
- Endpoint:
-
Retrieve Messages
- Endpoint:
GET /messages - Features:
- Fetch messages between two users or within a group.
- Support pagination for message history.
- Allow filtering by a date range.
- Endpoint:
-
Delivery Status
- Track message status:
sent,delivered, andread. - Update status when a recipient reads the message.
- Track message status:
Design a schema to store:
- Users
- Messages
- Groups (if applicable)
- Delivery status of each message.
Ensure the schema supports:
- Fast retrieval of conversation history.
- Efficient storage for large-scale systems.
- Support millions of users and billions of messages.
- Implement a caching layer (e.g., Redis) for frequently accessed data.
- Use database sharding or partitioning for scalability.
- Propose strategies for load balancing and failover.
- Use WebSockets or a similar protocol for real-time communication.
-
API Implementation
- Create fully functional RESTful API endpoints for the core features.
- Validate all inputs and handle errors gracefully.
-
Database Design
- Provide the schema definition (SQL or NoSQL) for the system.
-
Caching Strategy
- Integrate a caching layer for optimizing message retrieval and user status.
-
Documentation
- Provide a
README.mdexplaining:- How to set up and run the application.
- The architecture and design decisions.
- Provide a
-
Dockerization
- Create a
Dockerfileto containerize the application. - Provide a
docker-compose.ymlto set up the application, database, and caching layer.
- Create a
-
Code Quality:
- Adherence to clean code practices and readability.
-
Scalability and Performance:
- Efficient database queries and caching implementation.
-
Functionality:
- Correct implementation of API endpoints and real-time notifications.
-
Documentation:
- Clarity and completeness of the provided documentation.
Before running the system, make sure you have the following installed:
- Docker: For containerizing the application and services.
- Docker Compose: To manage multi-container Docker applications.
- Node.js (optional, if you're running the backend manually without Docker).
First, clone the repository to your local machine:
git clone https://github.com/Imo-Tech-Solutions/david-cole.git
cd david-cole
You can configure the application by modifying the .env.sample or create a new .env file, also set environment variables in the docker-compose.yml file.
PORT: The port on which the app will run. Default is 3000. DB_USER: add your postgres username. DB_PASSWORD: add your postgres password. DB_HOST: add your postgres hostname. (use: "db" for Docker). REDIS_HOST: The Redis host (default: redis for Docker). REDIS_PORT: The Redis port (default: 6379).
This system uses Docker to manage the backend services like PostgreSQL and Redis. To run the application, use Docker Compose to start all services.
Build and Run with Docker Compose: Run the following command to start the system with all dependencies:
docker-compose up --build
This will:
Build the Docker containers. Start the app container (the messaging system). Start the PostgreSQL container (for database storage). Start the Redis container (for caching and real-time messaging).
Once the system is running, you can test the functionality by making HTTP requests to
POST http://localhost:3000/api/v1/messages to add new messages
GET http://localhost:3000/api/v1/messages to retrieve messages
Web socket on ws://localhost:3000 to observe real time messaging