MicroServices and Redis

Query Caching

Query response can be cached in two most effective ways:

1. Cache prefetching

An entire set of required objects can be pre-cached in Redis. Objects could be static and dynamic:

2. Cache-aside pattern

Redis is filled on demand:

  1. An application requests data from the backend.
  2. The backend checks to find out if the data is available in Redis.
  3. If data is not found (a cache miss), it is fetched from the database.
  4. The data returned from the database is subsequently stored in Redis.
  5. The data is then returned to the application.
MicroServices and Redis

Redis Cache Configuration Comparison

Redis Cache Configuration in Spring Boot refers to the setup and customization of the Redis cache store for caching data. This involves configuring the cache expiration, cache key prefix, and other settings to optimize cache performance. Here are some key aspects of Redis Cache Configuration:

  1. Time-To-Live (TTL) Expiration: Configuring the duration after which cache entries expire and are removed from Redis.
  2. Cache Key Prefix: Defining a prefix for cache keys to help identify and manage cache entries.
  3. Cache Defaults: Setting default cache configuration parameters, such as TTL expiration, for all caches.
  4. Custom Cache Configuration: Providing custom implementations for cache writers, serializers, and other cache-related components.

RedisTemplate

RedisTemplate is a Spring-provided abstraction for interacting with Redis. It provides a higher-level API for executing Redis commands, such as SET, GET, and DEL, and offers features like:

  1. Transaction Support: Enabling atomic operations and ensuring consistency across multiple Redis commands.
  2. Connection Pooling: Managing connections to Redis and reusing them to improve performance.
  3. Serialization: Converting Java objects to Redis-friendly formats (e.g., JSON, Protocol Buffers) and vice versa.

Code Examples

Redis Cache Configuration

@Configuration
public class RedisConfig {
    @Bean
    public RedisCacheConfiguration cacheConfiguration() {
        return RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(5))
                .cacheKeyPrefix("my-app-cache-");
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }
    
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        // Configure Redis connection factory
    }
}

RedisTemplate Usage

@Autowired
private RedisTemplate<String, String> redisTemplate;

public void setCacheValue(String key, String value) {
    redisTemplate.opsForValue().set(key, value);
}

public String getCacheValue(String key) {
    return redisTemplate.opsForValue().get(key);
}

API Gateway Caching

When a request comes in:

  1. Decode the Authorization header.
  2. Validate the credentials.
  3. Store the session information on the request object or cache for further use down the line by the application.

Caching Architecture

Write Behind

  1. The application reads and writes data to Redis.
  2. Redis syncs any changed data to the MongoDB database asynchronously. This is eventual consistency.

Write Through

  1. The application reads and writes data to Redis.
  2. Redis syncs any changed data to the MongoDB database synchronously. The thread waits in this pattern until the write to the database is also completed.

Write-through caching with Redis ensures that the (critical data) cache is always up-to-date with the database, providing strong consistency and improving application performance.

Pub-Sub

Notify other microservices to retrieve updated content; fire and forget.

RedisTemplate

The template offers a high-level abstraction for Redis interactions. It takes care of serialization and connection management, freeing the user from dealing with such details. Once configured, the template is thread-safe and can be reused across multiple instances.

RedisTemplate uses a Java-based serializer for most of its operations. You can change the serialization mechanism on the template, and the Redis module offers several implementations available in the org.springframework.data.redis.serializer package.

Redis Streams

A Redis stream is a data structure that acts like an append-only log. While it has similarities to Pub/Sub, the main difference lies in the persistence of messages and how they are consumed.

Redis Streams use a persistent, append-only data type that retains messages until the stream is trimmed. Unlike Pub/Sub, which relies on server-side subscriptions and pushes messages to clients, Redis Streams require active polling.

Redis Transactions

Redis Transactions allow the execution of a group of commands in a single step. They are centered around the commands MULTI, EXEC, DISCARD and WATCH. Redis Transactions make two important guarantees:

  1. All commands in a transaction are serialized and executed sequentially.
  2. The EXEC command triggers the execution of all commands in the transaction.

RedisGears

RedisGears is a programmable serverless engine for transaction, batch, and event-driven data processing, allowing users to write and run their own functions on data stored in Redis.

Functions can be implemented in different languages, including Python and C, and can be executed by the RedisGears engine in two ways:

  1. Batch: triggered by the Run action, execution is immediate and on existing data
  2. Event: triggered by the Register action, execution is triggered by new events and on their data