Bird
Raised Fist0
NestJSframework~20 mins

TTL configuration in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TTL Configuration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does TTL affect cache expiration in NestJS?

Consider a NestJS cache configured with a TTL (time-to-live) of 5 seconds. What happens when you try to retrieve a cached value after 6 seconds?

NestJS
import { CacheModule, Injectable, Inject, CACHE_MANAGER } from '@nestjs/common';

@Injectable()
export class SampleService {
  constructor(@Inject(CACHE_MANAGER) private readonly cacheManager) {}

  async getCachedValue() {
    return await this.cacheManager.get('key');
  }
}

// CacheModule registered with TTL: 5 seconds
AThe cached value is null or undefined because it expired after 5 seconds.
BThe cached value is returned because TTL only affects cache insertion, not retrieval.
CAn error is thrown because the cache key is invalid after TTL expires.
DThe cache automatically refreshes the value after TTL expires, so the value is returned.
Attempts:
2 left
💡 Hint

Think about what TTL means for cached data lifetime.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets a TTL of 10 seconds in NestJS CacheModule?

Choose the correct way to configure the CacheModule with a TTL of 10 seconds.

NestJS
import { CacheModule } from '@nestjs/common';

CacheModule.register({
  // TTL configuration here
});
Attl: 10 * 1000
Bttl: 10
Cttl: 10000
Dttl: '10s'
Attempts:
2 left
💡 Hint

TTL is specified in seconds as a number.

🔧 Debug
advanced
2:00remaining
Why does the cached value never expire despite TTL set?

Given this NestJS cache configuration, the cached values never expire. What is the likely cause?

NestJS
CacheModule.register({
  ttl: 5,
  max: 100
});

// Usage:
await cacheManager.set('key', 'value');
// After 10 seconds
const val = await cacheManager.get('key'); // val is still 'value'
AThe max option disables TTL expiration when set.
BThe TTL value must be set in milliseconds, not seconds.
CThe cache store used does not support TTL expiration.
DThe cacheManager.set method overrides TTL to infinite by default.
Attempts:
2 left
💡 Hint

Consider the cache store implementation and its TTL support.

❓ state_output
advanced
2:00remaining
What is the output after setting different TTLs for cache keys?

Consider this code snippet using NestJS cache manager:

NestJS
await cacheManager.set('short', 'A', { ttl: 2 });
await cacheManager.set('long', 'B', { ttl: 5 });

// After 3 seconds
const shortVal = await cacheManager.get('short');
const longVal = await cacheManager.get('long');

console.log(shortVal, longVal);
Aundefined 'B'
B'A' undefined
Cundefined undefined
D'A' 'B'
Attempts:
2 left
💡 Hint

Think about which keys expire after 3 seconds.

🧠 Conceptual
expert
3:00remaining
How does TTL configuration interact with distributed cache in NestJS?

In a NestJS app using Redis as a distributed cache, what is the effect of setting TTL in CacheModule configuration?

ATTL causes Redis to replicate keys indefinitely without expiration.
BTTL is ignored because Redis does not support key expiration.
CTTL only applies locally in NestJS memory cache, not in Redis.
DTTL is enforced by Redis, so cached keys expire automatically after the set time.
Attempts:
2 left
💡 Hint

Think about Redis capabilities and how TTL works in distributed caches.

Practice

(1/5)
1. What does TTL stand for in NestJS caching configuration?
easy
A. Timed Token Lock, a security feature for cache access
B. Time To Live, the duration cached data stays before removal
C. Temporary Transfer Lock, a pause on cache updates
D. Total Transaction Limit, the max number of cache writes

Solution

  1. Step 1: Understand TTL meaning in caching

    TTL means how long cached data remains valid before it is removed automatically.
  2. Step 2: Match TTL to the correct full form

    TTL stands for Time To Live, which fits the caching context perfectly.
  3. Final Answer:

    Time To Live, the duration cached data stays before removal -> Option B
  4. Quick Check:

    TTL = Time To Live [OK]
Hint: TTL means how long cache data lives before removal [OK]
Common Mistakes:
  • Confusing TTL with security or transaction terms
  • Thinking TTL is a cache size limit
  • Assuming TTL is a manual cache clear command
2. Which is the correct way to set a global TTL of 10 seconds in NestJS cache module?
easy
A. CacheModule.register({ ttl: 10 })
B. CacheModule.register({ timeToLive: 10 })
C. CacheModule.register({ ttl: 10000 })
D. CacheModule.register({ expire: 10 })

Solution

  1. Step 1: Identify TTL unit in NestJS cache config

    TTL is set in seconds, so 10 means 10 seconds.
  2. Step 2: Check correct property name and value type

    The property is ttl, and the value is a number in seconds, so ttl: 10 is correct.
  3. Final Answer:

    CacheModule.register({ ttl: 10 }) -> Option A
  4. Quick Check:

    TTL in seconds uses 'ttl' property [OK]
Hint: TTL value is seconds, use 'ttl' property exactly [OK]
Common Mistakes:
  • Using milliseconds instead of seconds
  • Using wrong property names like timeToLive or expire
  • Passing ttl as string instead of number
3. Given this NestJS cache config snippet:
CacheModule.register({ ttl: 5 })

What happens to cached data after 6 seconds?
medium
A. Cached data throws an error after 6 seconds
B. Cached data remains indefinitely until manually cleared
C. Cached data is refreshed automatically every 6 seconds
D. Cached data is automatically removed after 5 seconds

Solution

  1. Step 1: Understand TTL effect on cached data

    TTL of 5 means cached data expires and is removed after 5 seconds.
  2. Step 2: Check what happens after 6 seconds

    Since 6 seconds is more than 5, cached data is no longer valid and is removed.
  3. Final Answer:

    Cached data is automatically removed after 5 seconds -> Option D
  4. Quick Check:

    TTL 5 seconds means removal after 5 seconds [OK]
Hint: Data expires after TTL seconds, so 6 > 5 means removal [OK]
Common Mistakes:
  • Thinking data stays forever without manual clear
  • Assuming TTL refreshes cache automatically
  • Believing cache throws errors on expiry
4. Identify the error in this NestJS cache TTL setup:
CacheModule.register({ ttl: '30' })
medium
A. TTL property name is incorrect, should be timeToLive
B. TTL cannot be set globally, only per item
C. TTL value should be a number, not a string
D. TTL value must be in milliseconds, not seconds

Solution

  1. Step 1: Check TTL value type

    TTL expects a number representing seconds, but '30' is a string.
  2. Step 2: Confirm correct property and units

    The property ttl is correct and uses seconds, so only the value type is wrong.
  3. Final Answer:

    TTL value should be a number, not a string -> Option C
  4. Quick Check:

    TTL must be numeric seconds, not string [OK]
Hint: TTL must be a number, not quoted string [OK]
Common Mistakes:
  • Using string instead of number for TTL
  • Changing property name incorrectly
  • Confusing seconds with milliseconds
5. You want to cache user data with a TTL of 60 seconds, but override it to 120 seconds for admin users only. How do you configure TTL in NestJS?
hard
A. Set global TTL to 60, then use @CacheTTL(120) decorator on admin methods
B. Set global TTL to 120, then manually clear cache for non-admin users
C. Set TTL only in controller constructor based on user role
D. Use two separate CacheModules with different TTLs and switch dynamically

Solution

  1. Step 1: Set global TTL for general users

    Configure CacheModule with ttl: 60 to cover normal users.
  2. Step 2: Override TTL for admin users

    Use the @CacheTTL(120) decorator on admin-specific methods to extend TTL.
  3. Final Answer:

    Set global TTL to 60, then use @CacheTTL(120) decorator on admin methods -> Option A
  4. Quick Check:

    Global TTL + @CacheTTL override [OK]
Hint: Use global TTL plus @CacheTTL decorator for overrides [OK]
Common Mistakes:
  • Trying to set TTL only in constructor without decorators
  • Using multiple CacheModules unnecessarily
  • Setting global TTL too high and clearing cache manually
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard