Introduction:

Hi team , we have seen during previous posts, how is working Redis , how to browse into data using Redis Insight and how to backup your database.

This time , in this first part ,we will go deeper by checking the different key type ,how to use it and their limitation.

So let’s go!

About Redis

As a reminder,let’s take a look again on the major advantages of using a Redis database:

Keys

  • Definition

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like “foo” to the content of a JPEG file. The empty string is also a valid key.

Best practice about keys:

  • Very long keys are not a good idea. For instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, hashing it (for example with SHA1) is a better idea, especially from the perspective of memory and bandwidth.
  • Very short keys are often not a good idea. There is little point in writing “u1000flw” as a key if you can instead write “user:1000:followers“. The latter is more readable and the added space is minor compared to the space used by the key object itself and the value object. While short keys will obviously consume a bit less memory, your job is to find the right balance.
  • Try to stick with a schema. For instance “object-type:id” is a good idea, as in “user:1000”. Dots or dashes are often used for multi-word fields, as in “comment:4321:reply.to” or “comment:4321:reply-to”.
  • The maximum allowed key size is 512 MB.

Of course I invite you to visit Redis site which is really complete and useful ( also a lot of Redis item definition you will find here are extracted from the Redis official site itself.)

The different Key type

Below you will see all Key Type you can find on Redis ( I used RedisInsight to show you that )

Note that the commands are not case sensitive , in other words a HSET and a hset or HSeT will give the same result 🙂

We will list them and give:

  • Their definition
  • The basic commands
  • A quick example
  • The limitation of each command.

Hash Type

Hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.

Basic commands

  • HSET sets the value of one or more fields on a hash.
  • HGET returns the value at a given field.
  • HMGET returns the values at one or more given fields.
  • HINCRBY increments the value at a given field by the integer provide
  • HGETALL – Displays the entire hash content.
  • HDEL – Removes an existing key-value pair from a hash.

Example

Limits

Every hash can store up to 4,294,967,295 (2^32 – 1) field-value pairs. In practice, your hashes are limited only by the overall memory on the VMs hosting your Redis deployment.

Lists

Redis allows you to associate an ordered sequence of strings to a key. This linked list of strings lets you perform a set of operations such as:

Basic commands

  • LPUSH adds a new element to the head of a list; RPUSH adds to the tail.
  • LPOP removes and returns an element from the head of a list; RPOP does the same but from the tails of a list.
  • LLEN returns the length of a list.
  • LMOVE atomically moves elements from one list to another.
  • LTRIM reduces a list to the specified range of elements.
  • RPUSH – Pushes the value to the tail end of the list.
  • LRANGE – Retrieves a range of items.
  • LPOP/RPOP – Used to display and remove items from both ends.
  • LINDEX – Obtain a value from a specific position within the list.

Sets

A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:

  • Track unique items (e.g., track all unique IP addresses accessing a given blog post).
  • Represent relations (e.g., the set of all users with a given role).
  • Perform common set operations such as intersection, unions, and differences.

Basic commands

  • SADD adds a new member to a set.
  • SREM removes the specified member from the set.
  • SISMEMBER tests a string for set membership.
  • SINTER returns the set of members that two or more sets have in common (i.e., the intersection).
  • SCARD returns the size (a.k.a. cardinality) of a set.
  • SMEMBERS – Retrieves all items from a set.

Example:

Limits

The max size of a Redis set is 2^32 – 1 (4,294,967,295) members.

Sorted sets

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include:

  • Leaderboards. For example, you can use sorted sets to easily maintain ordered lists of the highest scores in a massive online game.
  • Rate limiters. In particular, you can use a sorted set to build a sliding-window rate limiter to prevent excessive API requests.

Basic commands

  • ZADD adds a new member and associated score to a sorted set. If the member already exists, the score is updated.
  • ZRANGE returns members of a sorted set, sorted within a given range.
  • ZRANK returns the rank of the provided member, assuming the sorted is in ascending order.
  • ZREVRANK returns the rank of the provided member, assuming the sorted set is in descending order.
  • ZRANGEBYSCORE – Fetches items from the sorted set based on the defined score range. The withscores option produces the actual score values.ZREM Removes items from a sorted set.

Example

  • Let’s use Redinsight to create the sorted set:
  • We will create a new user and a score:
  • List the score of the players:

Note:

Using ZREVRANK will display the rank of user from the highest score to the lowest , also first rank will start with 0

Strings

Redis strings store sequences of bytes, including text, serialized objects, and binary arrays. As such, strings are the most basic Redis data type. They’re often used for caching, but they support additional functionality that lets you implement counters and perform bitwise operations, too.

Basic Commands

  • SET stores a string value.
  • SETNX stores a string value only if the key doesn’t already exist. Useful for implementing locks.
  • GET retrieves a string value.
  • MGET retrieves multiple string values in a single operation

Limits

By default, a single Redis string can be a maximum of 512 MB.

Conclusion

We have seen some of useful Redis command to create keys and play with it , there is some other remaining but we will see that in another post.

For the moment I invite you to share with us , visit the dbi bloggers and also to check my other blogs for Control-M or Jenkins for example :).

Stay tuned for the part 2!