Caching Flashcards

(62 cards)

1
Q

What is in memory caching

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is sticky session

A

Requests are always router to same server for processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is distributed caching

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is hybrid cache

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Compare memory Vs distributed caching

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does response caching middleware

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why response caching not useful for UI apps

A

Browsers generally set request headers that prevent caching
Output caching that is form dotnet 7 is helpful because it can decide independently of headers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is caching

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why caching

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What can benefit from caching

A

Data that changes infrequently and expensive to calculate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is caching a functionality and a source of truth

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why sticky session

A

Otherwise there will be cache inconsistencies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is cache aside pattern

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When does memory cache get cleared

A

Deployment, restart, GC, eviction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is cache stampede
What problem could cache aside pattern face

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the library for memory cache

A

Microsoft.Extensions.Caching.Memory is better integrated than System.Runtime.Caching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What to do when repopulating cache

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is memory issue happening with cache

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is expiration. What are the strategies

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can cache store null

A

No unless there is strategy for null caching like product not found for 1 min to avoid cache stampede

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Ways to mitigate cache stampede

A

Double locking, lazy, random expiration, background refresh.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is null caching

A

To cache ‘not found’ as result to prevent repeated missed and cache stampede

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Difference between expiration and invalidation of cache

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are the problems with absolute and sliding expiration. What to do.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the issue with size limit in memory cache.
If even one cache is to be set with size limit then every entry must have it otherwise the app throws. Hence only do it when you fully own the cache. If external framework use the same and doesn't set size then app throws. Thus never on DI provided shared cache. To mitigate, create a Singleton dedicated memory cache with size limit in options
26
What is unbounded key. What is the issue
Unbounded keys that are not fixed length such as user input. The key size is arbitrary and dangerous to spams that create memory spike. We have to normalize dynamic keys like user id, product id. Like lower case, encode, trim or else same thing becomes duplicate keys Cache keys must be unique, predictable, and reflect the critical dimensions that affect the data. Prefix keys by module/entity, normalize dynamic parts, consider TTL, and use hashes for very long queries. Otherwise, you risk collisions, cache misses, memory bloat, stale data, and even security leaks
27
How to monitor cache
Dashboard and logs to analyse cache hits and misses. Evictions
28
How to set size on memory cache
In options set the size limit which is going to be maximum one entry can hold. And each time adding a cache, set the weight of that cache
29
What exactly is size limit
It is a logical capacity. Say set it as 1000 as no of entries then each entry give a weight of 1 with set size then sum of entries can't exceed this limit 1000. Or if it is string length max and each entry sets it length of string entry. Overall entries can't exceed the maximum allowed characters in limit. It doesn't have defined unit of measure because there is no mechanism to measure size of entries by cache engine. The developer can choose the units. It can be number of entries, string length, bytes etc
30
What is shared memory cache. What issue arises
Service.AddMemoryCache() sets up the default IMemoryCache which the app code and third party frameworks share. Hence the size limit set size issue because developers don't have full control and frameworks don't set size because developer has the choice to choose unit of measure. With size limit set in options and if not all entries have size entry unit then app throws. So it is recommended that developer create a single caching for just the code to have full control on growth limit
31
Write code to check if value is present in memory cache
Memcache.TryGetValue (cachekeys.entry, out datatype cachevalue)
32
Difference between remove and compact
Remove removes a specific key Compact removes 25% of cache entries except that has priority as neverremove. The order is all expired then low priority then least recently used then earliest absolute then earliest sliding. So oldest leave first.
33
Is it possible to create hierarchy in caching
Yes. We can make one cache entry depend on another by using cancellationtokensource and cancellationchangetoken. If parent is evicted then child is automatically evicted Like a product list as parent and then individual product entires are related. So create cancellationtokensource for dependency which is parent. And pass it as cancellationchangetoken to child. When we call cancel() om token source, all child is evicted with parent. Cancellationtokensource can also take a timeout
34
Why core doesn't automatically evict. What's lazy removal
Constant background scanning will waste CPU usage. Lazy removal only checks the expiry when accessed. This has minimum cost
35
Why is request getting slow even with caching
Expensive time consuming cache computations during request scope Better to move it to background service to do work Asynchronously and populate cache when ready. It prevents race condition or cache stampede of multiple requests trying to update cache
36
What is Redis
Open source in memory data store used as distributed cache. By Azure available
37
How to configure redis for distributed cache
Create azurecache for redis. It gives you a connection string, add it to configuration. Save it in secret manager for development and Azure key vault for production. In services add stackexchangerediscache with options configuration with connection string and instance name.
38
What is distributed memory cache
It is not actually a distributed store. It is useful in development and testing environment where we just want to use app memory instead of distributed provider. Also useful when currently it is one server prod which can move into multiple servers for future. It allows implementing in the way we would a distributed cache. Do a services.AddDistributedMemoryCache()
39
Why not to use sql server for caching if database is also in it
Redis has better throughput and lower latency. Both cache and data in sql server can impact performance of both and not robust if database crash or outage. Recommended to use independent instance of it for cache
40
How is Serialisation done
Object is serialise using json Serialisation and that is encoded
41
How to choose provider for distributed
Azure hosting best with redis. Well integrated It is higher throughput and low latency. Check existing infrastructure, performance requirements, cost, developer skills
42
How to specify cache directives
Cache-control header Public means browser and shared cache like proxy cdn can store. Private means only browser can Max age is given in seconds. Doesn't accept response with more age
43
What is response caching
Reduces number of requests a client has to make to server by setting headers and storing cache accordingly. It reduces the amount of work server has to do
44
What sets the caching headers
Response Cache attribute. It sets those that client must follow. It can be globally applied
45
VaryByQuery
Varies response stored by values in the list of queries. If * then varies by all query parameters
46
Difference between no store and no cache
No cache means Cache can store response but revalidate with server to reuse like if none match and if modified since. Server responds with 304 not modified to reuse or 200 OK with response to be stored No store means caching not allowed. Both request and response can't be stored
47
Vary header
Cached response shouldn't be sent unless all the headers and it's value mentioned in vary match in cached response original request
48
Does server load reduce by caching
Not really. Server still has to respond but data is readily available without query. It reduces load on DB. Reduces latency and network overhead
49
Response caching vs output caching
Response caching strictly allow HTTP caching rules and server can't override it. It is client driven headers and server only stores and serve. It can fail if conditions are not met like no store, POST etc. There is no developer control. Output caching gives developer control to override http header rules and store cache on server based on it's on policy. Response caching can only use memory cache. Output cache can plug in providers like redis for distributed caching. It can give per user cache policies, cache partitioning
50
Why varybyquery, varybyheader throwing error
Response caching middleware must be enabled because there isn't a corresponding header in http. It is middleware specific Both addresponsecaching and useresponsecaching
51
Tell the mechanism of varybyquery
Middleware only serves cached response if query string and its value matches previous request. If request comes in with new value for it then server response
52
NoStore and Location.None
No-store, no-cache set if both true. If nostore false and location none then only no cache
53
Error page getting cached
Put location none and nostore true in responsecache attribute
54
How to enable caching in response caching
Duration should be positive Location any means public or client means private Private means even middleware can cache the value. For public it can
55
What are cache profiles
Configure in addcontrollers for cache profiles and reuse as default . Explicit mentioned will override. Or rest in attribute mention the cache profile
56
What shouldn't be cached
Responses that depend on user identity or required users to login Auth tokens, PHI
57
What doesn't get cached by response caching middleware
Authorization, set cookie, post, put , patch delete Location none , nostore Vary as *. It has to be explicit Content length must match size of response Expires, max-age valid. Smaller than maximumbodysize configured
58
What provider response caching middleware uses
IMemorycache. Limited capacity. It compacts when exceeded.
59
Why is it to difficult to test caching behaviour
Browser sets headers to prevent caching like when refreshing page like no cache max age 0. Fiddler can explicitly set headers and preferred for testing caching
60
ETag mechanism
For specific version of response body, an unique identifier called entity tag etag is issued by server. Next time client send it in If none match. If match then 304 If no match means 200 because tag got changed for the mapped response. Network payload save because 304 goes without body.
61
If modified since mechanism
It checks with server with the timestamp of cache response
62
Explain caching patterns
Cache-Aside (a.k.a. Lazy Loading) → Application checks cache first, if miss → load from DB → store in cache. Simple, most common. Read-Through → Cache itself knows how to fetch from DB on a miss. App always talks to cache. Write-Through → When writing, app writes to cache, and cache syncs DB. Write-Behind / Write-Back → App writes to cache, cache updates DB asynchronously later. Refresh-Ahead → Cache refreshes entries before they expire (proactively).