0
0
NestJSframework~20 mins

Why caching reduces response latency in NestJS - Challenge Your Understanding

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

In a NestJS application, why does using caching typically reduce the response latency for repeated requests?

ABecause caching compresses the response data, making it smaller to send over the network.
BBecause caching stores the result of expensive operations, so the server can quickly return the stored data without recalculating.
CBecause caching sends the request to multiple servers simultaneously to get the fastest response.
DBecause caching increases the server's CPU speed, making all operations faster.
Attempts:
2 left
💡 Hint

Think about what happens when the server has already done the work once and saves the result.

component_behavior
intermediate
2:00remaining
What happens when a cached value is available in NestJS?

Consider a NestJS controller method that uses caching. What is the behavior when a cached value exists for a request?

AThe method skips executing its logic and returns the cached value immediately.
BThe method executes fully but ignores the cached value.
CThe method clears the cache and then executes its logic.
DThe method returns an error because cached values are not allowed.
Attempts:
2 left
💡 Hint

Think about why caching is used: to avoid repeating work.

📝 Syntax
advanced
2:00remaining
Identify the correct NestJS cache interceptor usage

Which option correctly applies the built-in cache interceptor to a NestJS controller method to enable caching?

NestJS
import { Controller, Get, UseInterceptors, CacheInterceptor } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  @UseInterceptors(/* ??? */)
  findAll() {
    // expensive operation
    return ['item1', 'item2'];
  }
}
ACacheInterceptor
BCacheManager
CCacheService
DCacheModule
Attempts:
2 left
💡 Hint

Look for the interceptor class that handles caching.

state_output
advanced
2:00remaining
What is the response time difference with and without caching?

Given a NestJS endpoint that takes 2 seconds to compute a response, what will be the approximate response time for the second request if caching is enabled?

AAbout 4 seconds because caching doubles the processing time.
BAbout 2 seconds because caching does not affect response time.
CAbout 0.01 seconds because the cached result is returned instantly.
DAbout 1 second because caching halves the processing time.
Attempts:
2 left
💡 Hint

Think about how caching avoids repeating the expensive operation.

🔧 Debug
expert
3:00remaining
Why does caching not reduce latency in this NestJS example?

Consider this NestJS controller method using caching, but response times remain slow on repeated requests. What is the likely cause?

NestJS
import { Controller, Get, UseInterceptors, CacheInterceptor } from '@nestjs/common';

@Controller('data')
export class DataController {
  @Get()
  @UseInterceptors(CacheInterceptor)
  async fetchData() {
    const result = await this.expensiveOperation();
    return result;
  }

  async expensiveOperation() {
    // Simulate delay
    await new Promise(res => setTimeout(res, 2000));
    return { data: 'value' };
  }
}
AThe controller method must return a string, not an object, for caching to work.
BThe expensiveOperation method is async, so caching cannot store async results.
CThe CacheInterceptor only caches POST requests, not GET requests.
DThe cache is not enabled globally or the CacheModule is not imported, so caching does not work.
Attempts:
2 left
💡 Hint

Check if the caching system is properly set up in the app module.