Redis Best Practices and Performance Tuning

Hello everyone, we will discuss about redis in this article. We will look for answers to the questions where can we use or which scenarios can be useful for redis. Redis mean Remote Dictionary Server and Redis is one of most known and used NoSQL databases among developers. Being open sourced it is equipped with thousands of stars in GitHub and it’s found here. Redis is written in C and Redis is generally using as a cache in applications. Imagine you have an application is running under high traffic and you have to query an information from database or a service. This mean is latency for query or service call until response time in every request. Instead of you can store data that does not change very often in redis. Following image have redis data types:

redis data types ile ilgili görsel sonucu

Redis keeps values as key and value. Some commands for query from redis cache:

  • SET key value // Define a key with value
  • GET key // Get value
  • DEL key // Delete key
  • EXISTS key // Check is there key. 0 or 1 return.
  • EXPIRE key minute // Keys expiration time.
  • KEYS * // List of existing keys
  • KEYS user* // List of user starting keys.

Let’s write some code by declaring the Spring Data Redis dependencies in the pom.xml:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.3.3.RELEASE</version>
 </dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
    <type>jar</type>
</dependency>

Java Configuration

Let’s start with the configuration bean definitions:

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory jedisConFactory
      = new JedisConnectionFactory();
    jedisConFactory.setHostName("localhost");
    jedisConFactory.setPort(6379);
    return jedisConFactory;
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
}

The configuration is quite simple. First, using the Jedis client, we’re defining a connectionFactory.

Then, we defined a RedisTemplate using the jedisConnectionFactory. This can be used for querying data with a custom repository.

Redis Repository

Let’s use a Student entity for our examples:

@RedisHash("Student")
public class Student implements Serializable {
  
    public enum Gender { 
        MALE, FEMALE
    }

    private String id;
    private String name;
    private Gender gender;
    private int grade;
    // ...
}

The Spring Data Repository

Let’s now create the StudentRepository as follows:

@Repository
public interface StudentRepository extends CrudRepository<Student, String> {}

Saving a New Student Object

Let’s save a new student object in the data store:

Student student = new Student(
  "Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.save(student);

Retrieving an Existing Student Object

We can verify the correct insertion of the student in the previous section by fetching the student data:

Student retrievedStudent = 
  studentRepository.findById("Eng2015001").get();

We are retreiving data in 0-1 ms from redis. Incredible performance increase!!!

References:

https://www.baeldung.com/spring-data-redis-tutorial

https://docs.deistercloud.com/content/Databases.30/Redis?embedded=true

Leave a comment