Redis Cache With Spring Boot

  1. What is Redis?

  2. Redis stands for Remote Dictionary Server. It is an open source NoSQL database. It doesn’t have any tables, rows and columns. Also, it doesn’t allow statements like select statement, insert statements, update statements etc.

    It stores objects using key/value and basically used to cache and quick-response in an application to speed or increase performance.

    Various ways of Redis

    1. In-Memory Database
    2. Cache
  3. In-Memory Database

  4. In-Memory Database(IMDS) enables minimal response times. It provides responses in microsecond of times. It is the good choice for the application if an application handles the large numbers of traffic.

    Advantages:

    1. Low latency - provides real time responses
    2. High throughput
    3. High scalability

  5. Cache

  6. The cache provides data quickly with high latency to increase the data retrieval performance. Redis is the good choice for caching API calls, session states, and database queries etc.

    Advantages:

    1. High latency - provides data quickly and increase performance.
    2. Faster access to cached data.
    3. Response time is faster because data is retrieved from memory.

  7. How Redis Works?

  8. When a request comes, the code of logic checks in the Redis cache for the desired data. If appropriate data is available in the redis cache, logic simply returns the data from redis cache.

    If the requested data is not available/found in the redis cache, the logic falls back to the database or original data source to retrieve the required information to return required data. Subsequently, the fetched data is stored in the Redis cache.

  9. Spring Boot with Redis Configuration

  10. Below dependency is required in spring boot application.

    <dependency>
      <groupId> org.springframework.boot </groupId>
      <artifactId> spring-boot-starter-data-redis </artifactId>
      <version> 3.2.5 </version>
    </dependency>

  1. Need to add below dependency to create a kafka consumer in Java.
    <dependency>
        <groupId> org.apache.kafka </groupId>
        <artifactId> kafka-clients </artifactId>
        <version> 0.9.0.0 </version>
    </dependency>
  2. Java program of Kafka Consumer.
    import org.apache.kafka.clients.consumer.ConsumerConfig;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    import org.apache.kafka.common.serialization.StringDeserializer;
    
    import java.time.Duration;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class KafkaConsumerExample {
    
        static String TOPIC_NAME= "first-topic";
        public static void main(String args[]) {
            try {
                Properties properties = new Properties();
                properties.setProperty( ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092" );
                properties.setProperty( ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName() );
                properties.setProperty( ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName() );
                properties.setProperty( ConsumerConfig.GROUP_ID_CONFIG, "my_consumer_group" );
    
                KafkaConsumer<String, String> cons = new KafkaConsumer<>(properties);
                //Subscribe to the topic to consume messages/events.
                cons.subscribe(Arrays.asList(TOPIC_NAME));
                Duration duration = Duration.ofSeconds(1);
                System.out.println("*Started Receiving Messages*");
                while (true) {
                    ConsumerRecords<String, String> records =
                            cons.poll(duration.toMillis());
                    for (ConsumerRecord<String, String> record : records) {
                        System.out.println("Received event: " + record.value());
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    
    Output:
    *Started Receiving Messages*
    Received event: I am Good!
    Received event: What about You?
    Received event: Hope you are also well?
    

    Kafka Consumer In Java Output

  3. Terminate the Kafka Environment
  4. To terminate the Kafka Environment, go to the bin/windows directory of kafka folder on command prompt and write below command.
    zookeeper-server-stop.bat