Redis Open Source

Get Started with Redis

Getting started with Redis is easy.

It takes three simple steps – 1. Setup. 2. Connect. 3. Start to code. At the bottom of this page you’ll find additional developer resources in the Redis Labs community such as online training, videos, blogs, webinars, workshops, meetups, conferences and more.

Step 1 – Setup Redis

There are multiple ways to run Redis. The easiest way to start is to sign-up for the free Redis Cloud service offered by Redis Labs. You can then connect to the Redis Cloud database from your app that runs on any platform (Mac, Windows, Linux), or on cloud or PaaS.

If you prefer having a local development environment, you also have a few options to install Redis on a local server. Depending on your operating system, you can build Redis from scratch, run Redis as a container on Docker, or run Redis as a Homebrew service (on Macs).

Option A: Redis Cloud

Redis Labs offers Redis Cloud as a fully managed database as a service for free for datasets up to 30 MB. You can choose to run the Redis Cloud database as a service on Amazon Web Services, Microsoft Azure or Google Cloud Platform. It is also available in many different regions of the world. Creating a Redis instance with Redis Cloud is a two step process:

  1. Sign-up for a free Redis Cloud account: Visit the Redis Labs Get Started page, and click on SIGN UP under “Cloud Hosted” section.
  2. Setup a database endpoint: Once you create and verify your email address, you can login to your Redis Cloud account and create a new database. Your Redis Cloud database will be password protected. The URL to your endpoint will look like:

redis-11111.c1.us-east-1-1.ec2.cloud.redislabs.com:12345

In this example, 12345 is the port number of your Redis Cloud service.

redis-11111.c1.us-east-1-1.ec2.cloud.redislabs.com is the URL of your Redis service.

Option B: Build from Source

Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD and FreeBSD operating systems. Follow these steps to build Redis from source and start the server.

  1. Download Redis source code from the downloads page
  2. Unzip the file
    tar -xzf redis-VERSION.tar.gz
  3. Compile and build Redis
    cd redis-VERSION
    make
  4. Start Redis
    cd src
    ./redis-server

Option C: Docker

Redis is one of the most popular containers on Docker hub and was the first database container to reach a billion pulls on Docker hub. Running Redis as a Docker container is quite popular on Mac and Windows platforms. fairly straight forward. Follow the commands below to run or stop a Redis container.

  • Run Redis as a container on port 6379
    docker run –name redis -p 6379:6379 -d redis
  • Command to stop Redis
    docker stop redis
    docker rm redis

Option D: Homebrew

Homebrew is a popular package management system for Mac OS. It’s extremely easy to install and manage Redis using Homebrew. If you have Homebrew installed on your Mac, you could run the following commands to install, run and stop Redis.

  • Install Redis
    brew install Redis
  • Run Redis
    brew services start Redis
  • Stop Redis
    brew services stop Redis

Step 2 – Connect to Redis

After you have your Redis instance ready, the next step is to connect to Redis and verify that it’s up and working. The easiest way to do this is to test it with “redis-cli” — the Redis command line interface.

Redis Command Line Interface

Redis command line interface sends commands to Redis, reads the replies, and prints them on the terminal. Redis-cli also gives you options to connect to a Redis endpoint based on IP address and port number, and authenticate by providing the password.

How to install redis-cli

Redis-cli comes packaged with Redis. If you install Redis on your local computer either via building from source, or Docker, or Homebrew, you don’t need an extra step to install redis-cli.
If you are connecting to Redis Cloud using redis-cli, these are the quickest ways to acquire redis-cli based on your OS:

  1. Mac: Install Redis using Homebrew (see instructions above).
  2. Linux: Install Redis from source (see instructions above).
  3. Docker: Install Redis as a Docker container (see instructions above). This is good for Mac and Windows platforms.
  4. Windows: Download and install Redis msi package from MicrosoftArchive page. The package installs redis-cli.exe. Even though it is an older version of Redis, it is compatible with the later versions of Redis Cloud.

How to connect to Redis using redis-cli

Case 1: Redis is running locally (127.0.0.1) with the default port, 6379

$ redis-cli
127.0.0.1:6379>

Case 2: Redis is running remotely (example: 192.168.0.10) on a non-default port (example: 7000)

$ redis-cli -h 192.168.0.10 -p 7000

192.168.0.10:7000>

Case 3: Connecting to Redis Cloud with URL, redis.us-east-1-1.ec2.cloud.redislabs.com, port 12345, password “thisismypassword”

$ redis-cli -h redis.us-east-1-1.ec2.cloud.redislabs.com -p 12345 -a thisismypassword
redis.us-east-1-1.ec2.cloud.redislabs.com:12345>

Case 4: Connecting to Redis Cloud via redis-cli on the Docker container.

If you have Redis running as a Docker container with name, redis, then you can access redis-cli installed inside the container with this command:
$ docker exec -it redis redis-cli

If you are connecting to Redis Cloud, you run redis-cli with appropriate parameters as shown here:
$ docker exec -it redis redis-cli -h

redis.us-east-1-1.ec2.cloud.redislabs.com -p 12345 -a thisismypassword
redis.us-east-1-1.ec2.cloud.redislabs.com:12345>

How to test the connection

Redis-cli will take you to a prompt with the URL (or IP address) and the port number if it can successfully connect to Redis. You can then verify whether the server is responding to your queries with basic commands – 1. Ping – this returns PONG, 2. Set – set foo bar – this returns OK, 3. Get – get foo – this returns bar.

$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
“bar”
127.0.0.1:6379>

If you cannot connect to the Redis server for some reason, then you get an error as shown below.
$ redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

Step 3 – Start Coding

Redis client libraries are available in over 50 programming languages. Choose the appropriate library based on your choice of programming language and follow the client documentation to connect to Redis. Below we demonstrate connecting Redis in Python, Java, and JavaScript (Node.js).

1. Python

Recommended library
redis-py

How to install

You can install redis-py using “pip” or from source. The easiest way to get started is with pip:
$ pip install redis

Sample program

import redis
r = redis.Redis(
host= ‘redis.us-east-1-1.ec2.cloud.redislabs.com’,
port= ‘12345’,
Password = ‘thisismypassword’)
r.set(‘foo’,’bar’)
r.get(‘foo’)

2. Java

Recommended library

Jedis

How to install

You can download the Java source from github, or download the jar from the release page. To use the latest Redis 5.0 features, you can get the snapshot from the development branch. For that you can add the following maven dependency:

<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

Sample program

Jedis jedis = new Jedis(“redis.us-east-1-1.ec2.cloud.redislabs.com”,”12345″);
jedis.auth(“thisismypassword”);
jedis.set(“foo”, “bar”);
String value = jedis.get(“foo”);

3. Node.js

Recommended library

node_redis

How to install

You can install redis-py using “pip” or from source. The easiest way to get started is with pip:
$ npm install redis

Sample program

var redis = require(‘redis’);

var redisHost = ‘redis.us-east-1-1.ec2.cloud.redislabs.com’;
var redisPort = process.argv[3] || 12345;
var redisAuth = ‘thisismypassword’;

var client = redis.createClient ({
port : redisPort,
host : redisHost
});

client.auth(redisAuth, function(err, response){
if(err){
throw err;
}
});

client.set(‘foo’,’bar’);
client.get(‘foo’, function(err, response){
if(err) {
throw err;
}else{
console.log(response);
}
});

More About Client Libraries

Many Redis clients have rich set of features to support connection pooling, pipelining, clustering, synchronous and asynchronous calls, streaming, SSL connections, and so on. Choose your programming language and explore more about the libraries available in your language.

More Resources

There is more than one way to get started with Redis. Explore all our resources for more information.


Online Reading Material

How-to
Redis for Dummies
eBook: Redis in Action
Best Practices Guide

Videos

Videos
RedisConf18
Redis Day Tel Aviv

Redis University

Redis University

Redis Labs Community Resources

Technical blog
Webinars
Events
Meetups