Why use your application-level cache if database already provides caching?

vbezhenar picture vbezhenar · Jun 3, 2010 · Viewed 7.6k times · Source

Modern database provide caching support. Most of the ORM frameworks cache retrieved data too. Why this duplication is necessary?

Answer

Dean Harding picture Dean Harding · Jun 3, 2010

Because to get the data from the database's cache, you still have to:

  1. Generate the SQL from the ORM's "native" query format
  2. Do a network round-trip to the database server
  3. Parse the SQL
  4. Fetch the data from the cache
  5. Serialise the data to the database's over-the-wire format
  6. Deserialize the data into the database client library's format
  7. Convert the database client librarie's format into language-level objects (i.e. a collection of whatevers)

By caching at the application level, you don't have to do any of that. Typically, it's a simple lookup of an in-memory hashtable. Sometimes (if caching with memcache) there's still a network round-trip, but all of the other stuff no longer happens.