Bird
Raised Fist0
Spring Bootframework~20 mins

Redis as cache provider in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a cached value expires in Redis?

Consider a Spring Boot application using Redis as a cache provider with a TTL (time-to-live) set on cached entries.

What will happen when the TTL expires for a cached key?

AThe cached key is moved to a backup Redis database automatically.
BThe cached key remains in Redis but returns a null value until manually deleted.
CRedis throws an error when trying to access an expired key.
DThe cached key is automatically removed from Redis, so the next request will fetch fresh data and cache it again.
Attempts:
2 left
💡 Hint

Think about how Redis handles keys with expiration times.

📝 Syntax
intermediate
1:30remaining
Which annotation enables Redis caching in Spring Boot?

In a Spring Boot application, you want to enable Redis caching support. Which annotation should you add to your main application class?

A@RedisCacheable
B@EnableCaching
C@EnableRedisCache
D@CacheConfig
Attempts:
2 left
💡 Hint

Look for the general Spring annotation that activates caching.

🔧 Debug
advanced
2:30remaining
Why does this Redis cache not store values correctly?

Given this Spring Boot method with Redis caching:

@Cacheable(value = "items")
public List getItems() {
    return List.of("apple", "banana");
}

But the cache never stores the list, and the method runs every time. What is the likely cause?

ARedis does not support caching lists.
BThe @Cacheable annotation requires a key attribute to work.
CThe Redis cache manager is not configured properly or missing in the application context.
DThe method must be static to be cached.
Attempts:
2 left
💡 Hint

Check if the caching infrastructure is set up correctly.

🧠 Conceptual
advanced
2:00remaining
What is the effect of using @CachePut instead of @Cacheable with Redis?

In Spring Boot Redis caching, what is the main difference between @CachePut and @Cacheable annotations on a method?

A@CachePut always executes the method and updates the cache, while @Cacheable skips method execution if cache exists.
B@CachePut caches only null values, @Cacheable caches non-null values.
C@CachePut disables Redis caching, @Cacheable enables it.
D@CachePut caches the method result only on exceptions, @Cacheable caches on success.
Attempts:
2 left
💡 Hint

Think about when the method runs and when the cache updates.

❓ state_output
expert
3:00remaining
What is the output after these Redis cache operations in Spring Boot?

Consider this Spring Boot service using Redis cache:

@Cacheable(value = "numbers", key = "#num")
public int square(int num) {
    return num * num;
}

// Calls:
int a = square(3);
int b = square(3);
int c = square(4);

What are the values of a, b, and c, and how many times is the method body executed?

Aa=9, b=9, c=16; method executed 2 times
Ba=9, b=9, c=16; method executed 3 times
Ca=9, b=0, c=16; method executed 3 times
Da=9, b=9, c=16; method executed 1 time
Attempts:
2 left
💡 Hint

Remember how @Cacheable caches results by key.

Practice

(1/5)
1. What is the main purpose of using Redis as a cache provider in a Spring Boot application?
easy
A. To store frequently accessed data in memory for faster retrieval
B. To replace the database completely
C. To handle user authentication
D. To serve static web pages

Solution

  1. Step 1: Understand Redis caching role

    Redis stores data in memory, which is much faster than disk storage.
  2. Step 2: Identify caching benefit in Spring Boot

    Caching reduces database calls by storing frequently used data temporarily.
  3. Final Answer:

    To store frequently accessed data in memory for faster retrieval -> Option A
  4. Quick Check:

    Redis cache = fast data access [OK]
Hint: Redis cache speeds up data access by storing in memory [OK]
Common Mistakes:
  • Thinking Redis replaces the database
  • Confusing caching with authentication
  • Assuming Redis serves static files
2. Which annotation is used in Spring Boot to enable caching support when using Redis?
easy
A. @RedisCache
B. @Cacheable
C. @EnableCaching
D. @CacheConfig

Solution

  1. Step 1: Identify the annotation to activate caching

    @EnableCaching is used on a configuration class to turn on caching support.
  2. Step 2: Differentiate from other annotations

    @Cacheable marks methods to cache results, but does not enable caching globally.
  3. Final Answer:

    @EnableCaching -> Option C
  4. Quick Check:

    Enable caching = @EnableCaching [OK]
Hint: Use @EnableCaching to activate caching in Spring Boot [OK]
Common Mistakes:
  • Confusing @Cacheable with enabling caching
  • Using non-existent @RedisCache annotation
  • Mistaking @CacheConfig for enabling caching
3. Given the following Spring Boot method with Redis caching:
@Cacheable("books")
public Book findBookById(Long id) {
    System.out.println("Fetching from DB");
    return bookRepository.findById(id).orElse(null);
}
What will be printed if findBookById(1L) is called twice in a row?
medium
A. Fetching from DB
B. No output
C. Fetching from DB Fetching from DB
D. Error at runtime

Solution

  1. Step 1: Understand @Cacheable behavior

    The first call caches the result and prints "Fetching from DB".
  2. Step 2: Analyze second call output

    The second call returns cached data without executing method body, so no print.
  3. Final Answer:

    Fetching from DB -> Option A
  4. Quick Check:

    Cache avoids repeated DB fetch print [OK]
Hint: First call prints, second uses cache silently [OK]
Common Mistakes:
  • Expecting print on every call
  • Thinking cache disables method
  • Assuming runtime error due to caching
4. Consider this Spring Boot Redis cache configuration snippet:
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
    return RedisCacheManager.builder(factory).build();
}
If the Redis server is down, what will happen when the application tries to cache data?
medium
A. Application falls back to file-based cache
B. Application throws a runtime exception and crashes
C. Data is cached in memory without Redis
D. Caching silently fails but application continues

Solution

  1. Step 1: Understand RedisCacheManager dependency

    RedisCacheManager requires a running Redis server to store cache data.
  2. Step 2: Analyze behavior if Redis is down

    Without Redis, cache operations typically fail silently and the application continues unless explicitly configured to fail.
  3. Final Answer:

    Caching silently fails but application continues -> Option D
  4. Quick Check:

    Redis down usually does not crash app by default [OK]
Hint: Redis down causes silent cache failure unless handled [OK]
Common Mistakes:
  • Assuming fallback to memory cache
  • Thinking caching silently fails
  • Expecting file-based fallback
5. You want to cache the result of a method that returns a list of users filtered by role. Which is the best way to use Redis caching in Spring Boot to avoid caching empty or null results?
public List<User> getUsersByRole(String role) {
    // fetch users
}
hard
A. Use @CachePut to always update cache
B. Use @Cacheable with condition = "#result != null && !#result.isEmpty()"
C. Manually check and cache inside the method
D. Use @Cacheable without any condition

Solution

  1. Step 1: Understand caching empty results issue

    Caching empty or null results wastes cache space and may cause stale data.
  2. Step 2: Use @Cacheable condition attribute

    Adding condition "#result != null && !#result.isEmpty()" caches only meaningful results.
  3. Final Answer:

    Use @Cacheable with condition = "#result != null && !#result.isEmpty()" -> Option B
  4. Quick Check:

    Condition filters empty cache entries [OK]
Hint: Use @Cacheable condition to skip empty results [OK]
Common Mistakes:
  • Caching all results including empty lists
  • Manually caching inside method instead of annotation
  • Using @CachePut which always updates cache
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