Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 3 of 10 · Caching, Async & Scheduling
Interview question

How do you integrate Redis as a distributed cache in Spring Boot? स्प्रिंग बूट में डिस्ट्रिब्यूटेड कैश के रूप में Redis को कैसे इंटीग्रेट करें?

Answer

Spring Boot's default cache manager (ConcurrentMapCacheManager) stores cached data in local JVM memory, which doesn't work across multiple instances of a horizontally scaled application — each instance would have its own inconsistent cache. Redis solves this by acting as a shared, external, in-memory cache that every instance reads from and writes to.

Adding spring-boot-starter-data-redis and setting spring.cache.type=redis is enough for Spring Boot to auto-configure a RedisCacheManager, so existing @Cacheable, @CachePut, and @CacheEvict annotations work unchanged while the underlying storage becomes centralized, persistent (optionally), and shared across all service instances.

spring:
  cache:
    type: redis
  data:
    redis:
      host: localhost
      port: 6379

स्प्रिंग बूट का डिफ़ॉल्ट कैश मैनेजर (ConcurrentMapCacheManager) कैश्ड डेटा को लोकल JVM मेमोरी में स्टोर करता है, जो हॉरिज़ॉन्टली स्केल्ड एप्लिकेशन के कई इंस्टेंस में काम नहीं करता। Redis एक साझा, बाहरी, इन-मेमोरी कैश के रूप में काम करके इसे हल करता है जिसे हर इंस्टेंस पढ़ता और लिखता है।

spring-boot-starter-data-redis जोड़ना और spring.cache.type=redis सेट करना स्प्रिंग बूट के लिए RedisCacheManager ऑटो-कॉन्फिगर करने के लिए काफी है, इसलिए मौजूदा @Cacheable एनोटेशन बिना बदले काम करते हैं।

spring:
  cache:
    type: redis
  data:
    redis:
      host: localhost
      port: 6379

Was this answer clear?