Master Redis: Learn Commands, Discover Use Cases

What is Redis?

Quick Summary: This blog focuses on Redis covering its commands and highlighting its versatile uses such as caching, real-time analytics, session management, and messaging.

Introduction

Redis stands for Remote Directory Serve. It is an in-memory data structure store. It stores its data in memory, not on a hard drive. Also, it supports multiple data structures or types as a key-value database. Therefore, it helps map key-value-based strings for storing and retrieving data and lists, sets, etc.

Redis is open-source software released under a BSD 3-clause license, a specific type of permissive free software license. Furthermore, it began when Salvatore Sanfilippo needed to improve the scalability of his website. He soon open-sourced the platform. Nowadays, the core team develops and maintains Redis command, which has been sponsored by Redis Labs since 2015.

Many Software Development Services utilize Redis for fast caching, real-time analytics, session management, and messaging due to its in-memory data storage and high-performance capabilities.

Redis Use Cases

Redis data resides in memory, as opposed to traditional forms of databases that persist to disk. It gives Redis an edge over other types of storage systems and makes it much faster with high output and low latency. Therefore it is used in real-time programs and queue messaging systems. Other use cases include:

  • Queues
  • Publish/subscribe (pub/sub)
  • Real-time analytics
  • Machine learning
  • Geospatial processing
  • Leaderboards/Counting
  • Session cache
  • Full-page cache

Who uses Redis?

Many companies have adopted Redis, which includes these major international organizations. This list gives an overview of the many additional companies that use Redis.

Redis commands

Redis Commands

Redis commands help developers perform operations on Redis. Furthermore, you need a Redis client to execute commands on the server. Moreover, it is available in the package also. Type the Redis-client command into the terminal on your machine to launch the Redis client. Now, you can execute the command on the local server.

Syntax

  • Following is the syntax of the Redis client.
    • $redis-cli

Example

$redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG

In the above illustration, we execute a command PING, that checks whether the server is running or not.

Redis Keys Commands

Redis keys commands are used to manage keys in Redis. Following is the syntax for using key commands.

Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME

Example
redis 127.0.0.1:6379> SET myName Abhishek
OK
redis 127.0.0.1:6379> DEL myName
(integer) 1

In the above example, DEL is the command, while myName is the key. If the key is deleted, then the output will be 1, otherwise, it will be 0.

The following table lists some commands to manage keys in Redis.

Sr.NoCommandDescription
1DEL keyThis command deletes the key if it exists
2DUMP keyThis command returns a serialized version of the value stored at the specified key.
3EXISTS keyIt’ll check whether the key exists or not.
4EXPIRE key secondsSets the expiry time of the key.
5PEXPIRE key millisecondsSet the expiry of a key in milliseconds.
6KEYS patternFinds all keys matching the specified pattern.
7MOVE key dbMoves a key to another database.
8PERSIST keyRemoves the expiration from the key
9TTL keyGets the remaining time in keys expiry.
10RANDOM KEYReturns a random key from Redis.
11RENAME key newkeyChange the key name.
12TYPE keyReturns the data type of the value which is stored in the key.

Strings Commands

Redis strings commands are applied to handle string values in Redis. Following is the syntax for using string commands.

Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME

Example
redis 127.0.0.1:6379> SET SET myName Abhishek
OK
redis 127.0.0.1:6379> GET myName
“Abhishek”

In the above example, SET and GET are the commands, while myName is the key.

The following table lists some commands to manage strings in Redis.

Sr.NoCommandDescription
1SET key valueSet the value for the specified key.
2GET keyGets the value of a given key.
3GETRANGE key start endGets a substring of the string.
4GETSET key valueSet the value of a key and return its old value.
5MGET key1 [key2..]Gets the values of all the listed keys
6SETBIT key offset valueSet or clear the bit at the offset in the string value stored at the key.
7SETEX key seconds valueSets the value with the expiry time of a key
8SETNX key valueSets the value of a key, if the key doesn’t exist
9SETRANGE key offset valueOverwrites the part of a string at the key starting at the specified offset
10STRLEN keyGet the length of the value.
11MSET key value [key value …]Sets multiple keys-values
12PSETEX key milliseconds valueSets the value and expiry time in milliseconds.
13INCR keyIncrements the integer value by one
14INCRBY key incrementIncrements the integer value by the given amount
15INCRBYFLOAT key incrementIncrements the float value by the given amount
16DECR keyDecrements the integer value by one
17DECRBY key decrementDecrements the integer value by the given number
18APPEND key valueAppends a value

Lists Commands

Redis Lists are lists of strings, sorted by insertion order. You can add the new element in Redis lists in the head or the tail of the list.

Sr.NoCommandDescription
1RPUSH key value1 [value2]Appends one or multiple values
2LPUSH key value1 [value2]Prepends one or multiple values
3LINDEX key indexGet an element from a list by its index value
4LPOP keyRemoves and gets the first element in a list
5LLEN keyGets the length of a list

Conclusion

Redis commands is an in-memory data store, discussed extensively in the blog. It covers various Redis commands and their uses. Read this blog to get a comprehensive understanding of Redis usage and capabilities.

FAQ

Redis is an in-memory data store that serves as a high-performance cache and database. Furthermore, it stores data in key-value pairs, offering fast access and versatile data manipulation.

Redis cache is a caching mechanism that stores current accessed information in memory. Furthermore, it accelerates data retrieval, enhancing application performance by reducing the need to fetch information from slower data resources.

Redis commands are helpful for various purposes, including caching, real-time analytics, session management, message queuing, and more. It excels in scenarios that demand fast data retrieval and manipulation, making it suitable for high-performance applications and systems.

In Redis commands, information is temporarily stored in key-value data structures because it is fast and can store data in memory, often used as a cache.

An SQL database employs a structured data model, which organizes data into tables with a fixed schema. On the flip side, a flexible data model is used by Redis, which supports strings, hashes, lists, sets, and sorted sets.