Open In App

Complete Guide on Redis Data Types with Commands and Storage

Last Updated : 27 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Redis-Data-Types

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?

  • Redis uses a key-value store mechanism where a unique key is associated with a string value.
  • Keys are stored as binary data, and the associated string values can contain text or binary data.

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:

  • HSET– This command sets the key-value pair in Hash.
  • HGET – This command returns the value of a specified key.
  • HMGET – This command returns all the key and values present in the Hash table.
  • HINCRBY – This command will increase the value of a specified key.

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?

  • Hashes are implemented as key-value stores within Redis.
  • A hash can be thought of as a map or dictionary, where a single key maps to multiple field-value pairs.
  • Hashes are suitable for storing structured data.

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:

  • LPUSH – This command adds a new element to the head of the list.
  • RPUSH – This command adds a new element to the tail of the list.
  • LPOP – This command removes and return an element from the head of the list.
  • RPOP – This command removes and return an element from the tail of the list.
  • LLEN – This command returns the length of the list.
  • LMOVE – This command automatically moves elements from one list to other list.
  • LTRIM – This command will reduces a list to the specified range of elements.

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?

  • Lists in Redis are implemented as a doubly-linked list of values.
  • Each list has a unique key, and elements are stored in the order they were added.
  • Lists are commonly used for implementing queues, message brokers, and more.

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

  • SADD: This command is used to add a new element to the set.
  • SREM: This command will remove the specified element from set.
  • SISMEMBER: This command will tests a string for test membership.
  • SINTER: this command will return the intersection of lists.
  • SCARD: This command will return the size of the set.
  • SMEMBERS : This command will return all the members of the set value stored at key.

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?

  • Sets are implemented as an unordered collection of unique elements.
  • Redis uses an optimized data structure to ensure that elements in a set are unique.
  • Set operations like union, intersection, and difference are efficient.

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

  • ZADD: This command will add an element to the ordered- set, if the element is already present then the key value is updated.
  • ZRANGE : This command will returns elements of a sorted set within a given range.
  • ZRANK : This command will returns the Rank of a specified element in the sorted (Ascending order) list
  • ZREVRANK: This command will returns the Rank of a specified element in the sorted (Descending order) list

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?

  • Sorted sets combine the features of sets and ordered lists.
  • Each element in a sorted set is associated with a score, and elements are stored in ascending order of their scores.
  • Sorted sets are useful for ranking and leaderboard applications.

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

  • XADD – This command will add an element into the Stream.
  • XREAD – This command will read one or more entries.
  • XRANGE – This command returns entries within a range.
  • XLEN – This command will return the length of the Stream.

How Redis Stores Streams Data Types?

  • Streams are implemented as an append-only log.
  • Each stream has a unique key and consists of a sequence of entries with unique IDs.
  • Streams are used for event-driven and log-based data storage and processing.

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:

  • PFADD: This command will add an item to the HyperLogLog.
  • PFADD: This command will count the number of elements in the set.
  • PFMERGE: This command will merge two or more HyperLogs into one.

How Redis Stores HyperLogLog Data Types?

  • HyperLogLogs use probabilistic data structures to estimate the cardinality (number of unique elements) of a set.
  • They use a minimal amount of memory to provide approximate results.

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:

  • SETBIT: This command is used to set the bit to 0 From 1, and 1 from 0.
  • GETBIT: This command is used to returns the value of a bit at a given offset.
  • BITOP: This command is used to perform bitwise operations against one or more strings.

How Redis Stores Bitmaps Data Types?

  • Bitmaps are implemented as an array of bits.
  • Redis provides bit-level operations to set, clear, and manipulate individual bits in a bitmap.
  • Bitmaps are used for various applications, including analytics and tracking user behavior.

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:

  • BITFIELD : This command will atomically sets, increments and reads one or more values.
  • BITFIELD_RO: This command is a read-only variant of BITFIELD.

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:

  • GEOADD– This command adds a location to a given geospatial command.
  • GEOSEARCH- This command will returns the location of a given point.

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.

  • Sorted Sets (Zsets): You can use a Redis Sorted Set to store time-series data where the score represents the timestamp, and the member represents the data point.
  • Streams: Redis Streams are another option for managing time-series data. Streams are more suitable for real-time event streaming and logging use cases.

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads