Introduction to Redis

The most popular key-value store, Redis is.

Why Redis?

Redis is cool because it (1) is crazy fast, (2) lets you store just about anything at a key, and (3) provides safe, atomic operations. Use Redis for things like caching, message queues, counters, scoreboards, and session data.

Getting Started

You can play with Redis in an online sandbox or install a redis server on your own machine (on macOS, brew install redis) The online sandbox has a small tutorial. You can also work through the Redis tutorial by TutorialsPoint.

After installing, run redis-server (preferably in the background), then run redis-cli to start entering commands.

Some Basic Commands

Using Redis is all about issuing commands. Try these out:

$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set favoritecolor blue
OK
127.0.0.1:6379> set classyear senior
OK
127.0.0.1:6379> set count 50
OK
127.0.0.1:6379> incr count
(integer) 51
127.0.0.1:6379> get count
"51"
127.0.0.1:6379> incr x
(integer) 1

Data Types

Five types:

String
Strings up to 512MB in length. Some commands:
  • GET SET MGET MSET GETSET
  • GETRANGE SETRANGE
  • INCR DECR INCRBY
  • STRLEN APPEND
  • GITBIT SETBIT BITCOUNT BITOP BITPOS BITFIELD
List
Lists of strings. At most 4,294,967,295 elements. Near constant-time access at ends; linear time access in the middle. No semantic difference between empty list and nonexistent key. Some commands:
  • LPUSH RPUSH LPOP RPOP RPOPLPUSH
  • LLEN LINDEX LSET LRANGE LREM LINSERT LTRIM
  • BLPOP BRPOP BRPOPLPUSH
Set
Unordered collections of strings. At most 4,294,967,295 elements. Near constant-time access everywhere. Some commands:
  • SADD SREM SPOP SRANDMEMBER SMEMBERS
  • SCARD SISMEMBER
  • SUNION SINTER SDIFF SUNIONSTORE SINTERSTORE SDIFFSTORE
  • SMOVE SSCAN
Hash
Maps with string keys and string values. At most 4,294,967,295 key-value pairs.
  • HGET HMGET HGETALL HSET HSETNX HMSET HDEL
  • HLEN HEXISTS HSTRLEN
  • HKEYS HVALS
  • HINCRBY HINCRBYFLOAT
  • HSCAN
Sorted Set
Unordered collections of strings where each string has an associated score (used as a sort key). Logarithmic-time access.

Advanced Commands

You should be aware of, and consult frequently, the official documentation on all of the Redis commands.

Programming Redis

So how to write an application that uses Redis? Let’s look at a couple examples.

Using Redis with Python

You can use this Redis library.

redis-demo.py
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)

print(r.set("color", "purple"))
print(r.incrby("dogs", 3))
print(r.get("dogs"))
print(r.lpush("friends", "lauren"))
print(r.rpush("friends", "josh"))
print(r.hmset("words", {"blue": "azul", "green": "verde", "red": "rojo"}))
print(r.hget("words", "green"))
print(r.hkeys("words"))
print(r.lrange("friends", 0, -1))

Using Redis with JavaScript

You can use this Redis client for Node.

redis-demo.js
const redis = require('redis');
const client = redis.createClient({host: 'localhost', port: 6379, db: 0});

client.on('error', error => {
  console.error(error);
});

client.set('color', 'purple', (err, reply) => console.log(reply));
client.incrby('dogs', 3, (err, reply) => console.log(reply));
client.get('dogs', redis.print);
client.get('ThisWillFailWithTooManyArgsToGet', 'extra', 'Arguments', redis.print);
client.lpush('friends', 'lauren', redis.print);
client.rpush('friends', 'josh', redis.print);
client.hmset('words', 'blue', 'azul', 'green', 'verde', 'red', 'rojo');
client.hget('words', 'green', redis.print);
client.hkeys('words', redis.print);
client.lrange('friends', 0, -1, redis.print);
client.quit();