Skip to content

Anikk02/FastAPI-Authentication-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

48 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ FastAPI Authentication System (Backend Project)

๐Ÿ“Œ Overview

This project is a production-style FastAPI authentication system designed with scalability, performance, and real-world backend engineering practices in mind.

It demonstrates how to build and optimize a backend system using:

  • FastAPI
  • PostgreSQL
  • Redis
  • JWT Authentication
  • Load Testing (Locust)

๐Ÿง  Key Features

  • ๐Ÿ” JWT-based Authentication (Login/Register)
  • ๐Ÿ‘ค User Management APIs
  • โšก Redis Caching for performance optimization
  • ๐Ÿ—„๏ธ PostgreSQL with optimized connection pooling
  • ๐Ÿงช Load testing with Locust
  • ๐Ÿ“Š Performance benchmarking & analysis
  • ๐Ÿ”„ Multi-worker scaling using Uvicorn

๐Ÿ—๏ธ Tech Stack

Layer Technology
Backend FastAPI
Database PostgreSQL
Cache Redis
ORM SQLAlchemy
Auth JWT (python-jose)
Password Hashing bcrypt (passlib)
Load Testing Locust

โš™๏ธ System Architecture

Client โ†’ FastAPI โ†’ Redis (cache) โ†’ PostgreSQL
                โ†“
             JWT Auth

Architectural Design

Shows interactions between FastAPI, Redis, and PostgreSQL with JWT-based authentication. alt text

๐Ÿ”ฅ Performance Journey

Phase 1: Initial System

  • High latency
  • DB bottleneck
  • Poor scalability

Phase 2: PostgreSQL Optimization

  • Connection pooling introduced
  • Improved stability

Phase 3: bcrypt Optimization

  • Reduced hashing cost
  • Improved response time

Phase 4: Redis Integration

  • Cached /users/me
  • Eliminated repeated DB reads

๐Ÿ“Š Benchmark Results

โœ… With Redis (300 Users)

  • Throughput: ~155 RPS
  • Avg Latency: ~456 ms
  • Failure Rate: ~0.09%

โš ๏ธ With Redis (500 Users)

  • Throughput: ~93 RPS
  • Avg Latency: ~1.7 sec
  • Failure Rate: ~3%

๐Ÿง  Key Insights

  • Redis removed the database bottleneck
  • System shifted from I/O-bound โ†’ CPU-bound
  • bcrypt hashing became the main limitation
  • Worker saturation affects performance under high load

๐Ÿ“ Project Structure

FastAPI-Authentication-System/
โ”‚
โ”œโ”€โ”€ .github/
โ”‚ โ””โ”€โ”€ workflows/
โ”‚ โ””โ”€โ”€ ci.yml # CI/CD pipeline (GitHub Actions)
โ”‚
โ”œโ”€โ”€ app/
โ”‚ โ”œโ”€โ”€ core/
โ”‚ โ”‚ โ””โ”€โ”€ redis.py # Redis client setup
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ routes/
โ”‚ โ”‚ โ”œโ”€โ”€ auth_routes.py # Authentication endpoints (login/register)
โ”‚ โ”‚ โ”œโ”€โ”€ user_routes.py # User endpoints (/users/me)
โ”‚ โ”‚ โ”œโ”€โ”€ redis_routes.py # Redis test/debug endpoints
โ”‚ โ”‚ โ””โ”€โ”€ init.py
โ”‚ โ”‚
โ”‚ โ”œโ”€โ”€ init.py
โ”‚ โ”œโ”€โ”€ auth.py # JWT + password hashing (bcrypt)
โ”‚ โ”œโ”€โ”€ config.py # Environment configuration
โ”‚ โ”œโ”€โ”€ database.py # DB connection & session
โ”‚ โ”œโ”€โ”€ dependencies.py # Auth dependency (get_current_user)
โ”‚ โ”œโ”€โ”€ logger.py # Logging setup
โ”‚ โ”œโ”€โ”€ main.py # FastAPI entry point
โ”‚ โ”œโ”€โ”€ models.py # SQLAlchemy models
โ”‚ โ””โ”€โ”€ schemas.py # Pydantic schemas
โ”‚
โ”œโ”€โ”€ diagrams/ # System design diagrams
โ”‚ โ”œโ”€โ”€ architectural_design.png
โ”‚ โ”œโ”€โ”€ component.png
โ”‚ โ”œโ”€โ”€ sequence_login.png
โ”‚ โ”œโ”€โ”€ sequence_users_me.png
โ”‚ โ”œโ”€โ”€ state_auth.png
โ”‚ โ”œโ”€โ”€ deployment.png
โ”‚ โ””โ”€โ”€ data_flow.png
โ”‚
โ”œโ”€โ”€ performance/ # Load testing & benchmarking
โ”‚ โ”œโ”€โ”€ screenshots/ # Locust UI screenshots
โ”‚ โ”œโ”€โ”€ results/ # Metrics per phase
โ”‚ โ””โ”€โ”€ notes/ # Observations & insights
โ”‚
โ”œโ”€โ”€ tests/ # Unit & integration tests
โ”œโ”€โ”€ logs/ # Application logs
โ”‚
โ”œโ”€โ”€ requirements.txt # Dependencies
โ”œโ”€โ”€ README.md # Project documentation
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ test_auth.db # SQLite test DB (local/testing)
โ””โ”€โ”€ .test_auth.db # Temporary test DB (CI)

๐Ÿš€ How to Run

1. Clone Repository

git clone <your-repo-url>
cd FastAPI-Authentication-System

2. Install Dependencies

pip install -r requirements.txt

3. Start PostgreSQL

Make sure PostgreSQL is running and configured.

4. Start Redis (Docker)

docker run -d -p 6379:6379 redis

5. Run Server

uvicorn app.main:app --workers 4

๐Ÿงช Load Testing

Run Locust:

locust -f locustfile.py --host=http://127.0.0.1:8000

Open:

http://localhost:8089

๐Ÿ”ฎ Future Improvements

  • Async DB (asyncpg)
  • Background password hashing
  • Load balancer (Nginx)
  • Horizontal scaling (multiple instances)
  • Rate limiting using Redis

๐ŸŽฏ What This Project Demonstrates

  • Backend system design
  • Performance optimization
  • Load testing & benchmarking
  • Bottleneck identification
  • Real-world scalability challenges

๐Ÿ‘จโ€๐Ÿ’ป Author

Aniket Paswan

Aspiring AI/ML Engineer,Backend Engineer


โญ Final Note

This project reflects a real engineering journey:

Database Bottleneck โ†’ Redis Optimization โ†’ CPU Bottleneck

Understanding this transition is key to designing scalable backend systems.

About

A RESTful authentication service built using FastAPI, SQLAlchemy, and JWT. Implements secure user registration, login, token-based authentication, and role-ready user management. Follows modular architecture with centralized logging, environment-driven configuration, and clean dependency injection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors