Interview GURU

Which are the different data types used in Redis?

Redis Data Type

There are five types of data types supported by Redis database.
  • Strings
  • Hashes
  • Lists
  • Sets
  • Sorted Sets

Strings

String is a set of bytes. In Redis database, strings are binary safe. It means they have a known length and not determined by any special terminating characters. So it is possible to store anything up to 512 megabytes in one string.

Example
Let's store a string name "Ajeet Kumar" in the key by using SET command and then retrieve the same by using GET command.
SET name "Ajeet Kumar"  
OK  
GET name   
"Ajeet Kumar"  

In the above example, SET and GET are the Redis command, name is the key used in Redis, "Ajeet Kumar" is string value stored in Redis.

Hashes

Hash is a collection of key-value pairs. In Redis, hashes are maps between string fields and string values. So, they are used to represent objects.

Example

Let's store a user's object which contains basic information of a user.
HMSET user:1 username ajeet password javatpoint alexa 2000  
OK  
HGETALL  user:1  
"username"  
"ajeet"  
"password"  
"javatpoint"  
"alexa"  
"2000"  

Here, HMSET and HGETALL are the command for Redis, while user:1 is the key.

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

Lists

Redis Lists are defined as a lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.

Example
lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lpush javatpoint java  
(integer) 1  
lrange javatpoint 0 10  
"cassandra"  
"mongodb"  
"sql"  
"java"  

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

Sets

Sets are an unordered collection of strings in Redis database. In Redis, you can add, remove, and test for the existence of members in O(1) time complexity.

Example
sadd tutoriallist redis  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist sql  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 1  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 0  
redis 127.0.0.1:6379> sadd tutoriallist postgresql  
(integer) 0  
redis 127.0.0.1:6379> smembers tutoriallist  
1) "redis"  
2) "postgresql"  
3) "sql"  

In the above example, you can see that postgresql is added thrice but due to unique property of the set it is added only once.

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

Sorted Sets

Redis Sorted Sets are similar to Redis Sets. They are also a set of non-repeating collections of Strings. But every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the scores may be repeated.

Example
redis 127.0.0.1:6379> zadd tutoriallist 0 redis  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 sql  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 1  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 0  
redis 127.0.0.1:6379> zadd tutoriallist 0 postgresql  
(integer) 0  
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 10  
1) "postgresql"  
2) "redis"  
3) "sql"