Redis Data Type
There are five types of data types supported by Redis database.
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.
Let's store a string name "Ajeet Kumar" in the key by using SET command and then retrieve the same by using GET command.
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.
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.
Let's store a user's object which contains basic information of a user.
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).
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.
The max length of a list is 232 - 1 elements (more than 4 billion of elements per list).
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.
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).
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.