In-memory caching stores data temporarily inside the server's memory to quickly reuse it without fetching or computing again. It helps speed up apps by avoiding slow operations.
Click to reveal answer
beginner
Name a common data structure used for in-memory caching in Node.js.
A JavaScript object or Map is often used to hold cached data as key-value pairs for fast lookup.
Click to reveal answer
intermediate
What is the purpose of a cache expiration or TTL (time-to-live)?
TTL sets how long cached data stays valid. After that, the cache removes or refreshes the data to keep it fresh and avoid stale info.
Click to reveal answer
intermediate
Explain the 'lazy loading' caching pattern.
Lazy loading means the cache only loads data when first requested. If data is missing, the app fetches it, stores it in cache, then returns it.
Click to reveal answer
advanced
What is the difference between write-through and write-back caching?
Write-through updates cache and main storage at the same time, ensuring consistency. Write-back updates cache first and writes to storage later, improving speed but risking data loss.
Click to reveal answer
Which data structure is best for fast key-value caching in Node.js?
AMap
BArray
CSet
DString
✗ Incorrect
Map provides fast key-value access and better performance than arrays for caching.
What does TTL stand for in caching?
ATime To Live
BTotal Time Limit
CTime To Load
DTemporary Transfer Limit
✗ Incorrect
TTL means Time To Live, the duration cached data remains valid.
In lazy loading caching, when is data loaded into the cache?
AAt server startup
BNever loaded
CEvery minute automatically
DOnly when requested
✗ Incorrect
Lazy loading caches data only when it is first requested.
Which caching pattern updates the main storage immediately when cache changes?
AWrite-back
BWrite-through
CLazy loading
DCache-aside
✗ Incorrect
Write-through caching updates both cache and storage at the same time.
What is a risk of using write-back caching?
ASlower writes
BCache is always stale
CData loss if server crashes before storage update
DCache never updates
✗ Incorrect
Write-back caching risks data loss if the server crashes before writing to storage.
Describe how you would implement a simple in-memory cache in Node.js with expiration.
Think about storing data with time info and checking it on each request.
You got /4 concepts.
Explain the differences and use cases for write-through and write-back caching patterns.
Consider speed versus data safety trade-offs.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using in-memory caching in a Node.js application?
easy
A. To send data over the network
B. To permanently save data to disk
C. To encrypt data for security
D. To store data temporarily for faster access during runtime
Solution
Step 1: Understand caching purpose
In-memory caching stores data temporarily inside the app to avoid repeated slow operations.
Step 2: Differentiate from other storage types
Permanent storage is done on disk, not in-memory cache. Network and encryption are unrelated here.
Final Answer:
To store data temporarily for faster access during runtime -> Option D
Quick Check:
In-memory cache = temporary fast storage [OK]
Hint: Cache means fast temporary storage in app memory [OK]
Common Mistakes:
Confusing cache with permanent disk storage
Thinking cache sends data over network
Assuming cache encrypts data
2. Which of the following is the correct way to create a simple in-memory cache using JavaScript's Map in Node.js?
easy
A. const cache = Map();
B. const cache = {};
C. const cache = new Map();
D. const cache = new map();
Solution
Step 1: Recall Map object creation syntax
JavaScript's Map is created with new Map(), capital M and parentheses.
Step 2: Check options for syntax correctness
const cache = new Map(); uses correct syntax. const cache = Map(); misses new keyword. const cache = {}; uses object literal, not Map. const cache = new map(); uses lowercase 'map' which is invalid.
Final Answer:
const cache = new Map(); -> Option C
Quick Check:
Map creation = new Map() [OK]
Hint: Use 'new Map()' with capital M and parentheses [OK]
Common Mistakes:
Omitting 'new' keyword
Using lowercase 'map' instead of 'Map'
Confusing Map with plain object {}
3. Consider this Node.js code using a Map as cache:
Thinking cache.get returns undefined if key exists
4. This code snippet tries to implement a cache but has a bug:
const cache = new Map();
function getData(key) {
if (cache.has(key)) {
return cache.get;
}
const data = fetchFromDb(key);
cache.set(key, data);
return data;
}
What is the bug causing incorrect cache retrieval?
medium
A. fetchFromDb should be awaited with async
B. cache.get is a function reference, should call cache.get(key)
C. cache.set(key, data) should be cache.add(key, data)
D. cache.has(key) should be cache.exists(key)
Solution
Step 1: Analyze cache retrieval line
cache.get is a function, but code returns cache.get without calling it with key.
Step 2: Correct method usage
It should be cache.get(key) to retrieve the cached value properly.
Final Answer:
cache.get is a function reference, should call cache.get(key) -> Option B
Quick Check:
Use cache.get(key) to get value [OK]
Hint: Call cache.get with key: cache.get(key) [OK]
Common Mistakes:
Returning cache.get without parentheses
Using non-existent cache.exists method
Confusing cache.set with cache.add
5. You want to implement an in-memory cache in Node.js that automatically removes entries after 5 seconds to save memory. Which approach correctly adds expiration logic?
hard
A. Use Map and setTimeout to delete the key after 5 seconds when setting cache
B. Use Map and clear the entire cache every 5 seconds with setInterval
C. Use a plain object and rely on garbage collection to remove old keys
D. Use Map and never delete keys to keep cache forever
Solution
Step 1: Understand expiration requirement
We want each cache entry to be removed individually after 5 seconds.
Step 2: Evaluate options for expiration logic
Use Map and setTimeout to delete the key after 5 seconds when setting cache uses setTimeout per key to delete it after 5 seconds, which fits requirement. Use Map and clear the entire cache every 5 seconds with setInterval clears entire cache, not per key. Use a plain object and rely on garbage collection to remove old keys relies on garbage collection which does not remove keys automatically. Use Map and never delete keys to keep cache forever never deletes keys, so no expiration.
Final Answer:
Use Map and setTimeout to delete the key after 5 seconds when setting cache -> Option A
Quick Check:
Use setTimeout per key for expiration [OK]
Hint: Use setTimeout to delete each key after delay [OK]
Common Mistakes:
Clearing whole cache instead of individual keys
Expecting garbage collection to remove keys automatically
Not deleting keys causes memory bloat
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.