| title | Quickstart: Use Azure Managed Redis in Java | |||||||
|---|---|---|---|---|---|---|---|---|
| description | In this quickstart, you create a new Java app that uses Azure Managed Redis | |||||||
| author | KarlErickson | |||||||
| ms.author | karler | |||||||
| ms.reviewer | zhihaoguo | |||||||
| ms.date | 05/18/2025 | |||||||
| ms.topic | quickstart | |||||||
| ms.custom |
|
|||||||
| ms.devlang | java |
In this quickstart, you incorporate Azure Managed Redis into a Java app using the Jedis Redis client. Your cache is a secure, dedicated cache that is accessible from any application within Azure.
Clone the repo Java quickstart on GitHub.
- Azure subscription - create one for free
- Apache Maven
[!INCLUDE managed-redis-create]
The following steps show you how to set up the working environment for the Java app.
export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
export REDIS_CACHE_PORT=10000Replace the placeholders with the following values:
-
<your-host-name>: The DNS host name. To get the host name and ports for your cache, select Overview from the Resource menu. The host name is of the form<DNS name>.redis.cache.windows.net.:::image type="content" source="includes/media/redis-cache-access-keys/redis-cache-hostname-ports.png" alt-text="Screenshot showing Azure Cache for Redis properties.":::
-
<your-client-id>: The application (client) ID of your Azure AD application registration. -
<your-client-secret>: The client secret of your Azure AD application registration. -
<your-tenant-id>: Your Azure Active Directory tenant ID.[!NOTE] The above example uses client secret authentication. You can also configure the
redis-authx-entraidlibrary to use other authentication methods such as managed identity or client certificate by modifying theEntraIDTokenAuthConfigBuilderconfiguration in your code.
-
Use maven to generate a new quickstart app:
mvn archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=1.3 \ -DinteractiveMode=false \ -DgroupId=example.demo \ -DartifactId=redis-jedis-test \ -Dversion=1.0 -
Change to the new redis-jedis-test project directory.
-
Open the pom.xml file. In the file, you see a dependency for Jedis:
[!NOTE] Microsoft has entered into a partnership with Redis, Inc. As part of this collaboration, Microsoft Entra ID authentication support has been moved from Azure SDK to Redis Entra ID extensions. The new
redis-authx-entraidlibrary provides enhanced authentication capabilities and is the recommended approach for Microsoft Entra ID authentication with Azure Managed Redis.<dependency> <groupId>redis.clients.authentication</groupId> <artifactId>redis-authx-entraid</artifactId> <version>0.1.1-beta2</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>6.0.0</version> </dependency>
-
Close the pom.xml file.
-
Open App.java and see the code with the following code:
package example.demo; import com.azure.identity.DefaultAzureCredentialBuilder; import redis.clients.authentication.core.TokenAuthConfig; import redis.clients.authentication.entraid.AzureTokenAuthConfigBuilder; import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.authentication.AuthXManager; import java.util.Set; /** * Redis test with Microsoft Entra ID authentication using redis-authx-entraid * For more information about Redis authentication extensions, see: * https://redis.io/docs/latest/develop/clients/jedis/amr/ * */ public class App { public static void main( String[] args ) { String REDIS_CACHE_HOSTNAME = System.getenv("REDIS_CACHE_HOSTNAME"); int REDIS_PORT = Integer.parseInt(System.getenv().getOrDefault("REDIS_CACHE_PORT", "10000")); String SCOPES = "https://redis.azure.com/.default"; // The scope for Azure Managed Redis // Build TokenAuthConfig for Microsoft Entra ID authentication TokenAuthConfig tokenAuthConfig = AzureTokenAuthConfigBuilder.builder() .defaultAzureCredential(new DefaultAzureCredentialBuilder().build()) .scopes(Set.of(SCOPES)) .tokenRequestExecTimeoutInMs(2000) .build(); DefaultJedisClientConfig config = DefaultJedisClientConfig.builder() .authXManager(new AuthXManager(tokenAuthConfig)) .ssl(true) .build(); UnifiedJedis jedis = new UnifiedJedis( new HostAndPort(REDIS_CACHE_HOSTNAME, REDIS_PORT), config); // Test the connection System.out.println(String.format("Database size is %d", jedis.dbSize())); // Simple PING command System.out.println( "\nCache Command : Ping" ); System.out.println( "Cache Response : " + jedis.ping()); // Simple get and put of integral data types into the cache System.out.println( "\nCache Command : GET Message" ); System.out.println( "Cache Response : " + jedis.get("Message")); System.out.println( "\nCache Command : SET Message" ); System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!")); // Demonstrate "SET Message" executed as expected... System.out.println( "\nCache Command : GET Message" ); System.out.println( "Cache Response : " + jedis.get("Message")); jedis.close(); } }
This code shows you how to connect to an Azure Managed Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The
PINGcommands are also executed. -
Close the App.java file.
Execute the following Maven command to build and run the app:
mvn compile exec:java -D exec.mainClass=example.demo.AppIn the following output, you can see that the Message key previously had a cached value. The value was updated to a new value using jedis.set. The app also executed the PING commands.
Cache Command : Ping
Cache Response : PONG
Cache Command : GET Message
Cache Response : Hello! The cache is working from Java!
Cache Command : SET Message
Cache Response : OK
Cache Command : GET Message
Cache Response : Hello! The cache is working from Java!
[!INCLUDE redis-cache-resource-group-clean-up]
In this quickstart, you learned how to use Azure Managed Redis from a Java application.