Open In App

Complete Guide on Redis Data Types with Commands and Storage

Redis is an open-source, in-memory data store that supports various data types: such as String, Sets, Lists, Hashes, etc. to store data according to the needs of the user. These diverse data types make Redis versatile for caching, messaging, real-time analytics, and more.



Different Data Types in Redis

Let us now discuss about the various data types in Redis in detail one by one:



1. String Data Type in Redis

Redis String is a sequence of bytes which can store sequence of bytes, including text, Object, binary arrays. which can store a maximum of 512 mega bytes in one string. Redis String can also be used like a Redis Key for mapping a string to another string. String Data Types are useful in different types of use cases like: caching HTML fragements or different pages.

Example:

SET name = “GeeksForGeeks” 
GET name

Output: “GeeksForGeeks”

Note: In the above code SET command is used to set the string with key = name and value = “GeeksForGeeks”. And The GET command will print the value associated with the key name, which is GeeksForGeeks.

How Redis Stores String Data Types?

2. Hash Data type in Redis

Redis Hashes are used to store a list of multiple key- value pairs. it is used to map between a string to another string. It can also used to represent as objects.

Basic Command Used in Hash Data Structure in Redis:

Example:

HMSET user :1 name GFG city Noida State UP Cost 200
(Command 1)
HGET user : 1 city
Output 
: Noida
(Command 2)
HGET user:1 Cost
Output:
200

In the above code HMSET command is used to create Hash data structure in Redis. and HGET command is used to get the data of a particular key.

Note: Every Redis Hash can store up to 2 ^32 -1 key-value pairs. 

How Redis Stores Hash Data Types?

3. List Data Type in Redis

Redis Lists are similar to linked list of String values. Redis Lists can be used to implement stack and queue data structure.

Basic Command Used in List Data Structure in Redis:

Example:

LPUSH name: GFG city: Noida
LPUSH name: GFG articles : 500
RPOP name: GFG 
(// command 1)
Output: 
city: Noida
// command 2
RPOP name: GFG
Output: 
articles : 500

Note: The max length of a Redis list is 2^32 – 1 (4,294,967,295) elements.

How Redis Stores List Data Types?

4. Set Data type in Redis:

Set Data Types in Redis is an un-ordered collection of unique strings. it can be used to detect unique elements in a list, also for intersections, unions, and differences.

Basic Commands used in Set Data types in Redis

Example:

SADD user: GFG name: Ankit
SADD user: GFG name: Aditya
SADD user: GFG name: Aarohi
SMEMBERS user: GFG

Output: 
1) Ankit 
2) Aditya
3) Aarohi

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

How Redis Stores Set Data Types?

5. Sorted Set in Redis:

Sorted Set Data Types in Redis is an ordered-collection of unique strings stored in a sorted form ordered by associated key. when two or more key having same value then, the strings are ordered in lexicographical order. It is similar to Redis Set , the only difference is that Sorted set represents the data in a sorted or ordered way.

Basic Commands used in Set Data types in Redis

Example:

ZADD user: GFG 0 DSA
ZADD user : GFG 1 WEB_TECH
ZADD user: GFG 2 SYSTEM_DESIGN
ZADD user: GFG 2 PHYSICS
//command 1
ZRANGEWITHSCORE user: GFG 0 100

Output:
1. DSA
2. WEB_TECH
3. SYSTEM_DESIGN

How Redis Stores Sorted Set Data Types?

6. Streams in Redis:

A Redis stream is a data structure that acts like an append-only log but also implements several operations to overcome some of the limits of a typical append-only log. These include random access in O(1) time and complex consumption strategies, such as consumer groups.

Basic Commands used in Streams in Redis

How Redis Stores Streams Data Types?

7. HyperLogLog in Redis:

HyperLogLog is a probabilistic data structure that estimates the cardinality of a set. As a probabilistic data structure, HyperLogLog trades perfect accuracy for efficient space utilization.

Basic Commands used in HyperLogLog in Redis:

How Redis Stores HyperLogLog Data Types?

8. Bitmaps in Redis:

Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type which is treated like a bit vector. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 2^32 different bits.

Basic Commands used in Bitmaps in Redis:

How Redis Stores Bitmaps Data Types?

9. Bitfields in Redis

Redis bitfields allow you to set, increase, and get integer values with any number of bits. You can, for instance, work with everything from signed 63-bit numbers to unsigned 1-bit integers.

Basic Commands used in Bitfields in Redis:

10. Probabilistic in Redis:

Probabilistic data structures are used to provide approximate answers or estimations for certain operations with reduced memory usage compared to exact data structures. One of the most commonly used probabilistic data structures in Redis is HyperLogLog (HLL). It is used for estimating the cardinality (the number of unique elements) of a set. Redis also offers another probabilistic data structure called Bloom Filters. It checks for presence of an element in a set. Bloom Filters are useful for quickly checking membership in a large dataset without the need for storing the entire dataset in memory. Cuckoo filters are a probabilistic data structure that checks for presence of an element in a set.

11. Geospatial Indexes in Redis:

Redis Geospatial Indexes helps us to store the coordinates and search for them. this data structure is useful for finding nearby points.

Basic Commands used in Geospatial Indexed in Redis:

12. Time-Series in Redis

To create a time-series data structure in Redis, you can use Sorted Sets (Zsets) or Streams, depending on your specific requirements.

Redis efficiently manages memory for these data types and offers various commands for performing operations on them. Additionally, Redis allows data persistence through mechanisms like snapshots and write-ahead logs, ensuring data durability.


Article Tags :