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

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

  • Strings
  • Lists
  • Sets
  • SortedSet
  • Hashes
  • Bit Arrays
  • Streams
  • HyperLogLogs
  • Bitmaps(BitStrings)

Redis Strings

Redis strings contain raw strings that contain a sequence of characters. The 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

SET key valueAssign the value to the key
GET keyRetrieve the value for the given key
DEL keyDeletes the given key and value from a cache

Here is an add, get and delete the keys in Redis interactive shell> set name anderw
OK> get name
"anderw"> keys *
1) "name"> del name
(integer) 1

Lists in Redis

The list is used to store the 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 perform better than retrieving the elements.

Following is a list of operations in the Redis list.

LPUSH key valueadd an element to the left end of a list
RPUSH key valueadd an element to right end of a list
RANGES key start endRetrieve the list of values with start and index items for a given key,0 is a start and -1 is the end index
LPOP keydelete an element from the left end of a list
RPOP keyremove an element from the right end of a list
LINDEX key indexRetrieves 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 a roles of type lists by adding left to the end of the list. Since It is an empty list added admin as one of the elements.> lpush roles admin
(integer) 1

use lrange to retrieve a range of items in a list. 0 is starting index, and -1 is the end index of a list.> lrange roles 0 -1
1) "admin"

Lets add a few more elements to the left and right end of a list> lpush roles sales
(integer) 2> lrange roles 0 -1
1) "sales"
2) "admin"> rpush roles finance
(integer) 3> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"> rpush roles support
(integer) 4> rpush roles hr
(integer) 5> rpush roles billing
(integer) 6> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"
4) "support"
5) "hr"
6) "billing"

Next, get an element from a list using an index with lindex> lindex roles 0
"sales"> lindex roles 1

you can remove elements from the list using rpop or lpop> rpop roles
"billing"> lrange roles 0 -1

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

Redis List Types

Redis Sets Types

List are used to store similar list items. also called Arrays.