0
0
RedisHow-ToBeginner · 3 min read

How to Use Redis in Java: Simple Guide with Example

To use Redis in Java, you typically use a client library like Jedis. First, add Jedis to your project, then create a Jedis instance to connect to Redis and use commands like set and get to store and retrieve data.
📐

Syntax

Using Redis in Java involves these steps:

  • Import the Jedis library.
  • Create a Jedis client instance to connect to Redis server.
  • Use Redis commands like set and get through the client.
  • Close the connection when done.
java
import redis.clients.jedis.Jedis;

public class RedisExample {
    public static void main(String[] args) {
        // Connect to Redis server running on localhost
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            // Set a key-value pair
            jedis.set("key", "value");

            // Get the value by key
            String value = jedis.get("key");

            // Close connection is automatic with try-with-resources
        } catch (Exception e) {
            System.out.println("Error connecting to Redis: " + e.getMessage());
        }
    }
}
💻

Example

This example shows how to connect to Redis, store a string, retrieve it, and print the result.

java
import redis.clients.jedis.Jedis;

public class RedisExample {
    public static void main(String[] args) {
        // Connect to Redis server on localhost
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            // Store data
            jedis.set("greeting", "Hello, Redis!");

            // Retrieve data
            String value = jedis.get("greeting");

            // Print the value
            System.out.println("Stored value: " + value);
        } catch (Exception e) {
            System.out.println("Error connecting to Redis: " + e.getMessage());
        }
    }
}
Output
Stored value: Hello, Redis!
⚠️

Common Pitfalls

Common mistakes when using Redis in Java include:

  • Not closing the Jedis connection, which can cause resource leaks.
  • Assuming Redis is running locally without verifying the server is up.
  • Using wrong port or host in the connection.
  • Not handling exceptions when Redis is unreachable.

Always use try-with-resources or finally block to close connections.

java
import redis.clients.jedis.Jedis;

// Wrong way: Not closing connection
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("key", "value");
// Connection left open

// Right way: Using try-with-resources
try (Jedis jedis2 = new Jedis("localhost", 6379)) {
    jedis2.set("key", "value");
}
📊

Quick Reference

OperationJedis CommandDescription
Connectnew Jedis(host, port)Create a client connection to Redis server
Set valuejedis.set(key, value)Store a string value by key
Get valuejedis.get(key)Retrieve the string value by key
Closejedis.close()Close the Redis connection

Key Takeaways

Use the Jedis library to connect and interact with Redis in Java.
Always close the Jedis connection to avoid resource leaks.
Handle exceptions to manage Redis server unavailability gracefully.
Use simple commands like set and get to store and retrieve data.
Verify Redis server is running and accessible before connecting.