Redis stores key and value pairs in data structure server. It contains unique keys to each data values.

Key only contains string type. values contains following data types in Redis.

  • Strings
  • Lists
  • Sets
  • SortedSet
  • Hashes
  • HyperLogLogs
  • Bitmaps(BitStrings)

Redis Strings

Redis strings contains raw strings that contains sequence of characters. Allowed maximum string size is 512 megabytes in length. It stores any sequence of data and it is binary safe. Following commands used to interact with strings process in Redis

Command   Description  
SET key value Assign the value to the key
GET key Retrieve the value for given key
DEL key Deletes the give key and value from a cache

Here is an add,get and delete the keys in Redis interactive shell

127.0.0.1:6379> set name anderw
OK
127.0.0.1:6379> get name
"anderw"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> del name
(integer) 1

Lists in Redis

List is used to store sequence of strings as values in insertion order for a given key.

List elements can be inserts head and tail, with linked strings. It is also called LinkedList.

Adding and insertion to the list performs better than retrieving the elements.

Following are list of operations in redis list.

Command   Description  
LPUSH key value add an element to left end of an list
RPUSH key value add an element to right end of an list
RANGES key start end Retrieve the list of values with start and index items for a given key,0 is start and -1 is end index
LPOP key delete an element from left end of an list
RPOP key remove an element from right end of an list
LINDEX key index Retrieves the element from the given key at index position

Let’s see an examples

roles as keys values as admin,sales,hr,support,finance

let’s create an roles of type lists with adding left to end of list. Since It is empty list added admin as one of the element.

127.0.0.1:6379> lpush roles admin
(integer) 1

use lrange to retrieve range of items in a list. 0 is starting index, -1 is end index of an list.

127.0.0.1:6379> lrange roles 0 -1
1) "admin"

Lets add few more element to left and right end of a list

127.0.0.1:6379> lpush roles sales
(integer) 2
127.0.0.1:6379> lrange roles 0 -1
1) "sales"
2) "admin"


127.0.0.1:6379> rpush roles finance
(integer) 3
127.0.0.1:6379> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"
127.0.0.1:6379> rpush roles support
(integer) 4
127.0.0.1:6379> rpush roles hr
(integer) 5
127.0.0.1:6379> rpush roles billing
(integer) 6
127.0.0.1:6379> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"
4) "support"
5) "hr"
6) "billing"

Next, get an element from a list using index with lindex

127.0.0.1:6379> lindex roles 0
"sales"
127.0.0.1:6379> lindex roles 1
"admin"

you can remove elements from list usig rpop or lpop

127.0.0.1:6379> rpop roles
"billing"
127.0.0.1:6379> lrange roles 0 -1

1) "sales"
2) "admin"
3) "finance"
4) "support"
5) "hr"