Redis  

Cache, Local Storage, and Modern Caching Techniques

Local storage and caching are essential concepts for modern web applications. They help applications store and retrieve data efficiently, improve performance, and provide a smooth user experience.

In this article, we’ll explore the differences between cache, session, and cookies, explain caching techniques, and show how you can apply these concepts in practice.

Local Storage vs Session Storage

Local Storage:
Local storage allows you to store data directly in the browser. It persists even after the user refreshes the page or closes the browser. It’s ideal for saving user preferences, tokens, or other data that your application needs to access frequently.

Session Storage:
A session represents a temporary interaction between a user and the application. Session storage stores data on the server for the duration of the session. Once the user closes the browser or logs out, this data is discarded.

Cache / Local StorageSession Storage
Stored on client (browser)Stored on server
Persists after browser closesDeleted after logout or timeout
Improves performanceDoes not affect performance much

Cookies vs Session Storage

Cookies are small pieces of data stored in the client's browser to identify users or persist information. Each HTTP request includes cookies, which the server uses for authentication or personalization.

CookiesSession Storage
Stored on client, sometimes on serverStored temporarily on the server
Used for tracking or identificationExists only during the session
Expires based on defined durationDeleted after session ends

Cache vs Cookies

Caching is not the same as cookies. Cache stores frequently accessed data to reduce server/database requests and improve performance. Cookies store small user-specific data that may be needed occasionally.

CacheCookies
Stores frequently used dataStores user info (ID, preferences)
Cleared manually or based on strategyExpires automatically
Stored only in the client/browserStored on client and/or server

How Memory Caching Works

When a web application receives a request, it first checks if the required data is in the cache (RAM).

  • Cache Hit: Data is found in the cache → faster response.

  • Cache Miss: Data is not in cache → fetch from the server or database and store in cache for next time.

Modern .NET applications often use caching libraries such as MemoryCache or distributed caches like Redis for performance-critical systems.

Example:

var cache = new MemoryCache(new MemoryCacheOptions());
if (!cache.TryGetValue("UserProfile", out UserProfile profile))
{
    profile = dbContext.GetUserProfile(userId);
    cache.Set("UserProfile", profile, TimeSpan.FromMinutes(30));
}

Caching Techniques

  1. Write-Through Cache (Synchronous): Updates the cache and database simultaneously. Useful when consistency is critical. Example: implementing IWriteThruProvider in .NET.

  2. Write-Behind Cache (Asynchronous): Updates the cache immediately and writes back to the database later. Reduces latency but risks temporary inconsistency.

  3. Write-Around Cache: Writes go directly to the database; the cache is updated only when the data is next read. Ideal for rarely accessed data.

Cache Eviction Strategies

Since cache memory is limited, eviction strategies determine which data to remove when space is needed:

  • Least Recently Used (LRU): Removes the oldest accessed data first.

  • Least Frequently Used (LFU): Removes data with the fewest accesses first.

In distributed caching scenarios, you may also use time-based expiration or sliding expiration policies.

Conclusion

Caching and storage strategies are fundamental to application performance. Understanding the differences between cache, session storage, and cookies and applying proper caching techniques can dramatically improve the responsiveness and scalability of your web application.