A small FastAPI backend demonstrating a cache-aside pattern using Redis and PostgreSQL. The API uses Redis as an optional in-memory cache with TTL and falls back to the database as the main data source. This project is intentionally minimal and was focused on backend architecture and tool/library familiarity rather than frontend work.
- Python
- FastAPI
- Redis
- PostgreSQL
- SQLAlchemy
GET /items/{id}- Checks Redis first, falls back to check Postgres on cache miss, then writes to cache with TTL.
PUT /items/{id}- Writes to Postgres and invalidates the related Redis cache key.
- Redis is optional - if unavailable, the API still serves data from the database.
python -m venv apiproj
apiproj\Scripts\activate
pip install -r requirements.txt
DATABASE_URL=postgresql+psycopg2://appuser:apppass@localhost:5432/appdb
REDIS_HOST=localhost
REDIS_PORT=6379
CACHE_TTL_SECONDS=60
uvicorn app:app --reload
http://127.0.0.1:8000/ auto redirects to docs
GET /items/{id}PUT /items/{id}DELETE /items/{id}GET /health
- Cached values are JSON-serialized
- Cache keys use the format
item:{id} - TTL is configurable via environment variables