NOTE: For each of the data-types Redis offers a large set of commands. We’ll be exploring most used commands. If you want to refer the complete list of available commands, you can refer Redis commands here.

STRING:

We store STRING as a value of a key(ID) as shown below.

We have three widely used commands for STRING data types.

SET : Sets the value stored for the given key GET : Retrieves the data stored at the given key DEL : Deletes the value stored at the given key

STRING Data Type Operations

2. LIST:

Redis supports linked-list structure and offers all the basic commands(LPUSH, RPUSH, LPOP, RPOP etc) we expect with a linked-list. We can consider values as a list of items. Please note, lists can have duplicate items in value as shown below.

Following commands are widely used for Redis LIST:

RPUSH : Push item on right side of list/initialize a list LPUSH : Push item on left side of list/initialize a list LPOP : Pop an item from left side of list RPOP : Pop an item from right side of list LRANGE : Fetches all the data from a list for a given range of index LINDEX : Fetches data from a list at a given index

TIP: All commands starts with L.

Redis LIST data type commands

3. SET:

SET is very similar LIST, but the items in a SET has to be unique. SETs stores a set of items in un-ordered fashion. Thus, we can’t have PUSH or POP commands with SETs.

Redis SET

Following commands are widely used for Redis SET:

SADD : Add an item to SET/initialize SET SMEMBERS : Fetches all items for a key in SET SISMEMBER : Checks if an item is in a SET SREM : Removes an item from SET, if exists

TIP: All commands starts with S.

Redis SET Data type commands

4. HASH

Hash is one of the most widely used data structure in Ruby. LIST and SET stores the sequence of items, whereas Redis HASH gives us a true flavor of Hashes(Dictionary) we use in our day to day programming.

NOTE: 1. Even HASH are stored with a KEY identifiers. You can think of it as ID or variable name to hold the HASH. In Ruby we define a HASH variable as:

2.4.0 :001 > my_hash = { key1: ‘value1’, key2: ‘value2’ }

=> {:key1=>”value1", :key2=>”value2"}

2.4.0 :002 > my_hash[:key1]

=> “value1”

This variable my_hash is the variable, which holds the HASH. Similarly, in Redis, we define HASH with a name(Main key) and add the sub key-value pair to this HASH.

NOTE: 2. The value of sub key-value (i.e. actual hash) is always a String. Though it can be interpreted as numbers and can be incremented/decremented.

Following commands are widely used for Redis HASH:

HSET : Stores the value at the key for a given hash/initializes a HASH HGET : Retrieves the value at a given hash key for a given HASH HGETALL : Retrieves the entire HASH for a given HASH name HDEL : Deletes a key from a given hash, if it exists

TIP: All commands for HASH starts with H.

Redis HASH Data Type commands

5. ZSET or Ordered SET

ZSET is an ordered key-value pair data-structure. The keys are called members and the values(limited to floating point numbers) are called scores. Think of this as class rooms in a school. Class 1(Main Key for ZSET) has many members (students). Each student scored between 0 to 100 in Math tests. You can add/remove students from the Class 1. You can fetch all the students for a given score range.

A few commands which can be used with ZSET:

ZADD : Add one/more member for a given score to ZSET/initialize a ZSET with one/more member ZRANGE : Fetches all the items in the ZSET from their position in sorted order ZRANGEBYSCORE : Fetches items in the ZSET based on range of scores ZREM : Removes an item from the ZSET, if exists

TIP: All commands for ZSET starts with Z.

As you can see, ZSET gives us a lot of abilities for Ranking, Querying and perform basic CRUD over ordered dataset.

With these data-types, you can perform a gamut of operations. Due to this variety of data-types, Redis is preferred tool for caching tool for many developers.

Persistence:

Since Redis is in-memory database, you might worry about the situation, when your server gets down. Redis offers two ways to persist your data:

Dumping in-memory data to disk in compact format. Write/append a file with every commands which alters the data on Redis

Hope you enjoyed this post. In future series, I’ll build a few application, where you can utilize these data-types in real.

References: