What is Redis?
Redis is open-source software released under a BSD 3-clause license, a specific type of permissive free software license. Redis began when Salvatore Sanfilippo needed to improve scalability on his website. He soon open-sourced the platform. Nowadays, the core team develops and maintains Redis, which has been sponsored by Redis Labs since 2015.
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 are used to perform operations on the Redis.
To run commands on the Redis server, you need a Redis client. The Redis client is available in the package also. To start the Redis client, open the terminal on your machine and type the Redis-client command. It’ll connect to your local server now you can run any command.
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 |
Leave a Reply
Want to join the discussion?Feel free to contribute!