Discover how a simple trick can make your website feel lightning fast!
Why caching improves performance in Express - The Real Reasons
Imagine a busy website where every user request triggers the server to fetch data from a slow database again and again.
Manually fetching data each time wastes time, overloads the server, and makes users wait longer for pages to load.
Caching stores the data temporarily so the server can quickly reuse it without repeating slow operations, making responses faster.
app.get('/data', (req, res) => { db.query('SELECT * FROM table', (err, result) => { res.send(result); }); });
let cache; app.get('/data', (req, res) => { if (cache) return res.send(cache); db.query('SELECT * FROM table', (err, result) => { cache = result; res.send(result); }); });
Caching enables websites to serve many users quickly without slowing down, improving user experience and saving resources.
Popular news sites cache headlines so millions can see the latest news instantly without hitting the database every time.
Manual repeated data fetching is slow and resource-heavy.
Caching stores data temporarily for quick reuse.
This speeds up responses and reduces server load.
Practice
Solution
Step 1: Understand caching purpose
Caching saves results of expensive operations temporarily so they can be reused.Step 2: Relate caching to performance
By reusing stored data, the server avoids repeating slow tasks, speeding up responses.Final Answer:
It stores data temporarily to avoid repeating expensive operations -> Option CQuick Check:
Caching = Temporary storage for speed [OK]
- Thinking caching permanently increases memory
- Believing caching slows down the server
- Confusing caching with deleting data
Solution
Step 1: Identify correct Express method
Express uses res.set() or res.header() to set headers; res.set() is common.Step 2: Check cache header format
'Cache-Control' with 'max-age=3600' sets cache for 3600 seconds (1 hour).Final Answer:
res.set('Cache-Control', 'max-age=3600') -> Option BQuick Check:
Correct header syntax = res.set('Cache-Control', 'max-age=3600') [OK]
- Using non-existent res.cache() method
- Setting 'no-cache' disables caching
- Wrong method names like res.cacheControl()
app.get('/data', (req, res) => {
if (cache.data) {
res.send(cache.data);
} else {
const result = expensiveCalculation();
cache.data = result;
res.send(result);
}
});
What happens on the second request to '/data'?Solution
Step 1: Analyze first request behavior
On first request, cache.data is empty, so expensiveCalculation() runs and result is cached.Step 2: Analyze second request behavior
On second request, cache.data exists, so server sends cached data directly without recalculating.Final Answer:
The server sends cached data without recalculating -> Option AQuick Check:
Cache hit = reuse data [OK]
- Assuming calculation runs every time
- Thinking cache.data is undefined on second request
- Believing server sends empty response
app.get('/info', (req, res) => {
if (cache.info) {
res.send(cache.info);
} else {
const data = fetchData();
cache.info = data;
res.send(data);
}
});
fetchData() is asynchronous and returns a Promise. What is the main issue?Solution
Step 1: Identify async function usage
fetchData() returns a Promise but code treats it as synchronous.Step 2: Understand res.send() timing
res.send(data) runs immediately, sending the unresolved Promise, not actual data.Final Answer:
res.send() is called before fetchData() resolves, sending a Promise object -> Option DQuick Check:
Async function needs await before sending [OK]
- Ignoring async nature of fetchData()
- Assuming cache is never set
- Thinking res.send() can't send strings
Solution
Step 1: Understand caching with expiration
To keep data fresh, cache should have a time limit after which it refreshes.Step 2: Evaluate options for freshness and speed
Store data in cache with a timestamp; refresh cache if older than 10 minutes uses timestamp to refresh cache every 10 minutes, balancing speed and freshness.Final Answer:
Store data in cache with a timestamp; refresh cache if older than 10 minutes -> Option AQuick Check:
Timed cache refresh = Store data in cache with a timestamp; refresh cache if older than 10 minutes [OK]
- Caching forever causes stale data
- Never caching loses speed benefits
- Manual cache clearing is unreliable
Ready to prove it?
Pass the test and this pattern moves unknown
