Skip to main content

Caching

Caching is one of the most important performance optimization techniques in modern software engineering.

Overview​

Caching stores frequently accessed data in a faster storage layer to reduce latency, improve performance, and decrease backend load.

BenefitDescription
Faster Response TimeReduces latency
Reduced Database LoadFewer database queries
Improved ScalabilityHandles more users
Lower Infrastructure CostLess CPU and DB usage
Better User ExperienceFaster page loads
Increased AvailabilityBackend failures may be hidden temporarily

Types of Caching​

TypeExamples
Client-SideBrowser cache, LocalStorage, Service Workers
Server-SideRedis, Memcached, in-memory dictionaries
Database CachePostgreSQL shared buffers, MySQL query cache
CDN CacheCloudflare, Akamai, AWS CloudFront

High-Level Caching Architecture​


Cache Terminology​

TermMeaning
Cache HitData found in cache
Cache MissData not found in cache
EvictionRemoving old cache entries
TTLTime To Live
Warm CachePreloaded cache
Cold CacheEmpty cache
InvalidationRemoving stale data

Cache Eviction Policies​

PolicyBest For
LRU (Least Recently Used)General-purpose caching
LFU (Least Frequently Used)Predictable workloads
FIFO (First In First Out)Simple queues
Random ReplacementSimple but less optimal

Cache Strategies​

Cache-Aside Pattern (Lazy Loading)​

Most common caching strategy.

public async Task<Product> GetProductAsync(int id)
{
string key = $"product:{id}";

var cached = await redis.GetStringAsync(key);

if (cached != null)
{
return JsonSerializer.Deserialize<Product>(cached);
}

var product = await db.Products.FindAsync(id);

await redis.SetStringAsync(
key,
JsonSerializer.Serialize(product),
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
});

return product;
}

Write-Through Cache​

Data written to cache and database simultaneously.

Advantages: Consistent cache, fast reads. Disadvantages: Slower writes.


Write-Behind Cache​

Writes first go to cache, database updated asynchronously.

Advantages: Very fast writes, reduced DB pressure. Disadvantages: Risk of data loss.


In-Memory Caching​

ASP.NET Core — IMemoryCache​

builder.Services.AddMemoryCache();
public class ProductService
{
private readonly IMemoryCache _cache;

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

public Product GetProduct(int id)
{
return _cache.GetOrCreate($"product:{id}", entry =>
{
entry.AbsoluteExpirationRelativeToNow =
TimeSpan.FromMinutes(10);

return LoadProductFromDatabase(id);
});
}
}

Advantages: Extremely fast, simple setup.

Disadvantages: Not shared between servers, lost after restart.


Distributed Caching​

Distributed cache shared across multiple application servers.


Redis​

The most popular distributed caching system.

FeatureDescription
In-memoryExtremely fast
PersistenceOptional disk storage
Pub/SubMessaging support
ReplicationHigh availability
Data StructuresStrings, lists, sets, hashes
TTL SupportAutomatic expiration

Redis Data Types​

TypeExample Use
StringUser profile
HashObject fields
ListQueues
SetUnique values
Sorted SetRankings/leaderboards

Redis CLI​

SET user:1 "John"
GET user:1
EXPIRE user:1 300
TTL user:1
DEL user:1
INCR counter

ASP.NET Core — Redis Integration​

builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
});

Cache Invalidation​

Cache invalidation is one of the hardest problems in computer science.

Strategies​

StrategyMethod
TTL ExpirationAutomatic removal after a time limit
Manual Invalidationawait redis.RemoveAsync("product:1")
Event-BasedInvalidate after product update, order creation, etc.

Cache Consistency Problems​

ProblemDescriptionSolution
Stale DataCache contains outdated informationShort TTL or event-based invalidation
Race ConditionsMultiple updates overwrite each otherLocks or atomic operations
Cache StampedeMany requests miss cache simultaneouslyMutex locking, request coalescing, early expiration

Distributed Cache Challenges​

ProblemDescription
Network LatencyRemote cache calls
Serialization CostObject conversion overhead
ConsistencySynchronization issues
FailuresCache server downtime
ScalingCluster management

Cache Partitioning (Sharding)​

Large cache distributed across multiple nodes using Consistent Hashing to distribute keys evenly.


Multi-Level Caching​


HTTP Caching​

HeaderPurpose
Cache-ControlCache rules
ExpiresExpiration date
ETagVersion identifier
Last-ModifiedTimestamp validation
Cache-Control: public, max-age=3600
ETag: "abc123"

Performance Optimization​

PracticeWhy
Use TTLsPrevent stale data
Monitor hit rateMeasure effectiveness
Use distributed cache for scalingMulti-server support
Cache expensive queriesImprove performance
Avoid over-cachingReduce complexity
Gracefully handle failuresImprove resilience

Common Pitfalls​

PitfallProblem
Caching everythingMemory waste and complexity
Missing invalidation strategyStale data bugs
Huge cache objectsHigh serialization cost and memory pressure
Ignoring cache failuresApplication breaks without cache

Cache Strategies Comparison​

StrategyRead SpeedWrite SpeedComplexity
Cache AsideFastNormalMedium
Write ThroughFastSlowerMedium
Write BehindFastFastHigh
Read ThroughFastMediumHigh

Interview Questions​

Beginner​

  1. What is caching?
  2. What is cache hit vs miss?
  3. What is TTL?

Intermediate​

  1. Explain the cache-aside pattern.
  2. What is distributed caching?
  3. How does Redis work?
  4. What is cache invalidation?

Advanced​

  1. How do you prevent a cache stampede?
  2. Explain consistent hashing.
  3. Compare Redis vs Memcached.
  4. Design a scalable caching architecture.

Learning Roadmap​