Redis Fundamentals

Introduction

Redis is the world's fastest in-memory database. It offers cloud and on-premises solutions for caching, vector search, and NoSQL databases.

In-memory is a concept related to how data is stored and accessed in a computer. When we talk about in-memory, we refer to storing data directly in the computer's RAM (Random Access Memory) rather than on a hard drive.

Here are some key points about in-memory storage:

Storing data in RAM:  

  • When data is stored in-memory, it is kept in the computer's RAM. RAM has faster access speeds compared to hard drives, allowing for quicker data querying and processing.  
    • The read and write speed of RAM is very fast, typically reaching several GB/s.
    • The read and write speed of SSDs (Solid State Drives) usually starts from 320 MB/s and can reach thousands of megabytes per second.
    • The read and write speed of HDDs (Hard Disk Drives) is lower, generally ranging from 80 MB/s to 160 MB/s.
  • In-memory data is often used for purposes such as caching, temporary storage, and direct data processing.

Advantages

  • High access speed: Data in-memory can be accessed almost instantly, improving application performance.
  • Reduced latency: There's no need to read data from a hard drive, which helps reduce latency in data queries.

Disadvantages

  • Risk of data loss: Since data is stored only in RAM, it can be lost if the computer is turned off or restarted.
  • Memory capacity limitations: RAM has limited capacity, so not all data can be stored in-memory.

In summary, in-memory is a method of storing data in RAM, which helps improve performance and reduce latency in data queries. It is commonly used for tasks that require high processing speed and short access times.


Characteristics of Redis

Advantages:

  • In-memory: Redis stores data directly in memory, allowing for faster queries compared to PostgreSQL, without the need to read from a disk.
  • Simplicity: Redis has a simple architecture, focused on data storage and querying.
  • Designed for in-memory tasks: Redis is well-suited for storing cache, sessions, and temporary data.

Disadvantages:

  • Not ACID-compliant: Redis does not adhere to the ACID (Atomicity, Consistency, Isolation, Durability) standard like PostgreSQL.
  • Not suitable for complex data: Redis is not ideal for applications requiring complex data integrity.


Install with Docker

To start using Redis, one simple way is to set it up through Docker.  

First, you need to install Docker, and then you just need to execute the following command:

docker run -dp 6379:6379 redis:alpine


To start working with Redis, execute the following commands:

Data Types

Redis supports various data types, allowing you to solve a wide range of problems. Here are some of the core data types in Redis:

STRING

String is the most basic data type in Redis. There are three primary commands associated with it:

  • GET: Retrieves the value of a key.
  • SET: Sets the value for a key.
  • DEL: Deletes a key and its corresponding value

GET {key} {value}
SET {key}
DEL {key}


In Redis, commands are case-insensitive, meaning you can use either `SET` or `set` interchangeably.

A string in Redis is not just a simple `char*`; it's a structured data type. This structure includes an element that stores the string's length, which allows retrieving the string length with a time complexity of O(1). Additionally, the two most basic operations for strings (and for Redis in general), `GET` and `SET`, both have a time complexity of O(1). This efficiency is due to Redis storing key-value data in a hash table, which offers a significant advantage over BTree in MySQL, where operations like `INSERT` and `SEARCH` have a time complexity of O(log(n)).


LIST

A List in Redis is a Double Linked List that stores an ordered collection of strings. This storage method allows for adding an element to the head or tail of the list in constant time, regardless of the list's size. However, this advantage comes with a trade-off: accessing an element by index in a linked list is much slower compared to an array

Another difference from traditional data structures is that a list in Redis has an additional attribute that stores its length, so using the `LLEN` command (which retrieves the length of the list) has a time complexity of O(1).

Basic Commands for Working with LIST:

  • RPUSH: Pushes a value onto the right side of the list.
  • LPUSH: Pushes a value onto the left side of the list.
  • RPOP: Removes and returns the value at the far right of the list.
  • LPOP: Removes and returns the value at the far left of the list.
  • LRANGE: Retrieves values within a specified index range.
  • LINDEX: Retrieves an element from the list based on its index.
  • LLEN Get length of list

RPUSH {list name} {value}
LPUSH {list name} {value}
RPOP {list name}
LPOP {list name}
LRANGE {list name} {start index} {end index}
LINDEX {list name} {index}
LLEN {list name}


SET

A SET in Redis is similar to a list, but with key differences: the elements in a SET are unique, meaning no duplicates are allowed, and the elements are not stored in any particular order.

To enhance the performance of SET operations, Redis uses a hash table to store the keys within a SET (when an element is added to the SET). This use of a hash table allows for much faster operations, as the time complexity for hash table operations is O(1).

Basic Commands for Working with SET:

  • SADD: Adds an element to the SET.
  • SMEMBERS: Retrieves all the elements of the SET.
  • SISMEMBER: Checks if a specific element exists in the SET.
  • SREM: Removes an element from the SET (if it exists).
SADD {set name} {value}
SMEMBERS {set name}
SISMEMBER {set name} {value}
SREM {set name} {value}


HASH

While LIST and SET store collections of string values, a HASH in Redis is more like a miniature version of Redis itself, where data is organized in a key-value format. The key is still a string, but the value can be either a string or a number. If the value is a number, you can easily perform increment or decrement operations on it.

Basic Commands for Working with HASH:

  • HSET: Adds a key-value pair to the hash or updates the value of an existing key.
  • HGET: Retrieves the value associated with a specific key.
  • HGETALL: Retrieves all key-value pairs within the hash.
  • HDEL: Deletes a key-value pair from the hash.

HSET {hash name} {key} {value}
HGET {hash name} {key}
HGETALL {hash name}
HDEL {hash name} {key}

SORTED SET (ZSET)

A SORTED SET (ZSET) in Redis is a special version of a SET where each element is added as a key-value pair. However, the value field must be a float number, referred to as the score. These scores are automatically sorted in ascending order.

ZSET does not allow duplicate keys, but it does allow duplicate values (scores). If multiple elements have the same score, they are sorted by their keys.

ZSET uses a specialized and relatively complex data structure unique to Redis, which is optimized for solving problems related to finding top elements in a list, such as the top students with the highest scores or the top web pages with the most views.

Basic Commands for Working with ZSET:
  • ZADD: Adds one or more elements to the ZSET.
  • ZRANGE: Retrieves elements based on an index range.
  • ZRANGEBYSCORE: Retrieves elements based on a score range.
  • ZSCORE: Retrieves the score of a ZSET element based on its key.
  • ZREM: Deletes an element by its key.
ZADD {set name} [{score} {key}]...
ZRANGE {set name} {start index} {end index} [WITHSCORES]
ZRANGEBYSCORE {set name} {start score} {end score} [WITHSCORES]
ZSCORE {set name} {key}
ZREM {set name} {key}

  • The ZADD command allows you to add one or multiple elements to a ZSET, with each element being separated by a space.
  • The WITHSCORES option is optional and is used to display the score of each element when retrieving data (by default, only the keys are shown).

Conclusion

From this overview, there are two main characteristics of Redis you should keep in mind:

  • In-memory storage: Redis stores data in RAM, which allows for faster read/write speeds compared to HDDs/SSDs. While Redis does have mechanisms to synchronize data from RAM to disk, its memory-based storage limits its capacity (not suitable for storing hundreds of GBs/TBs). Therefore, Redis is ideal for smaller datasets that don't need to be stored for extended periods, making it perfect for caching data.
  • Simple design: Redis is designed with simplicity in mind, resulting in low query complexity and a focus on achieving high performance. However, this simplicity means that Redis is best suited for storing simple data. For scenarios requiring complex relationships between data, a specialized database like MySQL or PostgreSQL is more appropriate.

See you again in the next articles!

Comments

Popular posts from this blog

Kubernetes Practice Series

NodeJS Practice Series

Docker Practice Series

React Practice Series

Sitemap

Setting up Kubernetes Dashboard with Kind

Deploying a NodeJS Server on Google Kubernetes Engine

DevOps Practice Series

A Handy Guide to Using Dynamic Import in JavaScript

Using Kafka with Docker and NodeJS