Thiago P.

Redis - Basics

Written in Systems, Databases

← back to the notes


Redis an in-memory database. Datum are stored as key-value pairs in RAM memory. While data is stored in memory, persistence can be enabled through snapshots, where Redis will periodically take snapshots of your data and save it to disk. In the case of some failure, you will be able to recover your previous data while only losing the last few minutes of data.

Ubuntu Install

sudo apt-get update

sudo apt-get install redis-server

redis-server # to start the server

redis-cli  # to enter the redis cli


Data Types and Commands

Command Reference


The simplest data type.

SET key value

GET key value

In reality, you can store anything you want as a string, so long as it can be represented as a byte array. The maximum size of a string value is 512mb


A list of strings (byte array) sorted by insertion order

LPUSH mylist a   # now the list is "a"
LPUSH mylist b   # now the list is "b","a"
RPUSH mylist c   # now the list is "b","a","c" (RPUSH was used this time)

The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).

Lists provide constant time insertion and deletion at the head and tail as well as quick element access by index.


Sets are an unordered collection where each element must be unique. Attempting to add the same element multiple times is a no-op.

SADD friends:leto ghanima
SADD friends:leto duncan
SADD friends:paul duncan
SADD friends:paul gurney
SINTER friends:leto friends:paul

The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).

One interesting use could be to track the number of unique ip addresses that visit a site.

Sorted Sets

A set of unique members which are sorted by score field. Multiple entries may have the same score.

Hash (map)

A mapping between key and value pairs, referenced by a single key (essentially an object)

HMSET key prop1 value1 prop2 value2 prop3 value3 # set multiple properties on this Hash
HGETALL key # get all properties on this Hash

HSET key prop value # set a single property
HGET key prop # get a single property

Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).