Azure Cache for Redis provides an in-memory data store based on the Redis software. Redis improves the performance and scalability of an application that uses backend data stores heavily.
This tutorial demonstrates how to use a Redis cache to store and retrieve data in a Spring Boot application.
In this tutorial, we include two authentication methods: Microsoft Entra authentication and Redis authentication. The Passwordless tab shows the Microsoft Entra authentication and the Password tab shows the Redis authentication.
Microsoft Entra authentication is a mechanism for connecting to Azure Cache for Redis using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.
Redis authentication uses passwords in Redis. If you choose to use passwords as credentials, you need to manage the passwords by yourself.
A Spring Boot application. If you don't have one, create a Maven project with the Spring Initializr. Be sure to select Maven Project and, under Dependencies, add the Spring Web and Spring Data Reactive Redis dependencies, and then select Java version 8 or higher.
Caching Data to Azure Cache for Redis
With an Azure Cache for Redis instance, you can cache data using Spring Cloud Azure.
To install the Spring Cloud Azure Starter Data Redis with Lettuce module, add the following dependencies to your pom.xml file:
This Bill of Material (BOM) should be configured in the <dependencyManagement> section of your pom.xml file. This configuration ensures that all Spring Cloud Azure dependencies are using the same version. For more information about the version used for this BOM, see Which Version of Spring Cloud Azure Should I Use.
Code the application
To use a Redis cache to store and retrieve data, configure the application by using the following steps:
Then, start the application. The application retrieves data from your Redis cache. You should see logs similar to the following example:
Output
Add a key is done
Return the value from the cache: Hello World
Deploy to Azure Spring Apps
Now that you have the Spring Boot application running locally, it's time to move it to production. Azure Spring Apps makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see Deploy your first application to Azure Spring Apps.
Next steps
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.
Learn how to use Azure Cache for Redis to cache business data and HTTP session data in a Java Spring Boot application, then publish the application to the cloud.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.