what-is-Redis

What is Redis?

Redis stands for Remote Directory Serve. It is an in-memory data structure store. That means its storage is held in memory, not on a hard drive. It is a key-value database that supports multiple data structures or data types. That means that while Redis supports mapped key-value-based strings to store and retrieve data, it also supports other complex data structures like lists, sets, etc.

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

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.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.

Hire NodeJs Developer

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
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply