This is an introductory guide to Redis. You will learn how to install, run, and test the Redis server process.
Installing Redis
How you install Redis depends on your operating system. See below for the guide that best suits your needs:
Once you have Redis up and running and can connect using redis-cli, you can continue with the following steps.
Exploring Redis with the CLI
External programs talk to Redis using the TCP interface and a Redis-specific protocol. This protocol is available in the Redis client for different programming languages. To make Redis even simpler, Redis provides a useful command-line program for sending commands to Redis. This program is called redis-cli.
To check if Redis is working properly, first send the PING command using redis-cli:
$ redis-cli ping
PONG
After running redis-cli, this command is sent to the redis instance running on the host (port 6379) based on the command name and its parameters. You can change the host and port used by redis-cli, just try the -help option to check the usage information.
Another interesting way to run redis-cli is without parameters: the program will start in interactive mode. You can type different commands and see the replies.
$ redis-cli
redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey somevalue
OK
redis 127.0.0.1:6379> get mykey
"somevalue"
At this point, you can talk to Redis. You can pause this tutorial to startA 15-minute introduction to Redis data types(math.) genusin order to learn some Redis commands.
If you already know some basic Redis commands, you can continue.
Protecting Redis
By default, Redis is bound to all interfaces and requires no authentication. This is great if you are using Redis in a very controlled environment, isolated from external networks and attacks. However, if an unhardened Redis is exposed to the network, there is a significant security risk. If you can't 100% determine whether your environment is secure, check the following steps to make Redis more secure, which are listed in order of increasing security.
1) Ensure that the port used by Redis to listen for connections (6379 by default, 16379 if running Redis in cluster mode, and 26379 for Sentinel) has a firewall installed to ensure that no connections can be made to Redis from an external environment.
2) Use a configuration file with the bind directive set to ensure that Redis responds only to the network interface you are using. For example, if you are only accessing Redis locally from the same computer, use only the loopback interface (127.0.0.1), and so on.
3) Use the requirepass option to add an additional layer of security in case the client needs to authenticate using the AUTH command.
4) If your environment requires encryption, use spiped or other SSL tunneling software to encrypt the communication between the Redis server and the Redis client.
Note that a Redis instance exposed to the Internet without any security can be easily exploited, so make sure you are aware of this and apply at least one layer of firewall. Once the firewall is in place, try connecting with redis-cli from an external host to prove to yourself that you cannot access the instance.
Using Redis in your application
It is not enough to use Redis from the command line interface, the goal is to use it in your application. To do this, you need to download and install a Redis client library for your programming language. You can download and install a Redis client library for your programming language in theThis pageFind a complete list of clients in different languages on the
For example, if you happen to use the Ruby programming language, our best advice is to use theRedis-rbClient. You can install it using the gem install redis command.
These instructions are for Ruby, but in fact many of the mainstream language clients look very similar. If you want to create a Redis object and run the call method commands, see the following short interactive example in Ruby.
>> require 'rubygems'
=> false
>> require 'redis'
=> true
>> r = Redis.new
=> #
>> r.ping
=> "PONG"
>> r.set('foo','bar')
=> "OK"
>> r.get('foo')
=> "bar"