A robust, production-ready backend for a social media platform built with FastAPI, PostgreSQL, and Docker.
This project focuses on high-concurrency optimization, successfully scaling to handle 1,000 concurrent users.
- Python 3.10+
- Docker Desktop (Recommended)
OR - PostgreSQL 15+
Create a .env file in the root directory:
DATABASE_HOSTNAME=db
DATABASE_PORT=5432
DATABASE_PASSWORD=your_password
DATABASE_NAME=fastapi
DATABASE_USERNAME=postgres
SECRET_KEY=your_secret_key_here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30This method is optimized for high concurrency and avoids Windows networking limitations.
# 1. Build and start containers
docker-compose up --build -d
# 2. Run database migrations
docker-compose exec app alembic upgrade head
# 3. Create a test user
curl -X POST "http://localhost:8000/users/" \
-H "Content-Type: application/json" \
-d "{\"email\": \"hello124@gmail.com\", \"password\": \"password123\"}"
# 4. Verify login
curl -X POST "http://localhost:8000/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=hello124@gmail.com&password=password123"# Create virtual environment
python -m venv venv
# Activate it
source venv/bin/activate
# Windows:
# venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run the app
uvicorn app.main:app --reloadAs part of an engineering exploration at IIT Guwahati, this API was stress-tested using Locust to uncover real-world bottlenecks.
-
64 Connection Limit (Windows)
Encountered:ValueError: too many file descriptors in select()Fixed by moving to Docker (Linux
epoll) -
Connection Starvation
Default SQLAlchemy pool size (5) caused 100% failures at 200 users -
CPU Saturation
Argon2 password hashing maxed CPU → 34% failure rate at 1,000 users
Optimized system to handle 1,000 users (spawn rate = 2) with only 12% failures:
-
Engine Tuning
pool_size=50 max_overflow=100
-
PostgreSQL Scaling
max_connections = 300 -
Multi-Worker Scaling
- 4 Uvicorn workers (Docker)
-
Data Hygiene
reset_db.pyscript to truncate tables between tests
pytest# Start Locust
locust -f locustfile.pyTo push beyond current limits and scale to 5,000+ users:
- Cache
GET /posts/ - Store JWT user objects in memory
Reduce DB load by ~80%
- Reverse proxy
- SSL termination
- Smarter request distribution
- AWS (ECS + RDS) or Render
- Public production endpoint