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 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.No | Command | Description |
1 | DEL key | This command deletes the key if it exists |
2 | DUMP key | This command returns a serialized version of the value stored at the specified key. |
3 | EXISTS key | It’ll check whether the key exists or not. |
4 | EXPIRE key seconds | Sets the expiry time of the key. |
5 | PEXPIRE key milliseconds | Set the expiry of a key in milliseconds. |
6 | KEYS pattern | Finds all keys matching the specified pattern. |
7 | MOVE key db | Moves a key to another database. |
8 | PERSIST key | Removes the expiration from the key |
9 | TTL key | Gets the remaining time in keys expiry. |
10 | RANDOM KEY | Returns a random key from Redis. |
11 | RENAME key newkey | Change the key name. |
12 | TYPE key | Returns 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.No | Command | Description |
1 | SET key value | Set the value for the specified key. |
2 | GET key | Gets the value of a given key. |
3 | GETRANGE key start end | Gets a substring of the string. |
4 | GETSET key value | Set the value of a key and return its old value. |
5 | MGET key1 [key2..] | Gets the values of all the listed keys |
6 | SETBIT key offset value | Set or clear the bit at the offset in the string value stored at the key. |
7 | SETEX key seconds value | Sets the value with the expiry time of a key |
8 | SETNX key value | Sets the value of a key, if the key doesn’t exist |
9 | SETRANGE key offset value | Overwrites the part of a string at the key starting at the specified offset |
10 | STRLEN key | Get the length of the value. |
11 | MSET key value [key value …] | Sets multiple keys-values |
12 | PSETEX key milliseconds value | Sets the value and expiry time in milliseconds. |
13 | INCR key | Increments the integer value by one |
14 | INCRBY key increment | Increments the integer value by the given amount |
15 | INCRBYFLOAT key increment | Increments the float value by the given amount |
16 | DECR key | Decrements the integer value by one |
17 | DECRBY key decrement | Decrements the integer value by the given number |
18 | APPEND key value | Appends 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.No | Command | Description |
1 | RPUSH key value1 [value2] | Appends one or multiple values |
2 | LPUSH key value1 [value2] | Prepends one or multiple values |
3 | LINDEX key index | Get an element from a list by its index value |
4 | LPOP key | Removes and gets the first element in a list |
5 | LLEN key | Gets 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
What is Redis?
Redis is an example of an in-memory Key-Value store which in addition to being an in-memory cache is also a database. In addition, it uses the key-value data model and provides the owner with an efficient way of data retrieval and various operations on the data.
What is Redis cache?
Redis commands are also quite useful for a range of tasks, such as caching, real-time data analysis, sessions, message brokering, and many other applications. It stands out well for use in situations that involve quick access and manipulation of data hence employing this choice is ideal for high-performance systems and applications.
What are Redis commands used for?
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.
How are Redis commands used for caching?
This is because in Redis commands, information is temporarily stored, in key-value data structures because it is fast and stores data in memory, it even used as a cache.
What is the difference between Redis commands and a database?
An SQL database uses structured data model which describes how the data is stored in tables The data structure used in an SQL database is a fixed schema table format. However, Redis uses a flexible data model, where data can be accessed as ‘strings,’ ‘hashes,’ ‘lists,’ ‘sets,’ and ‘sorted sets’.