What is in memory caching
It uses server memory RAM to store cache data. Useful for single server or multiple servers with sticky session. They store as objects and can be readily accessed. They are fast operations. There is risk of losing cache in case of server crash
Core doesn’t automatically evict cache on GC pressure. Developer must take care of the expiration and size to control cache growth.
What is sticky session
Requests are always router to same server for processing
What is distributed caching
External service called distributed cache is used to store cache that is shared across multiple servers. Any server can handle the client request and serve cache.
The provider engines available are Redis Sql server postgres and NCache. Redis is the most performant
They are slower due to network trips but durable and persistent. They improve performance and scalability especially with cloud
What is hybrid cache
It gives the best world of both memory and distributed caching. It gives two level caching. Memory caching is primary and distributed is security. This way speed of memory caching plus durability of distributed caching it gets
It gives stampede protection by making all other requests wait for the first request to populate cache
Serialisation can be configured to be string byte json and xml etc and doesn’t have to be done manually
Compare memory Vs distributed caching
Memory caching stores cache in server Memory and distributed does on external store like Redis SQL server etc. Memory caching stores as objects which can be readily accessed and distributed does as bytes which developer has to manually serialise and deserialize. Memory caching is faster than distributed due to network trips. However Memory caching isn’t durable because they will be wiped out on server crash or app pool refresh. Distributed is durable and persistent as it survives server restarts and app deployments. Distributed ensures consistency as single source of truth for the cache rather than multiple servers with sticky session.
Both store as key value pair. Both are services resolved by DI. Both doesn’t offer stampede protection
What does response caching middleware
It enables HTTP headers based caching and is client driven. It follows the caching strictly. Even for GET and HEAD, it can cache only if conditions are met.
The cache is stored server side but driven by client headers.
Why response caching not useful for UI apps
Browsers generally set request headers that prevent caching
Output caching that is form dotnet 7 is helpful because it can decide independently of headers
What is caching
The process of storing frequently accessed data in a temporary store to serve readily rather than refetching or recalculating every single time. The first request response time is slow but it improves as it goes with the cache build up.
We trade data freshness for performance in speed
Why caching
Improve performance and scalability. It reduces the amount of work required and hence able to serve more requests to improve scalability.
The number of DB round trips get reduced reducing network cost and DB load is reduced. Hence the performance is improved in latency a cache data is readily available.
What can benefit from caching
Data that changes infrequently and expensive to calculate
Is caching a functionality and a source of truth
It is only a performance aspect. The app must work exactly the same in functionality without crashing. A cache failure shouldn’t make the app crash
Cache is not a source of truth DB is . It is only for optimization and performance
Why sticky session
Otherwise there will be cache inconsistencies
What is cache aside pattern
Serve using cache always if hit but if miss then fetch from source, populate the cache and return result. It guarantees availability
This technique assumes that cache is empty or evicted to avoid null exceptions failures or stale data.
When does memory cache get cleared
Deployment, restart, GC, eviction
What is cache stampede
What problem could cache aside pattern face
Multiple concurrent requests get a cache miss and get into a race condition. All the requests create a spike in load hitting DB at the same time.
Use double locking or lazy for cache key.
What is the library for memory cache
Microsoft.Extensions.Caching.Memory is better integrated than System.Runtime.Caching
What to do when repopulating cache
Set options - expiration and size
But make sure every cache has a size in that case otherwise app will throw.
Eg : EF core if using the same cache but doesn’t provide size
Why is memory issue happening with cache
Memory is limited but runtime doesn’t enforce Memory pressure (app low on memory) on Memory cache interface.
Developer has to make sure to limit growth by setting expiration strategy and size. Call compact or removed when memory is limited.
What is expiration. What are the strategies
Absolute expiration- the cache is evicted at a fixed date time. Like today 4 PM
Sliding expiration- it resets the time window each time the cache is accessed. It ensured the frequently accessed stays and only unused for long time gets evicted.
So say time is set as 3 seconds. It gets evicted after 3 seconds. If it is accessed within that then it stay for 3 more seconds
Relative Expiration: cache gets evicted in a set time from the time it is created. Like an 1 hour from now
Can cache store null
No unless there is strategy for null caching like product not found for 1 min to avoid cache stampede
Ways to mitigate cache stampede
Double locking, lazy, random expiration, background refresh.
What is null caching
To cache ‘not found’ as result to prevent repeated missed and cache stampede
Difference between expiration and invalidation of cache
Expiration is time based eviction through absolute sliding or relative. It is done using memoryoptions object.
Invalidation is when something outside of cache has changed and hence the cache entry is no longer valid, hence evict. Like user might update a record in DB so remove the old cache entry. Or some background service signals that the data has changed. It is done manually using remove or compact.
What are the problems with absolute and sliding expiration. What to do.
If only sliding expiration, then risk of item never expiring if it is repeated accessed in the interval.
Combine both to make sure active items stay but also ensure nothing lives forever. Absolute expiration sets upper bound on how long an item can live but also let it expire if not accessed within the interval set by sliding. If EITHER sliding interval or Absolute expiry time passes then item evicted