Data Caching in Web API

857 views Asked by At

I am working on web api project, my web api is calling repository.Repository calls third party data source to perform CRUD.Calling the data source is very costly, and it gets updated weekly.

So I thought to implement caching. I have seen few output caching packages, but it does not fulfill my requirement, because:

  1. If I output cache Get method, I am not able to use same Cache output in GetById method or the same cached data for some other operation like find opeartion. I have to also manually update cache when ever any update/post happens.
  2. One more thing i am confused what to do in this scenario whether remove cache or update cache whenever put or post operation happens?

I am totally confused to complete this requirement.Please suggest me how to fulfill this requirement.I searched on web,but have not found anything like that.

I am novice both on SO and WebAPI so pardon me if question not fulfilling the standard

1

There are 1 answers

0
Diptendu On

If I output cache Get method, I am not able to use same Cache output in GetById method or the same cached data for some other operation like find opeartion. I have to also manually update cache when ever any update/post happens.

To use the cached data for different operations like GetById and Find you need to store data in different data structures. Caches like REDIS supports hashmap for objects which can be used by GetById. It totally depends on your case what kind DS you need to use.

One more thing i am confused what to do in this scenario whether remove cache or update cache whenever put or post operation happens?

To answer second part of your first question and this one I would say you need to choose between a writeback and write through cache. You can read more about WB and WT caches in this article. Basically there are two approaches

  1. Each cache entry has some TTL and after that you fetch data from "data source". The Pros is your POST and PUT operations will be faster since it does not need to update the cache but cons is the data might be stale for sometime.
  2. The second option is to invalidate the appropriate entry in the cache whenever a POST or PUT operation happens.
  3. The third option is to update the cache entry at the time of POST and PUT.

In terms of write / update latency option 1 is the fastest but has the risk of getting stale data. Option 2 will slow down both GET, PUT / POST operations and option 3 will slow down the write operations.

Your choice should depend on the ratio of read and write operations in the system. If the system you are designing is read heavy then option 3 is better than 2.