CachingWhy bother caching?

πŸ§‘β€πŸ’» Your app πŸ›οΈ Database = city archive far away Β· slow Β· busy 🐌 every read: 20–100 ms
Every read walks to the archive.
Keep a copy closer.

Pattern 1In-Memory Cache = your desk

App instance A πŸ§‘β€πŸ’» price 9 € own desk, ~0.001 ms App instance B πŸ§‘β€πŸ’» price 8 € own desk, different notes! β‰  stale
⚑ fastest πŸ”’ private per instance ⚠ can drift
Fast and simple, but every desk has its own version.

Pattern 2Shared Cache = the office shelf

πŸ§‘β€πŸ’»App A πŸ§‘β€πŸ’»App B πŸ§‘β€πŸ’»App C 9 € user Redis / Memcached one copy Β· ~1 ms Β· everyone agrees πŸ›οΈ archive
🀝 consistent πŸ“ˆ scales out 🚢 one hop away
One shelf for the whole office.

Pattern 3Cache-Aside = the routine

πŸ§‘β€πŸ’»App (owns the logic) πŸ—„οΈ Cache (the shelf) πŸ›οΈ Database 1 check cache HIT βœ“ done 2 MISS βœ— β†’ read from DB 3 put a copy on the shelf
Write? Update the DB, then throw the cached copy away.