Skip to content
Shop

CommunityJoin Our PatreonDonate

Sponsored Ads

Sponsored Ads

Caching

You will learn how data is stored temporarily for faster retrieval

Brian Long
Senior Developer

What is a Cache?

A cache is backup copy of data. Backups can be created on the server side or the client side. The server asks the client for data using the API and the API triggers some code to get the data from a database and then returns it as JSON. Some data is saved for later retrieveal on the client (ketchup on the table) or sometimes on the server (condiments).

What is a Data Store?

Types of Cache

Caching Types:

Cache-Aside (Lazy Loading):

Most common strategy. Application checks the cache first for data. If a hit (data found), it's returned directly. On a miss (data not found), the database is queried, data is retrieved and cached, then returned. Advantages: Efficient cache usage, simple implementation. Disadvantages: Extra database query on miss.

Browser Cache: Stores frequently accessed web content (HTML, CSS, JavaScript, images) locally on the user's device. Improves website performance by reducing server load and download times for repeat visits. Uses browser-specific storage mechanisms like IndexedDB and LocalStorage.

Database Caching: Caches frequently accessed database queries or results to reduce database load and improve application responsiveness. Can be implemented within the database itself or as a separate layer.

CDN Caching: dContent Delivery Network (CDN) caches static content (images, videos, etc.) geographically distributed servers. Reduces latency for users by serving content from the closest server.

Application Caching: Caches data within the application code to avoid redundant calculations or database queries. Improves application performance for frequently used data.

Distributed Cache: Stores cached data across multiple servers for scalability and high availability. Improves performance for applications with many users or geographically distributed deployments.

Write Back: A cache update strategy where data is written to both the cache and the primary data source (database) simultaneously. Ensures data consistency, but can impact performance due to double writes.

Write-Around: A cache update strategy where data is only written to the cache on updates. Faster than write-back, but data in the primary source might become stale.

Caching Strategies

Read Through Caching Strategy:

When reading data, the cache is checked first. On a miss, the primary data source is queried and the data is populated in both the cache and returned to the application. Simplifies application logic but can lead to unnecessary writes if data is not frequently modified. Virtual Memory Caching:

  • Operating system technique that uses a portion of disk space to cache frequently accessed data from main memory (RAM).
  • Improves performance by reducing the need to access slower physical storage.

Write-Back Cache:

  • Type of cache that allows writes to occur in the cache first, with delayed updates to the primary data source.
  • Improves performance for write-heavy workloads.

Database Cache (see Database Caching):

  • Explained above.

Cache Avalanche:

  • A phenomenon where a surge in cache misses leads to a large number of concurrent requests to the primary data source, overwhelming it and causing performance degradation.
  • Can be mitigated by using appropriate cache invalidation strategies and setting cache expiration times.

File System Cache:

  • Operating system mechanism that caches recently accessed files to speed up subsequent reads.

In-Memory Cache:

  • A type of cache that stores data in RAM for very fast access times.
  • Ideal for frequently accessed data but limited by RAM capacity.

Memcached, Redis:

  • Popular open-source in-memory data stores often used for application caching.
  • Offer high performance and scalability for caching needs.

Browser Storage Mechanisms:

IndexedDB:

Client-side JavaScript API for persistent storage of large amounts of structured data (like databases) on the user's device. More robust than LocalStorage for complex data storage needs. LocalStorage:

Client-side JavaScript API for simple key-value pair storage on the user's device. Data persists even after browser closure, but limited storage capacity compared to IndexedDB.