Caching Flashcards

(20 cards)

1
Q

What is in memory caching and when would you use it?

A

Data stored in the memory of the computer (which is faster than disk).

Uses - frequently accessed data like API responses, session data and web page fragments.

Tools: Memcached or Redis (or custom)

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

What is disk caching and when would you use it?

A

Data stored on the hard disk, which is slower than main memory but faster than retrieving data from its source.

Uses: - data too big for in memory or needed between app restarts, like database queries and file system data.

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

What is a DNS cache and why is it used?

A

Uses: Faster domain to IP resolution

Tools: AWS Route53, Azure DNS, Google Cloud DNS

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

What are CDN caches and how are they used?

A

Uses: Faster restrieval of static content like images, videos, css sheets, static html

Tools: Akamai, CloudFront, ElastiCache, Azure CDN

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

What are webserver caches and how are they used.

A

Uses: Faster retrieval of dynamic web content like api responses or session data or html fragements.

Tools: ElasticCache, CloudFront

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

What are app server caches and how are they used?

A

Uses: Accelerated application performance and data access. Similar to webserver data, but just on the backend: api responses or session data or db queries or tokens.

Tools: Local server cache, remote cache Redis, Memcached, ElastiCache.

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

What are DB caches and how are they used?

A

Uses: Faster access to stored data. Can be in memory or on disk (both faster than query).

Tools: Local DB cache, Remote cache on Redis, Memached, ElastCache

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

What are the three primary caching strategies?

A
  1. Write-through cache: Under this scheme, data is written into the cache and the corresponding database simultaneously.
  2. Write-around cache: This technique is similar to write-through cache, but data is written directly to permanent storage, bypassing the cache.
  3. Write-back cache: Under this scheme, data is written to cache alone, and completion is immediately confirmed to the client. The write to the permanent storage is done after specified intervals or under certain conditions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the tradeoffs with write-through caching strategy?

A

Pros: Complete data consistency between the cache and the storage. If app goes down, data is always saved.

Cons: Slow, duplicating writes every time.

Uses Cases: When consistency is critical but you want to reduce subsequent read latency, like with bank data or bidding data.

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

What are the trade-offs with write around caching strategy?

A

Pros: Cache only on read. Reduces useless cache writes.

Cons: Higher rate of cache misses on non-previously-read data leading to higher latency.

Uses cases: Perfect for rarely read data like high volume logs or large data blobs.

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

What are the tradeoffs with write-back caching strategy?

A

Pros: Low latency writes.

Cons: Data loss in the event of a crash.

Uses: Hugely viral post racking up likes. A loss of 5k likes in the event of an outage is acceptable.

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

What are the main cache invalidation methods?

A
  1. Purge
  2. Refresh
  3. Ban
  4. TTL
  5. Stale-while-revalidate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe purge cache invalidation method.

A

Action: You explicitly tell the cache to immediately delete a specific object (usually by URL).

Result: The very next user who asks for that URL will get a “Miss” and trigger a fetch from the origin server.

Uses: You updated a specific image (hero-banner.jpg) or fixed a typo on a single page (/about-us) and need it fixed instantly.

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

Describe refresh cache invalidation method.

A

Action: The cache fetches a new copy from the origin server, even if the current cached copy hasn’t expired yet.

Result: The cache updates its copy with the latest version from the origin.

Uses: A user manually hits “Reload” (triggering Cache-Control: no-cache), or you need to ensure a specific asset is up-to-date without necessarily deleting it first.

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

Describe ban cache invalidation method.

A

Action: Create a “rule” or “pattern” (e.g., invalidate everything ending in .jpg or invalidate everything related to user_id=99) for invalidation.

Result: Every request references “Ban List.” If a request matches the ban rule, the cached data is considered dead, and a fresh copy is fetched.

Uses: You need to invalidate thousands of related items at once (like clearing all content for a specific category) without knowing every single specific URL.

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

Describe TTL cache invalidation method.

A

Action: You set a lifespan for the data when you create it (e.g., “Expires in 5 minutes”).

Result: The cache counts down. When the timer hits zero, the data acts as if it has been deleted. The next request triggers a refresh.

Use: The “Default” strategy. Great for news feeds, stock prices, or weather data where being 5 minutes old is acceptable.

17
Q

Describe stale-while-revalidate cache invalidation method.

A

Action: Cache simultaneously starts a background process to fetch the new data from the server while serving stale data to user.

Result: User A sees the old content (fast). User B (seconds later) sees the new content.

Uses: High-traffic content where speed is more important than absolute accuracy (e.g., a “Total Views” counter). It hides the latency of the database update from the user.

18
Q

Describe cache-aside, or lazy loading, cache reading.

A

Action: Code implementation reads the cache first. If a record does not exist, it reads from the DB and subsequently places the record in the cache.

Result: The cache is dumb. Has no idea there’s a DB. A little bit complex to implement.

Uses: Complex, aggregated data. Like multiple joins queries combined to show growth percentage or total sales.

19
Q

Describe read-through cache reading.

A

Action: Managed cache reads. All requests go through the cahce and it handles hits and misses.

Result: Logis is DRY, simple, and scalable.

Uses: A bunch of services reading from the same cache as opposed to managing their own lazy loading logic. The read logic is just centralized.

20
Q

Dscribe the most common cache eviction policies.

A

First In First Out (FIFO): The cache evicts the first block accessed first without any regard to how often or how many times it was accessed before.

Last In First Out (LIFO): The cache evicts the block accessed most recently first without any regard to how often or how many times it was accessed before.

Least Recently Used (LRU): Discards the least recently used items first.

Most Recently Used (MRU): Discards, in contrast to LRU, the most recently used items first.

Least Frequently Used (LFU): Counts how often an item is needed. Those that are used least often are discarded first.

Random Replacement (RR): Randomly selects a candidate item and discards it to make space when necessary.

All of these are done on a schedule or on size limits.