Companion code for Install Valkey on Ubuntu (Redis Replacement) on ComputingForGeeks.
A small FastAPI service that uses Valkey (a drop-in Redis replacement) for:
- Cache-aside over a slow Postgres query (519 ms to 0.71 ms in testing)
- Rate limiting (fixed-window,
INCR+EXPIRE) - Sessions (keys with a TTL)
It connects with the standard redis-py client, unchanged, to show Valkey is wire-compatible with Redis.
docker compose up -d
python3 -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
uvicorn app:app --host 127.0.0.1 --port 8000Then:
curl -s http://127.0.0.1:8000/products/1 # MISS -> Postgres
curl -s http://127.0.0.1:8000/products/1 # HIT -> ValkeyWatch the live traffic with valkey-cli monitor in another terminal.
MIT