1. Introduction
In this lecture, we’ll learn how to install and set up Redis, understand what happens during installation, and how to interact with the Redis server using the Redis CLI.
We’ll also look at how to run Redis inside a Docker container, which is a very common and convenient setup for developers.
2. Installing Redis
When you install Redis, the installation process sets up two main components:
- Redis Server (
redis-server
) – The main process that stores data in memory and handles all client requests. - Redis CLI (
redis-cli
) – A command-line interface used to interact with the Redis server.
Once installed, Redis runs a lightweight, in-memory database that can be accessed using the CLI or a programming language client library.
By default, the Redis server listens on port 6379
, which is the standard Redis port.
Although you can change this in the configuration file, for now, we’ll keep it as 6379
.
3. Redis Server Overview
When the Redis server (redis-server
) is running:
- It listens for client connections on port 6379.
- It maintains all data in memory for fast access.
- It supports multiple data structures such as Strings, Lists, Sets, Hashes, and Sorted Sets.
Redis does not use a declarative query language like SQL.
Instead, it uses an imperative command-based interface, where each operation is a direct command (for example, SET
, GET
, DEL
, etc.).
You don’t have to memorize all the commands — Redis provides a comprehensive command reference and cheat sheet for quick lookup.
4. Interacting with Redis: The CLI (redis-cli
)
The Redis CLI (redis-cli
) is the simplest way to interact with a Redis server.
It allows you to issue commands directly to the server and see the results immediately.
For example:
SET name "James" GET name
Output:
"James"
You can use it for learning, debugging, and administrative purposes.
If Redis is installed on your machine, you can start the server and CLI as follows:
- Start Redis Server
redis-server
This launches the Redis server and begins listening on port 6379. - Connect using Redis CLI
redis-cli
Once connected, you can start issuing commands to interact with the running Redis instance.
If you see a message like:
127.0.0.1:6379>
it means your CLI is successfully connected to the Redis server.
5. Running Redis using Docker
If you don’t want to install Redis directly on your system, you can easily run it using Docker.
This is often the preferred method for development setups because it keeps your environment isolated and easy to manage.
Step 1: Create a docker-compose.yml
file
Create a file named docker-compose.yml
in a folder of your choice, and add the following content:
version: '3.8' services: redis: image: redis:6.2 container_name: redis ports: - "6379:6379"
Explanation:
- image: Specifies the Redis image version to use (
redis:6.2
). - container_name: The name assigned to the container (here,
redis
). - ports: Maps Redis container port
6379
to host port6379
, allowing external clients to connect vialocalhost:6379
.
Step 2: Start the container
In the same directory as your docker-compose.yml
file, run:
docker-compose up -d
This command downloads the Redis image (if not already available) and runs the container in detached mode.
You can verify that Redis is running using:
docker ps
You should see a container named redis
listening on port 6379.
6. Connecting to Redis CLI inside the Docker Container
Since Redis is running inside a Docker container, you need to access the CLI from within the container to interact with it.
Run the following command to open an interactive shell inside the Redis container:
docker exec -it redis bash
You are now inside the Redis container.
From here, you can launch the Redis CLI:
redis-cli
If everything is working correctly, you should see the Redis prompt:
127.0.0.1:6379>
This confirms that the CLI has successfully connected to the Redis server running inside Docker.
7. Testing the Redis Setup
Now that you have Redis and its CLI running, let’s test it with some simple commands.
SET language "Java" GET language
Expected output:
"Java"
You can also try other operations:
INCR counter INCR counter GET counter
Output:
(integer) 1 (integer) 2 "2"
These commands verify that your Redis server is working correctly and storing data in memory.
8. Stopping the Redis Server
If you’re running Redis via Docker Compose, you can stop it at any time by running:
docker-compose down
This will gracefully shut down and remove the Redis container.
If you started it manually using docker run
, you can stop it using:
docker stop redis
9. Summary
Let’s quickly recap what we covered in this session:
- Redis installation includes both the server (
redis-server
) and the CLI (redis-cli
). - By default, Redis listens on port 6379.
- Redis uses imperative commands (not SQL) to manipulate data directly in memory.
- You can easily run Redis in Docker using a simple
docker-compose.yml
file. - The CLI can be accessed inside the container using:
docker exec -it redis bash redis-cli
- Once connected, you can execute Redis commands to store and retrieve data instantly.
Redis is now up and running, and you’re ready to explore its data types, persistence options, and more advanced features in future lessons.