Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions SECURITY.md

This file was deleted.

93 changes: 93 additions & 0 deletions submissions/fastapi-cache-valkey-demo_lokesh-kumar-yerra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# fastapi-cache Valkey Backend Demo

---

## Attendee/Team Details

**Name:** Lokesh Kumar Yerra
**GitHub Username:** LokeshKumar710
**LinkedIn Profile:** <!-- TODO: add your LinkedIn URL e.g. https://linkedin.com/in/lokeshkumaryerra -->
**GitHub Project Repository:** https://github.com/LokeshKumar710/fastapi-cache-valkey-demo

---

## Problem Statement Selected

```
Add Valkey as a native caching backend for the fastapi-cache library
(https://github.com/long2ice/fastapi-cache)
```

Track A — Framework/Library/Project Integration (Python / FastAPI ecosystem)

---

## Project Description

**fastapi-cache Valkey Backend Demo** is a complete, production-ready example application that proves Valkey works as a drop-in, high-performance caching backend for the popular `fastapi-cache2` library.

* **What is it?** A FastAPI application with a React frontend that demonstrates every major caching pattern — GET caching, POST caching with custom body-derived cache keys, TTL expiry, and manual cache invalidation — all backed by Valkey.
* **Who is it for?** Python/FastAPI developers who want a real, runnable example of using Valkey instead of Redis for application-level caching.
* **What problem does it solve?** The `fastapi-cache` library's documentation and examples only show Redis. This project provides a concrete, working Valkey integration with benchmarks that prove the latency improvement — making it easy for maintainers to accept a Valkey backend PR.
* **How does it help?** Developers can clone this repo, run `docker compose up`, and immediately see a 50–100× latency difference between cache misses and cache hits, with a live UI showing the numbers in real time.

---

## Approach

1. **Understood the problem** — `fastapi-cache2` uses a `Backend` abstraction (`RedisBackend`, `MemcacheBackend`, etc.). Since Valkey is Redis 7.2-compatible, the `redis-py` async client connects to Valkey with zero code changes. The challenge was proving this with a compelling demo.

2. **Designed the demo flow** — Built four distinct proof points:
- `GET /slow-products` — 3-second simulated DB call, cached 60 s → proves GET caching
- `POST /summarize` — 2–3 second fake AI call with a **SHA-256 body hash as the cache key** → proves POST body caching (the most interesting pattern for AI/LLM APIs)
- `DELETE /cache/{key}` → proves manual invalidation
- `GET /stats` → shows cache config, TTLs, and key structure

3. **Built a custom key builder** — The `summarize_key_builder` normalises the request body to stable JSON, hashes it with SHA-256, and builds a deterministic cache key. This is the real technical contribution: showing how to cache non-idempotent POST endpoints safely.

4. **Added a benchmark panel** — The React frontend runs cold → hot calls automatically and shows a real-time speedup multiplier (`68× faster`), making the value of Valkey caching instantly visible.

5. **Made it reproducible** — Docker Compose brings up Valkey + FastAPI + React in one command, so anyone can verify the results.

---

## Tech Stack

| Layer | Technology |
|---|---|
| Cache backend | **Valkey 7.2** (Redis-compatible, Docker image `valkey/valkey:7.2-alpine`) |
| Cache library | **fastapi-cache2** (`fastapi-cache2>=0.2.1`) |
| Backend framework | **FastAPI** + **Uvicorn** (Python 3.12) |
| Redis client | **redis-py** async (`redis>=5.0.4`) |
| Frontend | **React 18** + **Vite 5** |
| Styling | Vanilla CSS (dark-mode design system) |
| Icons | Lucide React |
| Containerisation | **Docker** + **Docker Compose** |
| Language | Python 3.12 / JavaScript (ES2023) |

---

## Key Technical Highlights

- **Custom POST cache key builder** using SHA-256 hash of the normalised request body — enables safe caching of POST endpoints (critical for AI/LLM API caching)
- **Zero Valkey-specific client code** — the standard `redis.asyncio` client connects to Valkey seamlessly, proving drop-in compatibility
- **Live latency proof** — the React benchmark panel runs cold + hot calls and displays the exact speedup ratio
- **Manual invalidation endpoint** — `DELETE /cache/{key}` lets you delete any Valkey key and watch the next request recompute, proving the cache was actually storing results

---

## How to Run

```bash
# Option 1: Docker Compose (everything in one command)
docker compose up --build

# Option 2: Local
docker run -d -p 6379:6379 valkey/valkey:7.2-alpine
pip install -r requirements.txt
uvicorn app.main:app --reload # Terminal 1 → API at :8000
cd frontend && npm install && npm run dev # Terminal 2 → UI at :3000
```

- Frontend: http://localhost:3000
- API docs: http://localhost:8000/docs