Configure a Spring Boot Initializer app to use Redis in the cloud with Azure Redis Cache

This article walks you through creating a Redis cache in the cloud using the Azure portal, then using the Spring Initializr to create a custom application, and then creating a Java web application that stores and retrieves data using your Redis cache.


The following prerequisites are required in order to complete the steps in this article:

Create a custom application using the Spring Initializr

  1. Browse to

  2. Specify that you want to generate a Maven project with Java, select Java version 8, and enter the Group and Artifact names for your application.

  3. Add dependencies for Spring Web section and check the box for Web, then scroll down to the NoSQL section and check the box for Spring Data Reactive Redis.

  4. Scroll to the bottom of the page and click the button to Generate Project.

    Basic Spring Initializr options


    The Spring Initializr will use the Group and Artifact names to create the package name; for example: com.contoso.myazuredemo.

  5. After you have extracted the files on your local system, your custom Spring Boot application will be ready for editing.

    Custom Spring Boot project files

Create a Redis cache on Azure

  1. Browse to the Azure portal at and click +New.

  2. Click Database, and then click Redis Cache.

    Selecting Redis Cache in the Azure portal.

  3. On the New Redis Cache page, specify the following information:

    • Enter the DNS name for your cache.
    • Specify your Subscription, Resource group, Location, and Cache type.


    You can use SSL with Redis caches, but you would need to use a different Redis client like Jedis. For more information, see How to use Azure Redis Cache with Java.

    When you have specified these options, select Advanced tab.

    Create the cache in the Azure portal.

    Select the checkbox next to Non-TLS port, then select Review + create, review your specifications, and select Create.

    Select Non-TLS port when creating azure cache.

  4. Once your cache has been completed, you will see it listed on your Azure Dashboard, as well as under the All Resources, and Redis Caches pages. You can click on your cache on any of those locations to open the properties page for your cache.

    Resource provisioned in the Azure portal.

  5. When the page that contains the list of properties for your cache is displayed, click Access keys and copy your access keys for your cache.

    Copy the access keys under the Access keys section.

Configure your custom Spring Boot to use your Redis Cache

  1. Locate the file in the resources directory of your app, or create the file if it does not already exist.

    Locate the file

  2. Open the file in a text editor, and add the following lines to the file, and replace the sample values with the appropriate properties from your cache:

    # Specify the DNS URI of your Redis cache.
    # Specify the port for your Redis cache.
    # Specify the access key for your Redis cache.

    Editing the file


    If you were using a different Redis client like Jedis that enables SSL, you would specify that you want to use SSL in your file and use port 6380. For example:

    # Specify the DNS URI of your Redis cache.
    # Specify the access key for your Redis cache.
    # Specify that you want to use SSL.
    # Specify the SSL port for your Redis cache.

    For more information, see How to use Azure Redis Cache with Java.

  3. Save and close the file.

  4. Create a folder named controller under the source folder for your package; for example:




  5. Create a new file named in the controller folder. Open the file in a text editor and add the following code to it:

    package com.contoso.myazuredemo;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    public class HelloController {
       private StringRedisTemplate template;
       // Define the Hello World controller.
       public String hello() {
          ValueOperations<String, String> ops = this.template.opsForValue();
          // Add a Hello World string to your cache.
          String key = "greeting";
          if (!this.template.hasKey(key)) {
              ops.set(key, "Hello World!");
          // Return the string from your cache.
          return ops.get(key);

    Where you will need to replace com.contoso.myazuredemo with the package name for your project.

  6. Save and close the file.

  7. Build your Spring Boot application with Maven and run it; for example:

    mvn clean package
    mvn spring-boot:run
  8. Test the web app by browsing to http://localhost:8080 using a web browser, or use the syntax like the following example if you have curl available:

    curl http://localhost:8080

    You should see the "Hello World!" message from your sample controller displayed, which is being retrieved dynamically from your Redis cache.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

Additional Resources

For more information about using Spring Boot applications on Azure, see the following articles:

For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.

For more information about getting started using Redis Cache with Java on Azure, see How to use Azure Redis Cache with Java.

The Spring Framework is an open-source solution that helps Java developers create enterprise-level applications. One of the more-popular projects that is built on top of that platform is Spring Boot, which provides a simplified approach for creating stand-alone Java applications. To help developers get started with Spring Boot, several sample Spring Boot packages are available at In addition to choosing from the list of basic Spring Boot projects, the Spring Initializr helps developers get started with creating custom Spring Boot applications.