0
0
Expressframework~8 mins

In-memory caching with node-cache in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: In-memory caching with node-cache
MEDIUM IMPACT
This concept affects server response time and reduces backend processing load, improving how fast pages or data are served to users.
Serving frequently requested data in an Express app
Express
import NodeCache from 'node-cache';
const cache = new NodeCache({ stdTTL: 60 });

app.get('/data', (req, res) => {
  const cachedData = cache.get('data');
  if (cachedData) {
    return res.json(cachedData);
  }
  fetchFromDatabase().then(data => {
    cache.set('data', data);
    res.json(data);
  });
});
Data is stored in memory for 60 seconds, so repeated requests return instantly without hitting the database.
📈 Performance GainReduces server processing time and database queries, improving response time and lowering server load.
Serving frequently requested data in an Express app
Express
app.get('/data', async (req, res) => {
  const data = await fetchFromDatabase();
  res.json(data);
});
Every request queries the database, causing slow responses and high server load.
📉 Performance CostBlocks response for each request; increases server CPU and database load.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching - fetch on every requestN/A (server-side)N/AN/A[X] Bad
In-memory caching with node-cacheN/A (server-side)N/AN/A[OK] Good
Rendering Pipeline
In-memory caching reduces backend processing before the response reaches the browser, speeding up the critical rendering path by delivering data faster.
Server Processing
Network Transfer
First Paint
⚠️ BottleneckServer Processing (database queries and data fetching)
Core Web Vital Affected
LCP
This concept affects server response time and reduces backend processing load, improving how fast pages or data are served to users.
Optimization Tips
1Cache frequently requested data in memory to reduce backend load.
2Set appropriate cache expiration to balance freshness and performance.
3Use caching to improve server response time and reduce LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using in-memory caching with node-cache in an Express app?
AReduces repeated database queries and speeds up response time
BIncreases the size of the client-side bundle
CTriggers more DOM reflows in the browser
DImproves CSS selector performance
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page or API call, and check response times and status codes.
What to look for: Look for faster response times and fewer requests hitting the backend when caching is enabled.