What is Caching?

๐Ÿ’ก Concept Name

Caching โ€“ The process of storing copies of data in a temporary storage location for faster access.

๐Ÿ“˜ Quick Intro

Caching improves performance by temporarily storing frequently accessed data in faster storage (e.g., memory), reducing the need to fetch or compute the same data repeatedly.

๐Ÿง  Analogy / Short Story

Think of caching like putting your frequently used tools in a desk drawer instead of the garage โ€” much quicker to reach and use them when needed.

๐Ÿ”ง Technical Explanation

  • โšก Reduces latency and load on databases, APIs, and file systems.
  • ๐Ÿง  Uses RAM or in-memory data structures for fast retrieval.
  • โ™ป๏ธ Can be local (in-memory), distributed, or persistent.
  • ๐Ÿ› ๏ธ Often used with key-value pairs (e.g., Dictionary, Redis).
  • ๐Ÿ•’ Requires cache invalidation policies like LRU, TTL.

๐ŸŽฏ Purpose & Use Case

  • โœ… Speeding up web APIs and database queries.
  • โœ… Storing configuration or frequently queried data.
  • โœ… Reducing server load in high-traffic applications.

๐Ÿ’ป Real Code Example

// Example using IMemoryCache in .NET
public class DataService
{
    private readonly IMemoryCache _cache;

    public DataService(IMemoryCache cache)
    {
        _cache = cache;
    }

    public string GetUserData(int userId)
    {
        return _cache.GetOrCreate("User_" + userId, entry =>
        {
            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
            return GetFromDatabase(userId);
        });
    }

    private string GetFromDatabase(int id) => $"User Data for {id}";
}

โ“ Interview Q&A

Q1: What is caching?
A: A way to store and reuse data quickly without fetching it repeatedly from the original source.

Q2: Why use caching?
A: To reduce latency and improve application speed.

Q3: What are some common caching tools?
A: Redis, MemoryCache, NCache, Memcached.

Q4: What is cache invalidation?
A: The process of clearing or updating outdated cache data.

Q5: What are TTL and LRU?
A: TTL: Time-To-Live, LRU: Least Recently Used โ€“ both are cache eviction policies.

๐Ÿงพ Cache Writing Strategies

  • Write-Through: Writes to cache and backing store simultaneously. Ensures consistency.
  • Write-Around: Writes directly to store, cache is updated only on next read.
  • Write-Back (Write-Behind): Writes to cache first, then persists later. Improves write speed, risk of loss.
  • Read-Through: Automatically loads data to cache on miss and returns it.
  • Refresh-Ahead: Proactively updates cache before it expires.
  • Write-Once: Data is cached once and never updated (for immutable data).

๐Ÿšซ Cache Invalidation

Cache Invalidation is the process of removing or updating outdated data from the cache to ensure the application returns fresh and accurate information.

  • ๐Ÿงน Manual Invalidation: Developer explicitly clears or updates the cache.
  • ๐Ÿ•’ Time-based (TTL): Cached data expires after a set duration (Time To Live).
  • โ™ป๏ธ Event-based: Cache is updated when data source changes (e.g., database triggers).
  • ๐Ÿ‘€ LRU (Least Recently Used): Evicts old or least-accessed entries when space is needed.
  • ๐Ÿ“‰ Stale-while-revalidate: Serves stale content while refreshing cache in background.
  • โš ๏ธ Incorrect or missing invalidation leads to stale data bugs.

๐Ÿ“ MCQs

Q1. What is caching used for?

  • Storing logs
  • Security
  • Faster access to data
  • Authorization

Q2. Where is cache usually stored?

  • On disk
  • On network
  • In-memory
  • In database

Q3. Which is a distributed cache system?

  • LINQ
  • ASP.NET
  • Redis
  • Entity Framework

Q4. What happens when cache expires?

  • App crashes
  • Data is lost
  • Fresh data is retrieved
  • Nothing

Q5. What’s a con of caching?

  • Slower apps
  • More memory
  • Can serve stale data
  • Security risk

Q6. What is cache invalidation?

  • Writing logs
  • Restarting server
  • Clearing or updating stale data
  • Refreshing UI

Q7. Which pattern fits caching best?

  • Relational
  • Key-value store
  • Graph
  • Queue

Q8. Which .NET interface supports caching?

  • IDatabase
  • IMemoryCache
  • ICacheable
  • ICacheService

Q9. What policy removes oldest data?

  • FIFO
  • TTL
  • LRU
  • GC

Q10. Which caching policy is time based?

  • LRU
  • TTL
  • MRU
  • CLR

๐Ÿ’ก Bonus Insight

While caching boosts performance, always balance freshness vs. speed. Use eviction and invalidation wisely.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

โฌ…๏ธ Previous:

โžก๏ธ Next:

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: