Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 974
vbezhenar Avatar asked Jun 03 '10 06:06

vbezhenar


People also ask

Why do we cache at the database query level in the application cache even if there is a cache in the database?

The cache actually stores the data already loaded from the database, so that the traffic between our application and the database will be reduced when the application want to access that data again. The access time and traffic will be reduced between the application and the database.

When would you use a cache over a database?

In-memory data caching can be one of the most effective strategies to improve your overall application performance and to reduce your database costs. Caching can be applied to any type of database including relational databases such as Amazon RDS or NoSQL databases such as Amazon DynamoDB, MongoDB and Apache Cassandra.

What is difference between cache and database?

Databases are generally regarded as persistent, consistent and queryable data stores. Caches behave like databases, except they shed many features to improve performance like it favors fast access over durability & consistency.

What is application level caching?

It consists of the insertion of caching logic into the application base code to temporarily store processed content in memory, and then decrease the response time of web requests by reusing this content.


2 Answers

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.

like image 196
Dean Harding Avatar answered Oct 03 '22 21:10

Dean Harding


Here are a couple of reasons why you may want this:

  • An application caches just what it needs so you should get a better cache hit ratio
  • Accessing a local cache will probably be a couple of orders of magnitude faster than accessing the database due to network latency - even with a fast network
like image 40
Robert Christie Avatar answered Oct 03 '22 21:10

Robert Christie