Quickstart: How to use Azure Cache for Redis with Java

Azure Cache for Redis gives you access to a dedicated Azure Cache for Redis, managed by Microsoft. Your cache is accessible from any application within Microsoft Azure.

This article shows you how to get started with Azure Cache for Redis using the Jedis Redis client for Java.

Cache app completed

You can use any code editor to complete the steps in this quickstart. However, Visual Studio Code is an excellent option available on the Windows, macOS, and Linux platforms.

If you don't have an Azure subscription, create a free account before you begin.


Apache Maven

Create an Azure Cache for Redis

  1. To create a cache, first sign in to the Azure portal. Then select Create a resource > Databases > Azure Cache for Redis.

    New cache

  2. In New Azure Cache for Redis, configure the settings for your new cache.

    Setting Suggested value Description
    DNS name Globally unique name The cache name. It must be a string between 1 and 63 characters and contain only numbers, letters, and the - character. The cache name cannot start or end with the - character, and consecutive - characters are not valid.
    Subscription Your subscription The subscription under which this new Azure Cache for Redis instance is created.
    Resource group TestResources Name for the new resource group in which to create your cache. By putting all the resources for an app in a group, you can manage them together. For example, deleting the resource group deletes all resources that are associated with the app.
    Location East US Choose a region near to other services that will use your cache.
    Pricing tier Basic C0 (250 MB Cache) The pricing tier determines the size, performance, and features that are available for the cache. For more information, see Azure Cache for Redis Overview.
    Pin to dashboard Selected Pin the new cache to your dashboard to make it easy to find.

    Create cache

  3. After the new cache settings are configured, select Create.

    It can take a few minutes for the cache to be created. To check the status, you can monitor the progress on the dashboard. After the cache has been created, it shows the status Running, and is ready for use.

    Cache created

Retrieve host name, ports, and access keys by using the Azure portal

When connecting to an Azure Cache for Redis instance, cache clients need the host name, ports, and a key for the cache. Some clients might refer to these items by slightly different names. You can retrieve this information in the Azure portal.

To retrieve the access keys and host name

  1. To retrieve the access keys by using the Azure portal, browse to your cache and select Access keys.

    Azure Cache for Redis keys

  2. To retrieve the host name and ports, select Properties.

    Azure Cache for Redis properties

Add environment variables for your HOST NAME and Primary access key. You will use these variables from your code instead of including the sensitive information directly in your code.

set REDISCACHEHOSTNAME=contosoCache.redis.cache.windows.net

Create a new Java app

Using Maven, generate a new quickstart app:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.3 -DgroupId=example.demo -DartifactId=redistest -Dversion=1.0

Change to the new redistest project directory.

Open the pom.xml file and add a dependency for Jedis:


Save the pom.xml file.

Open App.java and replace the code with the following code:

package example.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;

 * Redis test
public class App 
    public static void main( String[] args )

        boolean useSsl = true;
        String cacheHostname = System.getenv("REDISCACHEHOSTNAME");
        String cachekey = System.getenv("REDISCACHEKEY");

        // Connect to the Azure Cache for Redis over the SSL port using the key.
        JedisShardInfo shardInfo = new JedisShardInfo(cacheHostname, 6380, useSsl);
        shardInfo.setPassword(cachekey); /* Use your access key. */
        Jedis jedis = new Jedis(shardInfo);      

        // Perform cache operations using the cache connection object...

        // 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"));

        // Get the client list, useful to see if connection list is growing...
        System.out.println( "\nCache Command  : CLIENT LIST" );
        System.out.println( "Cache Response : " + jedis.clientList());


This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The PING and CLIENT LIST commands are also executed.

Save App.java.

Build and run the app

Execute the following Maven command to build and run the app:

mvn compile
mvn exec:java -D exec.mainClass=example.demo.App

In the example below, you can see the Message key previously had a cached value, which was set using the Redis Console in the Azure portal. The app updated that cached value. The app also executed the PING and CLIENT LIST commands.

Cache app completed

Clean up resources

If you will be continuing to the next tutorial, you can keep the resources created in this quickstart and reuse them.

Otherwise, if you are finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.


Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually from their respective blades instead of deleting the resource group.

Sign in to the Azure portal and click Resource groups.

In the Filter by name... textbox, type the name of your resource group. The instructions for this article used a resource group named TestResources. On your resource group in the result list, click ... then Delete resource group.


You will be asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and click Delete.

After a few moments, the resource group and all of its contained resources are deleted.

Next steps

In this quickstart, you learned how to use Azure Cache for Redis from a Java application. Continue to the next quickstart to use Azure Cache for Redis with an ASP.NET web app.