• Redis server v=5.0.7


The Redis String type is the simplest type of value you can associate with a Redis key.

Redis keys are strings, when we use the string type as a value too, we are mapping a string to another string.

つまり mapping a string to another string ということで、僕らがKey-Value StoreのNoSQLと聞いて連想するそれですね。> set key1 value ex 10
OK> get key1
"value"> ttl key1
(integer) 5> get key1


Redis Lists are simply lists of strings, sorted by insertion order.

Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time. The speed of adding a new element with the LPUSH command to the head of a list with ten elements is the same as adding an element to the head of list with 10 million elements.


Remember the latest updates posted by users into a social network.

Communication between processes, using a consumer-producer pattern where the producer pushes items into a list, and a consumer (usually a worker) consumes those items and executed actions.


The popular Twitter social network takes the latest tweets posted by users into Redis lists.

ユーザー毎にKeyを用意して、最新アクションをListsに格納するイメージでしょうか。> rpush list-key value1       # Push onto end
(integer) 1> rpush list-key value2       # Push onto end
(integer) 2> lrange list-key 0 -1       # Access range
1) "value1"
2) "value2"> lindex list-key 0       # Access by index
"value1"> lpush list-key value0       # Push onto start
(integer) 3> lrange list-key 0 -1
1) "value0"
2) "value1"
3) "value2"> rpop list-key       # Pop from end
"value2"> lrange list-key 0 -1
1) "value0"
2) "value1"


Redis Sets are an unordered collection of Strings.

Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element.


Want to know all the unique IP addresses visiting a given blog post? Simply use SADD every time you process a page view. You are sure repeated IPs will not be inserted.> sadd set-key AAA BBB CCC       # Add item
(integer) 3> smembers set-key       # Get all
1) "BBB"
2) "CCC"
3) "AAA"> sadd set-key CCC DDD       # Add item
(integer) 1> smembers set-key       # Get all
1) "BBB"
2) "CCC"
3) "AAA"
4) "DDD"


Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects

Key-ValueValueに、連想配列を格納できるもの。一組のKey-ValueValueの中に、さらに複数組のKey-Value(sub-key, sub-valueとも言える)をできる、というイメージでしょうか。> hset hash-key sub-key1 sub-value1       # Set item
(integer) 1> hget hash-key sub-key1       # Get item
"sub-value1"> hset hash-key sub-key2 sub-value2
(integer) 1> hkeys hash-key       # Return all keys
1) "sub-key1"
2) "sub-key2"

Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

Hashでは、登録できる連想配列Valueに自由な値を指定できましたが、Sorted Setsの場合、数値を登録する事となります。登録された数値により、サブKeyはソートされて管理されます。


Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK.> zadd zset-key 100 yukari
(integer) 1> zadd zset-key 50 maki
(integer) 1> zrange zset-key 0 100
1) "maki"
2) "yukari"> zrank zset-key yukari
(integer) 1