DEV Community

Cover image for Redis Demystified: A Simple Introduction for System Design 🧩
Priya
Priya

Posted on

Redis Demystified: A Simple Introduction for System Design 🧩

You’ve probably encountered the word "Redis" many times while learning about system design. This tutorial will give you a better understanding (I hope) of what Redis is all about from a beginner's perspective, and in the future, we'll dive deeper into real-world examples of how to implement Redis in your applications.

What is Redis?

Redis Image

Redis is a single-threaded, in-memory data structure server or data store. It’s incredibly fast compared to traditional databases because it operates in memory (RAM) instead of disk storage.

So many terms? No worries! Let’s break it down and understand each one:


Single Threaded

Single thread

Redis processes all requests on a single thread. While this might seem like a bottleneck, it’s actually a well-thought-out design choice that ensures simplicity and efficiency. Redis achieves this efficiency using a mechanism called I/O Multiplexing.

Understanding I/O Multiplexing with an Analogy

Imagine a waiter in a busy restaurant where customers are lining up to place their orders. If the waiter attends to one customer at a time—waiting for them to browse the menu and decide what to order—other customers would have to wait longer.

Now, let’s make an improvement: The waiter places menus at each table so customers can decide on their orders in advance. Instead of waiting for each customer to be ready, the waiter immediately takes orders from those who are prepared, significantly reducing waiting time.

This is similar to how I/O Multiplexing works in Redis.

  • The Redis server listens to all incoming requests.
  • Some clients may be idle (e.g., not sending commands).
  • Redis processes the requests that are ready to be handled, ensuring smooth and fast operations.

By doing this, Redis optimizes performance and keeps things simple without needing multiple threads or complex locking mechanisms.


In-Memory

Im-memory db image

Redis is an in-memory data store, meaning it keeps all its data in RAM for fast access. This in-memory design allows Redis to be incredibly fast for read and write operations, as accessing data from RAM is much quicker than from disk storage.

However, because RAM is volatile, meaning the data is lost if the server crashes, Redis provides mechanisms to ensure that data persists even in case of unexpected failures. Redis periodically saves its data to disk using two mechanisms:

  1. Snapshotting: Redis saves the dataset to disk at specified intervals, creating a point-in-time snapshot of the data. By default, Redis writes this snapshot to disk:

    • Every 900 seconds (15 minutes) if at least one key has changed.
    • Every 300 seconds (5 minutes) if at least 100 keys have changed. (This data was gathered from sources like Google, but it's always good to double-check against Redis documentation for accuracy.)
  2. Append-Only File (AOF): Redis can also log every write operation to an append-only file, allowing the data to be reconstructed in case of a crash.

These settings are configurable, meaning you can adjust how often Redis saves data to disk based on your application’s needs.


Data structure

Data structures image

We often learn more about Data Structures and Algorithms (DSA) than other topics in computer science, but why focus on Data Structures here? Redis allows you to store data as key-value pairs, where the value can be a variety of data structures, from simple strings and integers to more complex ones like Lists, Sets, Sorted Sets, and even Hashes.

This flexibility in data structures is one of the reasons Redis is so powerful. It allows you to efficiently handle and manipulate different types of data based on your needs.


Commands to Start Off With

  • SET: Set a value for a key.
SET mykey "Hello, World!"
Enter fullscreen mode Exit fullscreen mode
  • GET: Retrieve the value of a key.
GET mykey
Enter fullscreen mode Exit fullscreen mode
  • DEL: Delete a key.
DEL mykey
Enter fullscreen mode Exit fullscreen mode
  • EXPIRE: Set an expiration time on a key.
EXPIRE mykey 60  # Key expires in 60 seconds
Enter fullscreen mode Exit fullscreen mode
  • HSET: Set a field in a hash (similar to object).
HSET user:1000 name "Priya"
HSET user:1000 email "priya@example.com"
Enter fullscreen mode Exit fullscreen mode

And much more!

For a quick reference on Redis commands, check out the official Redis Quick Start Cheat Sheet.


Practical Example

Imagine you have a web application where users frequently query a product database. Repeated queries for the same product can be slow and waste resources. Here’s how Redis can help:

  • Set Product Data in Redis:
SET product:1234 "{ 'name': 'Laptop', 'price': 1000 }"
Enter fullscreen mode Exit fullscreen mode
  • Check Redis Before Querying Database: Before hitting the database for a product, check Redis to see if the data exists.
GET product:1234
Enter fullscreen mode Exit fullscreen mode
  • Use Expiration: To avoid outdated data, set an expiration time on the cached data.
EXPIRE product:1234 600
Enter fullscreen mode Exit fullscreen mode

Wrap-Up

Redis is a powerful tool that enables fast, flexible data management using in-memory storage. By understanding its core concepts, you can use Redis to improve your application’s performance and scalability.

Feel free to share your experiences with Redis or any tips you have!

Happy Designing! 😊💻

Top comments (0)