This repository contains the stub code for the Distributed Systems course assignment to implement a Facebook Messenger backend using Apache Cassandra as the distributed database.
The application follows a typical FastAPI structure:
app/
: Main application packageapi/
: API routes and endpointscontrollers/
: Controller logic (stubs provided)models/
: Database models (stubs provided, to be implemented by students)schemas/
: Pydantic models for request/response validationdb/
: Database connection utilities (Cassandra client)
- Docker and Docker Compose (for containerized development environment)
- Python 3.11+ (for local development)
This repository includes a Docker setup to simplify the development process. All you need to get started is:
- Clone this repository
- Make sure Docker and Docker Compose are installed on your system
- Run the initialization script:
./init.sh
This will:
- Start both FastAPI application and Cassandra containers
- Initialize the Cassandra keyspace and tables
- Optionally generate test data for development
- Make the application available at http://localhost:8000
Access the API documentation at http://localhost:8000/docs
To stop the application:
docker-compose down
The setup script includes an option to generate test data for development purposes. This will create:
- 10 test users (with IDs 1-10)
- 15 conversations between random pairs of users
- Multiple messages in each conversation with realistic timestamps
You can use these IDs for testing your API implementations. If you need to regenerate the test data:
docker-compose exec app python scripts/generate_test_data.py
If you prefer not to use Docker, you can set up the environment manually:
- Clone this repository
- Install Cassandra locally and start it
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
uv pip install -r requirements.txt
- Run the setup script to initialize Cassandra:
python scripts/setup_db.py
- Start the application:
uvicorn app.main:app --reload
For this assignment, you will need to design and implement your own data model in Cassandra to support the required API functionality:
- Sending messages between users
- Retrieving conversations for a user, ordered by most recent activity
- Retrieving messages in a conversation, ordered by timestamp
- Retrieving messages before a specific timestamp
Your data model should consider:
- Efficient distribution of data across nodes
- Appropriate partition keys and clustering columns
- How to handle pagination efficiently
- How to optimize for the required query patterns
You need to implement:
- Cassandra schema design - create tables to support the required queries
- Message and Conversation models (
app/models/
) to interact with Cassandra - Controller methods in the stub classes (
app/controllers/
):- Send Message from one user to another (only the DB interaction parts here. No need to implement websocket etc needed to actually deliver message to other user)
- Get Recent Conversations of a user (paginated)
- Get Messages in a particular conversation (paginated)
- Get Messages in a conversation prior to a specific timestamp (paginated)
POST /api/messages/
: Send a message from one user to anotherGET /api/messages/conversation/{conversation_id}
: Get all messages in a conversationGET /api/messages/conversation/{conversation_id}/before
: Get messages before a timestamp
GET /api/conversations/user/{user_id}
: Get all conversations for a userGET /api/conversations/{conversation_id}
: Get a specific conversation
- Correct implementation of all required endpoints
- Proper error handling and edge cases
- Efficient Cassandra queries (avoid hotspots and ensure good distribution)
- Code quality and organization
- Proper implementation of pagination
- Performance considerations for distributed systems
- Adherence to Cassandra data modeling best practices