Redis ( is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. It is widely used in micro services. Spring boot already provided support for Redis. We will introduce how to integrate Redis in spring boot with a simple hello world example.

Step by Step

  • Install Redis in local

You can download redis in directly if you are using Linux.

  • Add Redis dependencies in your project, we use gradle project as a example.
compile 'org.springframework.boot:spring-boot-starter-data-redis'
  • Add necessary configurations in your applications.yml
spring: redis: database: 0 host: localhost port: 6379 password: pool.max-active: 8 pool.max-wait: -1 pool.max-idle: 8 pool.min-idle: 0 timeout: 0
  • Use Redis in your code by using “RedisTemplate
@RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("test") public class ApplicationTests { // @Autowired // private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate<String,String> redisTemplate; @Test public void test() throws Exception { redisTemplate.opsForValue().set("testKey","Hello"); Assert.assertEquals("Hello", redisTemplate.opsForValue().get("testKey")); // stringRedisTemplate.opsForValue().set("testKey", "Hello"); // Assert.assertEquals("Hello", stringRedisTemplate.opsForValue().get("testKey")); } } 

You may noticed we comments out the usage of StringRedisTemplate, this is an extension provided by Spring. The javadoc of this class is as follows:

String-focused extension of RedisTemplate. Since most operations against Redis are String based, this class provides a dedicated class that minimizes configuration of its more generic {@link RedisTemplate template} especially in terms of serializers.

Thus, we can use StringRedisTemplate directly in many cases.


As Redis is a key-value store, you need to make sure the key is unique whenever you want to store your data in it.You can also delete your data by invoking delete method provided in RedisTemplate


Redis provide a lot operations for different types, it also provide support for transaction but it is disable by default. you can find more information in

  • Using Redis in Cloud such as SCP

Spring cloud also provide support for Redis, you can add following configuration to connection your Redis in Cloud simply.

@Bean public RedisConnectionFactory redisFactory() { PoolConfig poolConfig = new PoolConfig(5, 30, 3000); PooledServiceConnectorConfig redisConfig = new PooledServiceConnectorConfig(poolConfig); return connectionFactory().redisConnectionFactory(redisConfig); }


We only cover few information of Redis in this blog, there are many advanced usage of Redis in micro services you can find in