Skip to content

Caching Strategies and How to Choose the Right one

  • Caching is one of the easiest ways to increase system performance
  • Your caching strategy depends on the data and data access patterns
  • A caching strategy for Top-10 leaderboard system for mobile games will be very different than a service which aggregates and returns user profiles.

Caching Strategies

  1. Cache Aside

Cache Aside

  • Also called lazy loading
  • most commonly used, cache sits aside and the applications talks with both the cache and the database (if needed)
  • Read Flow -> Cache Request -> Cache Hit (return data) ->if Cache Miss, Read from Database and write to cache.
  • No connection between the cache and the primary database
  • Most common write strategy is to write directly to database.
    • Cache may become inconsistent with the database.
    • To avoid cache inconsistency, cache data has time to live after which data expires.
Use Case, Pros and Cons
  • good for read-heavy environments
  • resilient to cache failures
    • Cache is optional, not critical. If cache fails, system still continues directly with the database
    • Database is the source of data, not the cache. Cache loss doesn't affect data integrity
    • Cache self heals once it comes back online as it is populated by the application via database reads.
  • Data model used in cache can be different that data model used in DB.
    • This gives freedom to store multiple queries or data in different dimensions based on specific app needs.

Read Through Cache

  • Read-through cache sits in-line with the database.
  • When there is a cache miss, it loads missing data from database, populates the cache and returns it to the application.
  • Loads data lazily, only when it is requested by the application.
Use Case, Pros and Cons
  • The application is not responsible for populating the cache. Usually done by the library itself.
  • Data model in read through cache cannot be different than that of the database.
  • Works best for read-heavy workloads when same data is requested multiple times.
  • possible for data to become inconsistent between cache and the database

Write Through Cache

  • Data is first written to the cache and then to the database.
    • Writes always go through the cache to the main database
Use Case, Pros and Cons

- On standalone, write through cache is a bad idea as it increases latency.