All right, let's discuss 2.e Caches.

  • Definition: A cache is a hardware or software component that stores data so that future requests for that data can be served faster. The data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere (e.g., in a database). Caching is a fundamental technique for improving performance in computer systems.

  • How it Works:

    1. A client requests some data.
    2. The system first checks the cache to see if the data is already stored there (a "cache hit").
    3. If the data is in the cache (cache hit), it's returned to the client quickly.
    4. If the data is not in the cache (a "cache miss"), the system retrieves the data from the original source (e.g., a database), stores a copy in the cache, and then returns the data to the client.
    5. Subsequent requests for the same data will be served from the cache (until the data is evicted or invalidated).
Advertisement
  • Key Concepts:

    • Cache Hit: The requested data is found in the cache.
    • Cache Miss: The requested data is not found in the cache.
    • Cache Eviction Policies: Determine which data to remove from the cache when it's full. Common policies include:
      • LRU (Least Recently Used): Evicts the data that hasn't been accessed for the longest time.
      • FIFO (First-In, First-Out): Evicts the data that was added to the cache first.
      • LFU (Least Frequently Used): Evicts the data that has been accessed the fewest times.
    • Cache Invalidation Strategies: Determine when data in the cache should be updated or removed because it's no longer valid.
      • Write-Through Cache: Data is written to both the cache and the underlying storage (e.g., database) simultaneously. Ensures strong consistency but can be slower.
      • Write-Back Cache: Data is written only to the cache initially. The write to the underlying storage is delayed. Faster but can lead to data loss if the cache fails before the data is written to storage.
      • Write-Around Cache: Data is written directly to permanent storage, bypassing the cache. Reduces cache pollution.
    • Cache Expiration (TTL - Time To Live) Setting a time limit for how long data remains valid in the cache.
  • Types of Caches:

    • Client-Side Caching: Browsers cache web pages, images, and other resources to reduce page load times.
    • CDN (Content Delivery Network): A geographically distributed network of servers that cache static content (e.g., images, videos, CSS, JavaScript) closer to users, reducing latency.
    • Server-Side Caching:
      • In-Memory Caches: Store data in the server's RAM (e.g., Redis, Memcached). Very fast but data is lost if the server restarts.
      • Database Caching: Databases often have their own internal caches to speed up query execution.
      • Application Level Caching: Caching data within the code itself.
Advertisement
  • Benefits of Caching:

    • Reduced Latency: Faster response times for users.
    • Reduced Load on Servers: Fewer requests need to be handled by the backend servers.
    • Improved Performance: Overall system performance is improved.
    • Reduced Network Traffic: Less data needs to be transferred over the network.
    • Cost Savings: Can reduce infrastructure costs by reducing the load on servers and databases.
  • Example: A news website might cache the front page content to serve it quickly to many users. When a user visits the front page, the web server checks the cache. If the content is in the cache (cache hit), it's returned immediately. If not (cache miss), the server retrieves the content from the database, stores it in the cache, and then returns it to the user.

Advertisement