Welcome to the first lesson of our Redis course! In this unit, we'll begin with the essentials — setting up and connecting to a Redis server. Understanding how to establish this connection is crucial as it forms the foundation of all Redis operations.
By the end of this lesson, you’ll be confident in setting up a Redis server, connecting to it, and verifying that connection through simple operations using Java.
Before installing and setting up Redis, let's first understand what exactly it is.
Redis, which stands for Remote Dictionary Server, is an open-source, in-memory data structure store widely used for its speed and flexibility. As a key-value database, Redis offers support for various data structures such as strings, lists, sets, hashes, bitmaps, and hyperloglogs. Its in-memory nature ensures high performance and low latency, making it an ideal choice for applications that require real-time data processing and retrieval. Redis is designed to be versatile and is often used for caching, session management, real-time analytics, and even as a message broker with its support for Pub/Sub messaging paradigms.
Beyond its data storage capabilities, Redis offers persistence through snapshotting or append-only file (AOF) logs, allowing data to be partially stored on disk. This ensures durability while maintaining quick access times. Redis also supports replication, partitioning, and lua scripting, enhancing its robustness and scalability for distributed systems. With its extensive feature set and active community, Redis has become a popular choice among developers for building responsive, data-driven applications.
Before we can start using Redis with Java, we need to install and set up the Redis server. Follow these steps to get Redis running on your system:
-
Installing Redis:
- On Linux:
Bash
1sudo apt update 2sudo apt install redis
- On macOS:
Bash
1brew install redis
- On Windows: You can download the latest Redis binaries for Windows from the Microsoft Open Tech GitHub repository.
- On Linux:
-
Starting Redis:
- Start the Redis server:
Bash
1redis-server
- To verify that Redis is running, use the
redis-cli
:If the server is running, you should seeBash1redis-cli ping
PONG
.
- Start the Redis server:
Redis is now ready to accept connections!
To interact with Redis from Java, we'll use Jedis, a Java client library for Redis. Add Jedis to your project using either Maven or Gradle.
-
Using Maven: Add this dependency to your
pom.xml
:HTML, XML1<dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>4.3.1</version> 5</dependency>
-
Using Gradle: Add this line to the
dependencies
section of yourbuild.gradle
:Groovy1implementation 'redis.clients:jedis:4.3.1'
After adding the dependency, your build tool will download and make Jedis available in your project. In the practice section, the setup and installation will already be done for you, allowing you to focus on learning how to use Jedis effectively.
Now that Redis is running and Jedis is installed, here’s a simple Java program to connect to the Redis server and perform basic operations:
Java1import redis.clients.jedis.Jedis; 2 3public class Main { 4 public static void main(String[] args) { 5 // Connect to the Redis server 6 Jedis jedis = new Jedis("localhost", 6379); 7 8 // Verify the connection by setting and getting a value 9 jedis.set("name", "Redis Learner"); 10 System.out.println("Stored string in Redis: " + jedis.get("name")); 11 12 // Close the connection 13 jedis.close(); 14 } 15}
-
Connecting to Redis:
new Jedis("localhost", 6379)
connects to a Redis server running on your local machine (localhost) at the default Redis port,6379
. -
Performing Basic Operations:
jedis.set("name", "Redis Learner")
: Stores a key-value pair in Redis.jedis.get("name")
: Retrieves the value associated with the key"name"
.
-
Closing the Connection:
jedis.close();
is used to explicitly close the connection to the Redis server, ensuring that all resources are properly released.
When you run this program, the output will be:
1Stored string in Redis: Redis Learner
This confirms that the connection was successful and Redis is working as expected.
Setting up and establishing a connection to a Redis server is the first step in using the various features Redis has to offer, from fast data access to caching and message brokering. Without this fundamental step, you wouldn't be able to use Redis effectively. Knowing how to connect to a Redis server will enable you to start experimenting with Redis's powerful features, such as data structures and atomic operations.
Are you ready to get started? Let's dive into the practice section and make sure you can connect to a Redis server seamlessly.