Definition of Redis Data Types
Redis data types is one of the most famous NoSQL databases, which finds its application in caching, storing, and managing user sessions, ad servicing, and recommendations. Redis is an open-source database server that stores data as key-value pairs in RAM memory. The keys manage, delete, and perform various operations on the values paired with them. The value can be any one of the data types supported by Redis. Some of the data types are strings, lists, hashes, sets, sorted sets, hyperloglogs, and bitmaps. Some of the commands are GET, LPUSH, HSET, and SISMEMBER.
Key Highlights
- Redis is an open-source database server that stores data in RAM as key-value pairs, which makes Redis remarkably fast.
- Data types pair the keys with the values so you can perform various operations on them.
- Some common Redis data types are strings, sets, sorted sets, hashes, lists, hyperloglogs, and bitmaps.
- Each data type has its own command set for managing the access pattern regularly, providing transaction support, and performing a large number of operations in a constant time.
Before moving further, please install Redis in your terminal for better understanding and practicing purposes. Here is the link for installing Redis. Please make use of it. redis/.
Redis Data Types
Data types and commands that are supported by Redis are given below.
a) Strings
- Strings are a widely used Redis data type. It stores data in a sequence of bytes and is binary-safe.
- Each string can store data up to 512 MB.
- The data can be text, integers, floats, videos, images, and audio files.
Commands
String data type allows 3 types of commands.
- SET command: It helps to create a new key-value pair in the database.
- GET command: It helps to fetch the value of the specified key.
- DEL command: It deletes the specified key and its value.
Example:
- The command SET stores the value “project” in a key eduCBA.
- The GET command retrieves and displays the value “project”.
- The DEL command deletes the value in the key eduCBA and returns the number of values deleted.
b) Lists
- Lists are strings sorted in an ordered sequence.
- Lists allow simultaneous insertion of values both in the head and tail ends of the lists at a constant time.
- The addition of values at a constant time is very helpful for faster writing operations.
Commands
- LPUSH: It helps to push the value to the left end of the list.
- RPUSH: It helps to push the value to the right end of the list.
- LRANGE: It provides a range of items in the lists.
- LPOP/RPOP: It helps to display and remove the value from both the right and left ends.
- LINDEX: It helps to extract value from the specified position within the lists.
Example:
- The addition of values to the list using LPUSH and RPUSH displays the number of values in the lists.
- LRANGE provides the values in lists between 0 and 7.
c) Hashes
- A Redis hash stores a large number of unordered objects or keys in minimal space.
- It maps the unordered keys to the string values.
- The value should always be a Redis string.
- Other complex data structures are not allowed in Hash.
- The Redis hash can store more than 4 billion key-value pairs.
Commands
- HSET: It assigns a value to a key in the hash.
- HGET: It fetches values associated with a key within the hash.
- HGETALL: It displays the whole content of the hash.
- HDEL: It deletes a key and its associated value from the hash.
Example:
- The HSET command checks for the existence of the value.
- It provides the output of the number of values entered in the hash.
- Similarly, when the HDEL command is executed, it deletes the value and displays the number of values in the hash.
- The HGETALL command displays all values in the hash key.
d) Sets
- Sets are unordered collections of string values, and hence they prohibit duplication of data.
- Since they are in an unordered sequence, you cannot add values to the right and left sides of the sets.
- The values of the sets are very unique and non-repeatable.
Commands
- SADD: It helps to insert a value into a set.
- SISMEMBER: It helps to find the existence of value in a set.
- SMEMBERS: It helps to fetch and display all values from a set.
- SREM: It deletes the specified value from the set.
- SRANDMEMBER: It helps to fetch a random set value from a set.
Example:
- The SADD command adds the value “redis” to the set and returns the integer (1) since the number of values added is 1.
- The SMEMBERS display all values in the set.
e) Sorted Sets
- Sorted sets, otherwise known as Zests, are similar to the mix of Redis sets and Redis hash.
- The key-value pairs in the sorted sets are made of distinctive string keys known as members and the value is known as scores.
- The scores are nothing but a floating point value that sorts the elements or keys in the desired order.
- If two scores are added to the same value, the last added score will exist in the sorted set by replacing the previously added one.
Command
- ZADD: It helps to insert a key-value pair with a score into the sorted set.
- ZRANGE: It displays values in the sorted order from low to high, based on the range of scores. The actual score values are displayed by using the WITHSCORES option.
- ZREVRANGE: It displays the values of a sorted set arranged from high to low based on the range of scores.
- ZRANGEBYSCORE: It displays values from the sorted set based on the predefined range of scores.
- ZREM: It helps to delete values from a sorted set.
Example:
- The command ZADD adds the value “redis” to the key along with the score 1.
- The ZRANGE along with the WITHSCORES command displays the values and their scores sorted in ascending order.
f) HyperLogLogs
- Hyperloglogs (HLLs) are probabilistic data structures.
- It gives the count of the distinctive values in a collection.
- Generally, the counting process requires memory equal to the number of values, whereas HLLs use fixed memory and reduce memory consumption.
- The maximum memory size of HLLs is 12KB.
- The Redis server stores the HLLs as Redis strings.
- HLLs provide an approximate count of the values.
Commands
- PFADD: It helps to insert one or several values into Hyperloglogs.
- PFCOUNT: It helps to display the count of unique values from Hyperloglogs.
- PFMERGE: It combines two different HyperLogLogs into a single Hyperloglog.
Example:
- The PFADD command adds the value “redis” to the key eduCBA and displays the count as 1.
- The PFMERGE combines the two values of the keys eduCBA and tutorial.
g) Bitmaps
- Redis allows bit-level operations on strings known as bitmaps.
- Bitwise operations are executed using commands in two ways: operation on single-bit and operations on groups of bits.
- It saves more memory space when storing information.
Commands
- SETBIT: It establishes the bits based on a 0 or 1 value.
- GETBIT: It helps to fetch the bit value specified by a key.
- BITOP: It helps to perform bitwise operations between strings.
- BITPOS: It obtains the first bit with a specified value of 1 or 0 in a string.
- BITCOUNT: It counts the number of bits set to 1 in a string.
Example:
- The SETBIT considers the first argument as a bit number and the second argument as the value of the bit.
- The GETBIT displays the value of bit 11.
Benefits of Redis Data Types and Commands
- Since Redis stores data in RAM, it provides a super-fast website browsing experience and session storage.
- Easy setup and faster access are the greatest benefits of Redis.
- Redis has a unique hashing mechanism called Redis Hashing.
- It exhibits a data replication process, hence any change in the master node will affect the slave node, and the data will be updated automatically.
- It has a mass insertion mechanism that helps in loading millions of pieces of data into the cache in a short period.
Conclusion
Redis is extremely fast and is used in various applications. Each Redis data type finds its own applications to perform various tasks. Some applications are as follows: traffic balancing applications, social media posts, user data, ordered leader boards of player scores in an online competition, and unique user interactions or queries.
FAQs
Q1. What are the data types supported in the Redis database?
Answer:
The Redis database commonly supports seven types of data, such as strings, sets, sorted sets, hashes, lists, hyperloglogs, and bitmaps.
Q2. Why is Redis super-fast?
Answer:
- Redis is a type of NoSQL database server that stores data on disk, it stores data in RAM, which is much faster than on disk.
- It supports well-organized lower-level data structures.
- Redis also supports input/output multiplexing and a single-threaded execution loop for efficient execution.
Q3. What is the maximum number of keys that can be handled by Redis in a single instance?
Answer:
In a single instance, Redis can handle up to 2^32 keys irrespective of data types. It was proved that per instance, Redis can handle up to 250 million keys.
Recommended Articles
This article explains everything about Redis Data Types and Commands. To know more about related topics, visit the following links: