0
0
Expressframework~20 mins

Why caching improves performance in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Caching Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does caching reduce server load in Express?

Imagine your Express server handles many requests for the same data. How does caching help reduce the server's work?

ACaching stores the data temporarily so the server can quickly send it without recalculating or fetching it again.
BCaching deletes old data to free up memory and speed up the server.
CCaching forces the server to process every request from scratch to ensure fresh data.
DCaching slows down the server by adding extra steps before sending data.
Attempts:
2 left
💡 Hint

Think about how remembering answers helps you avoid repeating work.

component_behavior
intermediate
2:00remaining
What happens when Express serves cached content?

Consider an Express route that caches API responses. What is the main effect on response time when a cached response is available?

Express
app.get('/data', (req, res) => {
  if (cache.has('data')) {
    res.send(cache.get('data'));
  } else {
    const data = fetchDataFromDB();
    cache.set('data', data);
    res.send(data);
  }
});
AResponse time is unpredictable because cache may cause errors.
BResponse time is slower because the server checks the cache first.
CResponse time is faster because data is sent directly from cache without waiting for the database.
DResponse time is the same because the database is always queried.
Attempts:
2 left
💡 Hint

Think about skipping the slowest step.

🔧 Debug
advanced
2:30remaining
Identify the caching mistake causing stale data in Express

Look at this Express caching code. Why might users see outdated data?

Express
const cache = new Map();

app.get('/info', (req, res) => {
  if (cache.has('info')) {
    res.send(cache.get('info'));
  } else {
    const info = fetchLatestInfo();
    cache.set('info', info);
    res.send(info);
  }
});
AThe fetchLatestInfo function is asynchronous but used synchronously.
BThe cache is cleared too often, causing frequent database calls.
CThe cache key 'info' is misspelled, so cache is never used.
DThe cache never expires, so old data stays forever without updates.
Attempts:
2 left
💡 Hint

Think about how long cached data stays valid.

📝 Syntax
advanced
2:30remaining
Which caching code snippet correctly caches JSON responses in Express?

Choose the code that properly caches and sends JSON data in Express.

A
app.get('/user', (req, res) => {
  if (cache.user) {
    res.json(cache.user);
  } else {
    const user = getUser();
    cache.user = user;
    res.json(user);
  }
});
B
app.get('/user', (req, res) => {
  if (cache.user) {
    res.send(JSON.stringify(cache.user));
  } else {
    const user = getUser();
    cache.user = JSON.stringify(user);
    res.send(user);
  }
});
C
app.get('/user', (req, res) => {
  if (cache.user) {
    res.json(JSON.parse(cache.user));
  } else {
    const user = getUser();
    cache.user = user;
    res.send(user);
  }
});
D
app.get('/user', (req, res) => {
  if (cache.user) {
    res.send(cache.user);
  } else {
    const user = getUser();
    cache.user = JSON.stringify(user);
    res.send(JSON.parse(cache.user));
  }
});
Attempts:
2 left
💡 Hint

Remember Express has a method to send JSON directly.

state_output
expert
3:00remaining
What is the cache state after multiple requests?

Given this Express caching code, what is the content of cache after three requests to /count?

Express
const cache = {};

app.get('/count', (req, res) => {
  if (!cache.count) {
    cache.count = 1;
  } else {
    cache.count += 1;
  }
  res.send(`Count is ${cache.count}`);
});
A{}
B{"count": 3}
C{"count": 0}
D{"count": 1}
Attempts:
2 left
💡 Hint

Each request increases the count by one if cached.