Redis Basics | Introduction

Ali Mohammad
7 min readApr 28, 2023

👨🏻‍💻 In this basic article, we will tackle the basic Redis commands and get more familiar with them!

So What is Redis?

Redis is an advanced key-value data store that uses NoSQL. Due to its robust data types, including Strings, Hashes, Lists, Sets, Sorted Sets, Bitmaps, and HyperLogLogs, it is also referred to as a “data structure server”.

Redis stores all of its data in memory by default, making read and write operations exceedingly quick. Data may also stay on the disk as a result of it. Redis allows for the creation of binary snapshots of the data that is being saved or readable files that contain the sequence of all commands that have been run over time. These are referred to as journaling and snapshotting, respectively.

Running the CLI!

redis-server is the actual Redis data store. It can be started in standalone mode or in cluster mode.

redis-cli is a command-line interface that can perform any Redis command (it is a Redis client).

By default, Redis binds to port 6379, runs in standalone mode, and can be started with this line)

Now, lets start our beautiful redis-cli and start learning!

The basics:

Let’s create a key first!

We use the SET command to create a key with a string value and then the GET command to read the key value

And we can get the key value by using:

Fabulous right!

Fabulous GIF
fabulous gif

You can use the HELP command to see what a command does.

The HELP command is useful for learning about command syntax. It displays the command parameters with a summary and examples.

The KEYS command is also useful, as it returns all stored keys that match a pattern (it is a glob-style pattern, like the Unix shell glob pattern)

Get the DataType of a Key

Delete a key

Does a key exists

Set Expiration in UNIX epoch

Expire a key in a number of milliseconds

Check the Time to Live for an expiring Key

Remove Expiration from a key

Name a key

Rename a key if the new one does not exist

Redis’s string data types are the most flexible since they may be used for a wide variety of tasks and commands. Depending on its value and the commands used, a String may act as an integer, float, text string, or bitmap. Any type of data can be stored by it, including binary data, floats, integers, and text (including XML, JSON, and raw text) (videos, images, or audio files).

A String value cannot exceed 512 MB of text or binary data. The following are some use cases for Strings:

  • Mechanisms for caching: Text or binary data, such as HTML pages, API answers, photos, and videos, can be cached in Redis. With the use of the commands SET, GET, MSET, and MGET, a basic cache system may be put into place.
  • Cache with automated key expiration: Using the commands SETEX, EXPIRE, and EXPIREAT, strings can be paired with automatic key expiration to create a reliable cache system. When database queries can be cached for a predetermined amount of time and take a long time to execute, this is highly helpful. As a result, this prevents those queries from being run too frequently and can improve the speed of applications.
  • Counting: The commands INCR and INCRBY and Strings make it simple to create a counter. Good examples of counters are page views, video views, and likes. Strings also provide other counting commands, such as DECR, DECRBY, and INCRFLOATBY.

Appending

Increment by 1

Increment by any

Decrement by 1

Get string length

Set multiple values

Set multiple values if key does not exists (nx)

Due to their ability to behave as a straightforward collection, stack, or queue, lists are a particularly flexible data type in Redis. Because Redis Lists’ operations guarantee that concurrent systems won’t overlap while removing items from a queue, many event systems employ Lists as their queue. Commands in a list are atomic. Redis’s Lists support blocking commands, thus if a client uses a blocking command in an empty List, Redis will wait for a new item to be added to the List.

Redis’s Lists are linked lists, hence adding to or removing from a List’s beginning or end takes O(1), constant time. A List’s elements can be accessed in O(N), linear time, but the first and last elements are always accessible in constant time.

If a list has fewer elements than the configuration for list-max-ziplist-entries and if each element is smaller than the configuration for list-max-ziplist-value, a list can be encoded and memory optimized (in bytes).

The maximum number of elements a List can hold is 232–1, which means there can be more than 4 billion elements per List. Some real-world use cases of Lists are as follows:

  • Many technologies, like Resque, Celery, and Logstash, utilize lists.
  • Storing most recent user posts.

Create a new list (Or add to one)

Push an item to a list only if the list exists

Set a List Item to a different value based on the Index

Inserts an item into a list based of a value

Get the List Length

Remove the First or last item

Get the Value of a List Index

Show all List Items

Hashes are a great data structure for storing objects because you can map fields to values. They are designed to search for data quickly and utilise memory effectively. Both the field name and the value in a hash are Strings. A Hash is hence a mapping from a String to a String.

Hashes have the benefit of being memory-optimized. The hash-max-ziplist-entries and hash-max-ziplist-value parameters serve as the foundation for the optimization. Internally, a hash can be either a hash table or a ziplist. A dual linked list called a ziplist is made to be memory-efficient. In a ziplist, real integers rather than a string of characters are used to store integers. A ziplist has memory optimizations, but lookups don’t happen instantly. A hash table, on the other hand, offers constant-time lookup but is not memory-efficient.

Set a Hash

Set Multiple Hashes

Get Hash Data

Get Single Hash Value Data

Get Multiple Hash Values

Delete a member from a hash

Check if a hash exists

Summary

In this article we’ve learned about Redis and it’s basic data types and commands.

In the next aritcle we will tackle sets, sorted sets and Hyperloglogs

H️ope you found this article interesting and fun ❤️!

--

--