100 points
3

Want to solve programming problems and get paid for it? If that sounds interesting to you then contact us.

Top 15 Cache Interview Questions (With Code) To Smash Your Next Interview

Q1. What is Caching?

Answer: In computing, a cache is a high-speed data storage layer which stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data’s primary storage location. Caching allows you to efficiently reuse previously retrieved or computed data.

Source: medium.com

Q2.What is Application object?

Answer: The Application object can be used in situations where we want data to be shared across users globally.

Source: codeproject.com

Q3.How to access a cache object ?

Answer: The Cache object is defined in the System.Web.Caching namespace. You can get a reference to the Cache object by using the Cache property of the HttpContext class in the System.Web namespace or by using the Cache property of the Page object.

Source: codeproject.com

Q4. What are the different layers in a distributed system, where cache can be implemented?

Answer:  Caching can be implemented in the following layers.

  • Client caching - Caching on the client side (browser)
  • CDN caching - A Content Delivery Network (CDN) which is a globally distributed network of proxy servers, typically serving static content, is considered a type of cache.
  • Web server caching - Caching on the web server or proxy server side, which enables returning client response without contacting the application servers or databases.
  • Application server caching - Caching on the application server side, which enables returning client response without contacting the database.
  • Database caching - Caching on the database side, which enables returning client response without executing the queries.

Source: interviewgrid.com

Q5. What is cache invalidation? What are the three main methods of cache invalidation ?

Answer: When data is updated in the database, then that data has to be refreshed in the cache as well. This is called cache invalidation.There are three main methods of cache invalidation

Write-through cache - Data is written to the cache and database at the same time.

Write-around cache - Data is written to the database writing to cache. Data is written to cache when a request results in a 'cache miss', at which point data in retrieved from database, written to cache, and send back to client.

Write-back (Write-behind) cache - Data is written to the cache without writing to database. Data is written to database asynchronously.

 Source: interviewgrid.com

Q6.What are cache eviction algorithms?

Answer:  Following are some common cache eviction algorithms :

  • First In First Out (FIFO)
  • Last In First Out (LIFO)
  • Least Recently Used (RLU)
  • Most Recently Used (MRO)
  • Least Frequently Used
  • Least Frequent Recently Used (LFRU)
  • Random Replacement (RR)

 Source: interviewgrid.com

Q7. What should be the access pattern for the given cache?

Answer: There are majorly three kinds of caching systems :

  • Write through cache : This is a caching system where writes go through the cache and write is confirmed as success only if writes to DB and the cache BOTH succeed. This is really useful for applications which write and re-read the information quickly. However, write latency will be higher in this case as there are writes to 2 separate systems.
  • Write around cache : This is a caching system where write directly goes to the DB. The cache system reads the information from DB incase of a miss. While this ensures lower write load to the cache and faster writes, this can lead to higher read latency incase of applications which write and re-read the information quickly.
  • Write back cache : This is a caching system where the write is directly done to the caching layer and the write is confirmed as soon as the write to the cache completes. The cache then asynchronously syncs this write to the DB. 

 Source: interviewbit.com

Q8. What should be the eviction strategy?

Answer:  It is possible that we might get entries when we would not have space to accommodate new entries. In such cases, we would need to remove one or more entries to make space for the new entry.

 Source: interviewbit.com

Q9.What is ViewState?

Answer:    ViewState is a built-in structure for automatically retaining values amongst the multiple requests for the same page. The viewstate is internally maintained as a hidden field on the page but is hashed, providing greater security than developer-implemented hidden fields do.

 Source: programmingcrackers.blogspot.com

Q10. Where do u find usage of DataCacheing?

Answer:-While dealing with company Quarter results, Dataset can be maintained in Cache

                -In news website common news will be maintained in Cache

 Source: programmingcrackers.blogspot.com

Q11. What is cache callback in cache?

Answer: -The cache object is dependent on its dependencies, for example, file based, time based, etc...Cache items remove the object when the cache dependencies change. ASP.NET provides the capability to execute a callback method when that item is removed from the cache.

 Source: programmingcrackers.blogspot.com

Q12. Name some Cache Invalidation methods ?

Answer: 

  • Purge - Removes content from cache immediately. When the client requests the data again, it is fetched from the application and stored in the cache. This method removes all variants of the cached content.

  • Refresh - Fetches requested content from the application, even if cached content is available. The content previously stored in the cache is replaced with a new version from the application. This method affects only one variant of the cached content.

  • Ban - A reference to the cached content is added to a blacklist (or ban list). Client requests are then checked against this blacklist, and if a request matches, new content is fetched from the application, returned to the client, and added to the cache. This method, unlike purge, does not immediately remove cached content from the cache. Instead, the cached content is updated after a client requests that specific information.

Source: en.wikipedia.org

Q13. Name some Cache Writing Strategies ?

Answer : There are two common strategies to write data in a cache:

  1. Pre-caching data, for small pieces of data, usually during the application initialization, before any request.
  2. On-demand, checking first if the requested data is in the cache (if the data is found, it is called a cache hit), using it, improving the performance of the application. Whenever the requested data has not been written to the cache (cache miss), the application will need to retrieve it from the slower source, then writing the results in the cache, thus saving time on subsequent requests for the same data.

Source: eximiaco.tech

Q14.What are the dependencies in Cache and the types of dependencies?

Answer :  When you add an item to the cache, you can define the dependency relationships that can force that item to be removed from the cache under specific activities of dependencies. For example, if the cache object is dependent on a file and when the file data changes, you want the cache object to be updated. Following are the supported dependencies:

  • File dependency: Allows you to invalidate a specific cache item when a disk based file or files change.
  • Time-based expiration: Allows you to invalidate a specific cache item depending on a predefined time.
  • Key dependency: Allows you to invalidate a specific cache item when another cached item changes.

Source: codeproject.com

Q15. What is scavenging?

Answer : When the server running your ASP.NET application runs low on memory resources, items are removed from cache depending on cache item priority. Cache item priority is set when you add an item to cache. By setting the cache item priority controls, the items scavenging are removed according to priority.

Source: codeproject.com

100 points
3