Redis

Using redis for caching

See

  1. https://github.com/explorer436/programming-playground/tree/main/java-playground/redis-caching-using-lettuce
  2. https://github.com/explorer436/programming-playground/tree/main/java-playground/redis-cacing-using-jedis

Using it for caching results from the DAO layer

This can reduce database loads drastically (by 70%) for read-heavy operations.

@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(10))
            .disableCachingNullValues();

        return RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(cacheConfig)
            .withCacheConfiguration("products",
                RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofMinutes(5)))
            .withCacheConfiguration("categories",
                RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofHours(1)))
            .build();
    }
}

And then using it

@Service
public class ProductService {
    // Other code

    @Cacheable(value = "products", key = "#id")
    public Mono<Product> getProductById(Long id) {
        return repository.findById(id)
            .switchIfEmpty(Mono.error(new ProductNotFoundException(id)));
    }

    @CacheEvict(value = "products", key = "#product.id")
    public Mono<Product> updateProduct(Product product) {
        return repository.save(product);
    }
}

What can you use Redis for other than caching?

Redis is an open-source, in-memory data structure store that can function as a database, cache, and message broker.

Most engineers think of Redis just for caching, but it’s far more versatile. Here are some powerful use cases you might not know:

  1. Distributed Locks
    1. Use Case: Ensuring exclusive access to shared resources in distributed systems.
    2. Example: Preventing multiple servers from modifying the same record at the same time, ensuring data consistency.
  2. Real-time Counters
    1. Use Case: Tracking event occurrences in real-time.
    2. Example: Monitoring active users on a website or counting how many times a button is clicked.
  3. Rate Limiting
    1. Use Case: Controlling how many times an action can be performed within a time window.
    2. Example: Throttling API requests to prevent abuse and protect backend servers.
  4. Leaderboards & Rankings
    1. Use Case: Managing dynamic, real-time rankings based on scores or metrics.
    2. Example: Displaying the top players in a game or ranking top products based on user ratings.
  5. Session Storage
    1. Use Case: Maintaining user session data efficiently.
    2. Example: Keeping users logged in across multiple requests without hitting the database frequently.
  6. Message Queues
    1. Use Case: Enabling asynchronous communication between services.
    2. Example: Background job processing—offloading tasks like email notifications or data processing.
  7. Inventory Management
    1. Use Case: Keeping track of stock levels in real-time.
    2. Example: Ensuring accurate stock updates in an e-commerce store to prevent overselling.

Links to this note