0
0
NestJSframework~10 mins

Why caching reduces response latency in NestJS - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why caching reduces response latency
Client sends request
Check cache for data
Return cached
response fast
Store data in cache
Return response
The system first checks if the requested data is in cache. If yes, it returns it immediately, skipping slower database or API calls, thus reducing response time.
Execution Sample
NestJS
async getUser(id: string) {
  const cached = await this.cacheManager.get(id);
  if (cached) return cached;
  const user = await this.userService.findById(id);
  await this.cacheManager.set(id, user);
  return user;
}
This function tries to get user data from cache first; if missing, it fetches from the database, caches it, then returns.
Execution Table
StepActionCache Check ResultData SourceCache UpdateResponse
1Receive request for user id '123'N/AN/AN/AN/A
2Check cache for '123'MissN/AN/AN/A
3Fetch user from DBN/ADatabaseN/AN/A
4Store user data in cacheN/AN/ACache set '123'N/A
5Return user dataN/AN/AN/AUser data from DB
6Receive request for user id '123' againN/AN/AN/AN/A
7Check cache for '123'HitCacheN/AUser data from cache
8Return cached user dataN/AN/AN/AUser data from cache
💡 Execution stops after returning user data from cache or DB.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7Final
cacheManager.get('123')undefinedundefined (miss)user object cacheduser object cached (hit)user object cached
userundefinedundefineduser object from DBuser object from DBuser object returned
Key Moments - 2 Insights
Why does the function return immediately when cache has data?
Because the cache hit at step 7 means data is ready locally, so no need to wait for slower DB fetch (see execution_table row 7).
What happens when cache misses the data?
The function fetches data from the database (step 3), then stores it in cache (step 4) before returning (step 5), as shown in execution_table rows 3-5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the data source at step 3?
ADatabase
BAPI
CCache
DUndefined
💡 Hint
Check the 'Data Source' column at step 3 in the execution_table.
At which step does the cache get updated with new data?
AStep 7
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Cache Update' column in the execution_table.
If the cache always hits, which steps would be skipped?
ASteps 2 and 7
BSteps 3 and 4
CSteps 5 and 8
DSteps 1 and 6
💡 Hint
Refer to execution_table rows where cache miss triggers DB fetch and cache set.
Concept Snapshot
Caching stores data temporarily to serve future requests faster.
Check cache first: if data found, return immediately.
If not found, fetch from DB/API, store in cache, then return.
Reduces response latency by avoiding slow data fetches repeatedly.
Common in NestJS using cacheManager with async get/set methods.
Full Transcript
When a client requests data, the system first checks if the data is in cache. If it is, the cached data is returned immediately, making the response very fast. If the data is not in cache, the system fetches it from the database or an API, then stores it in cache for future requests before returning it. This process reduces response latency by avoiding repeated slow data fetches. In NestJS, this is done using the cacheManager's get and set methods asynchronously.