Cache Flashcards

1
Q

remember: network calls are…

A

blocking

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

primary use case for caching

A

you find yourself frequently accessing the same data from a remote server.

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

define a cache ‘miss’

miss-rate

A

data is requested from cache, and it doesn’t have it

miss-rate: the percentage of requests that were misses

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

define a cache ‘hit’

hit-rate

A

data is requested from cache, and it has the data

hit-rate: the percentage of requests that were hits

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

in Guava cache framework, the 3 core classes

A

LoadingCache
CacheBuilder
CacheLoader

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

Loading Cache (Guava framework)

A

This class manages a cache, keeping objects in a Map and automatically retrieving new data from the data store on a cache miss

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

CacheBuilder (Guava framework)

A

This factory class creates properly configured instances of LoadingCache

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

CacheLoader (Guava framework)

A

LoadingCache uses a CacheLoader that we configure to retrieve new data from the data store.

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

high miss-rates indicate that…

A

…many requests make the expensive request AND add overhead to check and update the cache.

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

Cache is most helpful when…

A

… the system requests the same information repeatedly. You benefit from the cache only if you request the same data again and resuse the previous result.

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

Programs with high miss-rates may improve the cache performance by…

A

…adjusting the cache configuration size
adjusting the cache TTL
evaluating if users reuse the same data
“sweet spot”

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

amount of RAM that Guava defaults to use

A

Guava defaults to using all available RAM. Devs always set an upper limit on the amount of memory the cache can use, via the CacheBuilder’s method maximumSize()

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

maximumSize()

A

method in Guava’s CacheBuilder that sets the maximum amount of memory the cache will consume, in number of entries (of the pair)

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

where is maximumSize() set?

A

this is set in your CustomizedCacheDao constructor:

public CustomCacheDao(DataAccessDao accessDao)
loadingCacheVariable = CacheBuilder.newBuilder()
.maximumSize(50)
.build(CacheLoader.from(accessDao::getData)
);

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

how does Cache evict data?

A

When the cache already contains the maximum number of entries, and we request a new key, the cache evicts the least recently used entry to make space.

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

staleness

A

Data that is out of date and no longer valid. The data has changed on the remote source, and is different from what we have in our cache.

17
Q

cache invalidation

A

difficult process of trying to remove stale data

18
Q

Guava’s expire based on time methods

A

in CacheBuilder:
.expireAfterAccess(6, TimeUnit.HOURS)
or
.expireAfterWrite(6, TimeUnit.HOURS)

19
Q

Guava’s expire based on time methods

A

in CacheBuilder:
.expireAfterAccess(6, TimeUnit.HOURS)
or
.expireAfterWrite(6, TimeUnit.HOURS)

used when instantiating the cache, just like .maximumSize() and .build(CacheLoader.from(accessDao::getData)

20
Q

When a cache entry is expired…

A

…Guava will re-load the information from the remote data store even on a cache hit.

21
Q

expired data term

A

In caching terminology, the maximum “age” is called Time To Live (TTL), as in “ the item will live in the cache for this amount of time”

22
Q

TTL

A

depends of your application’s need for fresh data. A tradeoff between hit-rate and staleness

23
Q

.expireAfterAccess(num, TimeUnit.unit)

A

specifies the amount of time (num) that can elapse after the last time an item in the cache has been accessed before it is invalidated.
“access” refers to writing to the cache and reading from the cache.
As long as the callers request the item more often that that TTL, it won’t be invalidated.
(for num=6, if the cached item hasn’t been used in 6 hours it will be invalidated)
example: frequent items in cart

24
Q

.expireAfterWrite(num, TimeUnit.unit)

A

invalidates an item in the cache a specified amount of time after it was written to the cache ragardless of how recently it was read.
example: movie recommendations (want to forward recent trends to the customer)

25
Q

Data that remains relatively static

A

product description
addresses
informational type of records
–>longer TTL

26
Q

Data that remains relatively static

A

product description
addresses
informational type of records
–>longer TTL
if data has a low chance of becoming stale, or has a minimal impact when stale
–>use .expireAfterAccess with a long duration

27
Q

Data that has a high liklihood of change

A

product ratings
prices
popular products
–>lower TTL
for data that changes frequently, or which is more sensitive to staleness
–>use .expireAfterWrite with shorter duration

28
Q

If data has the potential to have a negative impact when it becomes stale, or that it might become stale more often…

A

…use .expireAfterWrite

We shouldn’t make the problem worse by continuing to extend its life each time someone requests it (.expireAfterAccess)