0
0
Node.jsframework~10 mins

In-memory caching patterns in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - In-memory caching patterns
Request comes in
Check cache for data
Return cached
Send response
Send response
This flow shows how a request first checks the cache. If data is found, it returns immediately. If not, it fetches from the source, stores it in cache, then returns.
Execution Sample
Node.js
const cache = new Map();

async function getData(key) {
  if (cache.has(key)) return cache.get(key);
  const data = await fetchFromDB(key);
  cache.set(key, data);
  return data;
}
This code checks if data is in cache. If yes, returns it. Otherwise, fetches from DB, caches it, then returns.
Execution Table
StepActionCache StateResult
1Call getData('user1'){}Cache miss, fetch from DB
2Fetch data for 'user1'{}{ name: 'Alice' }
3Store 'user1' data in cache{ 'user1': { name: 'Alice' } }Data cached
4Return data for 'user1'{ 'user1': { name: 'Alice' } }{ name: 'Alice' }
5Call getData('user1') again{ 'user1': { name: 'Alice' } }Cache hit, return cached data
6Return cached data for 'user1'{ 'user1': { name: 'Alice' } }{ name: 'Alice' }
💡 After second call, data is returned from cache, no DB fetch needed.
Variable Tracker
VariableStartAfter Step 3After Step 6
cache{}{ 'user1': { name: 'Alice' } }{ 'user1': { name: 'Alice' } }
Key Moments - 2 Insights
Why does the second call to getData('user1') not fetch from the DB again?
Because the cache already contains 'user1' data after step 3, so at step 5 the function returns cached data immediately without fetching.
What happens if the cache does not have the requested key?
The function fetches data from the source (DB), stores it in cache (step 3), then returns it (step 4), as shown in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after step 3?
A{ 'user2': { name: 'Bob' } }
B{ 'user1': { name: 'Alice' } }
C{}
Dnull
💡 Hint
Check the 'Cache State' column at step 3 in the execution table.
At which step does the function return cached data instead of fetching from DB?
AStep 5
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'Cache hit' in the 'Result' column of the execution table.
If we clear the cache before step 5, what would happen at step 5?
AReturn cached data
BThrow an error
CFetch from DB again
DReturn null
💡 Hint
Refer to the cache state in variable_tracker and what happens on cache miss in execution_table.
Concept Snapshot
In-memory caching stores data in memory for fast access.
Check cache first before fetching from slow source.
If cache miss, fetch data, store in cache, then return.
Use simple Map or object for cache in Node.js.
Improves performance by avoiding repeated fetches.
Full Transcript
In-memory caching is a pattern where data is stored temporarily in the program's memory to speed up repeated access. When a request for data comes in, the program first checks if the data is already in the cache. If it is, it returns the cached data immediately, saving time. If not, it fetches the data from a slower source like a database, stores it in the cache, and then returns it. This way, subsequent requests for the same data are faster. The example code uses a Map object as the cache. The execution table shows the steps: first call misses cache and fetches data, second call hits cache and returns data quickly. This pattern helps improve performance and reduce load on external data sources.