Skip to content

tirthraj07/cache-patterns

Repository files navigation

Overview

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.

Getting Started

  • Prerequisites: Python, Docker Desktop (for Redis/MySQL)

  • Start infrastructure: docker compose up -d launches Redis on 6379 and MySQL on 3307.

  • Bootstrap the database: docker exec -i mysql-db mysql -u cache_demo -pcache_demo < schema.sql.

  • Configure the app: copy app_config.toml if you need different ports or credentials; you can also override entries with ${ENV_VAR} placeholders plus a .env.

How to Use

  • 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_worker in another terminal to drain the Redis queue.

Detailed Concepts

  • 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.

About

Common cache-to-database coordination strategies with their implementation with Redis and MySQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages