This repo contains common cache-to-database coordination strategies—cache-aside, read-through, write-through, write-around, and write-back using Redis and MySQL. Each pattern lives in its own module so you can trace the exact read/write flow from the command line.
-
Prerequisites: Python, Docker Desktop (for Redis/MySQL)
-
Start infrastructure:
docker compose up -dlaunches Redis on6379and MySQL on3307. -
Bootstrap the database:
docker exec -i mysql-db mysql -u cache_demo -pcache_demo < schema.sql. -
Configure the app: copy
app_config.tomlif you need different ports or credentials; you can also override entries with${ENV_VAR}placeholders plus a.env.
- Run all pattern demos:
uv run python -m app.main. Each pattern writes sample users and prints cache/DB reads. - Observe write-back persistence: start
uv run python -m app.worker.write_back_workerin another terminal to drain the Redis queue.
- Cache-Aside (
app/patterns/cache_aside.py): writes go to MySQL, then the Redis key is invalidated so the next read reloads from the DB.
- Read-Through (
app/patterns/read_through.py): documents why Redis lacks true read-through behavior and when managed caches (Hazelcast, DAX, etc.) make sense.
- Write-Through (
app/patterns/write_through.py): every write persists to MySQL first, then best-effort to Redis with a TTL pulled from config.
- Write-Around (
app/patterns/write_around.py): writes bypass Redis entirely; reads repopulate keys on demand.
- Write-Back (
app/patterns/write_back.py+app/worker/write_back_worker.py): writes land in Redis and a queue; a separate worker drains the queue and commits to MySQL asynchronously.





