Getting Started With Redis And It's Data Types On Redis-CLI
Published: 2022-01-31 07:38:54

## REDIS (REmote DIctionary Service)  

### What is Redis?  

Redis is an open source, **in-memory data structure store**, used as a database or as a cache. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, streams and much more.  
You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.

Redis is written in ANSI C and works in most systems like Linux and OS X, without external dependencies. Linux and OS X are the two operating systems where Redis is developed and tested the most, and it is recommended to use Linux for deployment. There is **no official support for Windows** builds.

### Use cases of Redis  
- Queues  
- Publish/Subscribe (Pub/Sub)  
- Real-time analytics  
- Geospatial processing  
- Leaderboards/counting  
- Session Cache  
- Full page cache  

### Redis Installation

1. For Windows  
Though Redis is not supported on Windows, we can have a workaround for it. 
- Enable WSL from "Turn Windows features on or off settings"
- Restart the machine
- Install Ubuntu20.04 from Windows Store
- Open Ubuntu App and setup name and password
- Run the following commands:
    > sudo apt update  
    > sudo apt upgrade  
    > sudo apt install redis-server  
    > sudo service redis-server restart  
    > redis-cli  

2. For Linux  
Redis installation on Linux is super easy.  
Run the commands:  
   > sudo apt install redis-server  
   > sudo service redis-server restart  
   > redis-cli  

The command `redis-cli` will start the redis command line on which we will be interacting with redis store.  

-----------------------------------------------------------------------

## Common Redis Commands  

### NORMAL USAGE AS A KEY VALUE STORE

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643459191285/mB5AjTX-F.png)

- `SET *key value*` => Set a key value pair
- `GET *key*`  => Get a value by searching for corresponding key
- `TTL *key*`  => Time To Live is the expiry(time left) of the key 
- `KEYS *regex*` => To search for the pattern of keys. Common usage: `KEYS *`  
- `DEL *key*` => Deletes the key if present  
- `FLUSHALL` => Remove all keys present  
- `EXISTS *key*` => Returns 1 if key is present else return 0
- `EXPIRE *key time*`(in sec)  => Sets expiry time to the key if it exists
- `SETEX *key time value*`  => Sets a new key value pair with given TTL  

### LIST/ARRAY  
Redis Lists are simple lists of strings, sorted by insertion order. We can add elements  from the left or right end of the list.  

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643459755681/ISgZncd9r.png)

- `LPUSH *arr val1 val2 val3 ....*` => Left push in arr(key) values val1, val2 ....
- `RPUSH *arr val1 val2 val3 ....*` => Right push in arr(key) values val1, val2 ....
- `LRANGE *arr start stop*` => Return elements of arr from index range *start* to *stop*   
- `LPOP *arr*` => Pop leftmost element of list  
- `RPOP *arr*` => Pop rightmost element of list  


### SET  
Redis Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key.  

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643459963836/xDH-DeqUj.png)

- `SADD *key val1 val2 ...*`  => Adds one or more values to a set
- `SMEMBERS *key*` => Gets all the members in a set
- `SCARD *key*` => Gets the number of members in a set  
- `SREM *key val1 val2 ...*` => Removes one or more members from a set


### OBJECT/HASH  

Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects. In a lot of ways, we can think of `HASH`es in Redis as miniature versions of Redis itself.  
In Redis, every hash can store up to more than 4 billion field-value pairs.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643460534622/z0JiA34lF.png)

- `HSET *key field value*` => Sets field in hash stored at the key to value
- `HGET *key field*` => Gets the value of a hash field stored at the specified key
- `HGETALL *key*` => Gets all fields and values stored in a hash at the specified key
- `HDEL *key field*` => Deletes one or more hash fields
- `HEXISTS *key field*` => Determines whether a hash field exists or not    

> P.S : The commands listed for the above data types are not all. There are a lot of other commands which can be found on the redis official website. 
 
## Resources And References

The official website of Redis is the most awesome resource I have found to know anything about using Redis and it's commands: [https://redis.io/commands](https://redis.io/commands)  

Also this cheat sheet I found on the web: [Link](https://lzone.de/cheat-sheet/Redis) 

## Wrap Up  

I hope you found this blog helpful and keep it handy for a quick reference. If you want me to add more information in this blog or need any help please let me know in the comment section.

Let's connect on [LinkedIn](https://www.linkedin.com/in/audarya-uttarwar/)

👋 Thanks for reading, See you next time.