DEV Community

Cover image for Redis For Beginners
Jemmy Dalsaniya
Jemmy Dalsaniya

Posted on

Redis For Beginners

Basic Overview πŸ“–

  • Why Redis is needed ??

Ans: In contemporary application development, Redis plays a critical role by serving as a highly efficient in-memory data store. Its primary function is to cache computed data, which significantly reduces server load caused by excessive database queries. This caching mechanism ensures that frequently accessed data can be retrieved quickly, thus minimizing response times.

By integrating Redis, applications achieve two key optimizations:

Reduced Query Load: Redis stores frequently requested data in memory, alleviating the need for repeated queries to the primary database. This not only reduces the workload on the database server but also enhances overall system performance by avoiding bottlenecks associated with frequent data retrieval operations.

Enhanced Response Time: When data is cached in Redis, it can be returned to the user almost instantaneously. This rapid data access is crucial for applications requiring real-time responses, thereby improving user experience and making the application more responsive and efficient.

  • Define Redis: Redis is an open source in-memory data store that can be used as a database, cache, or message broker. It's often used for caching web pages and reducing the load on servers.Redis run on the port number 6379

  • Redis have various data-types to store the data. It includes String,Set,List,Hash,etc


Redis Architecture: βš™οΈ

architecture

  • Cache Lookup: Upon receiving a request, the system first checks Redis to see if the requested data is already cached. If the data is found (cache hit), it is immediately returned to the client, ensuring a fast response.

  • Cache Miss Handling: If the data is not found in Redis (cache miss), the system then queries the persistent database to retrieve the necessary data. This ensures that the data retrieval process continues, albeit at a slightly slower pace compared to a cache hit.

  • Cache Priming: Once the data is fetched from the persistent database, it is not only returned to the client but also stored in Redis. This step, known as priming the cache, ensures that subsequent requests for the same data can be served quickly from the cache, improving overall system performance.


Installation βš’οΈ

  • Firstly we create the redis container in Docker. For that you must have installed the Docker Desktop. You may visit my blog inorder to know how to install the Docker: Click here

  • Open the Docker Desktop and open the command line and paste the following command.

docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

  • As soon as you run the command you will find the container named "redis stack" running in the docker desktop.

container

  • Open the chrome and write localhost:8001, if you get following output then your redis installation is successfull:

redis stack

  • Now write the following command to get into the container shell :

docker exec -it redis-stack bash and write redis-cli --version to get the redis version :

cli

  • Write the redis-cli command to get into the redis server

cli enter

  • Now you are ready to write the redis command and execute.

Setting up in VS Code in order to code along

  • Create a folder in vs code and do npm install in terminal. You have to install ioredis package to use it with nodejs. Run the following command to do it :
    npm i ioredis

  • Make a file name client.js and write the code shown below: code

client


Commands for Strings:

-> set key value : For eg. set name redis : To set the data

-> get key: for eg. get name => "redis" : To get the data

set get

-> set key value nx : Here nx is a special command that will cached the data only if not already cached before.

-> mget key1 key2 key3 : To simultaneously get more than 1 cached data

-> mset key1 "value1" key2 "value2": To set multiple cached data simultaneously

mset mget

-> incr key : to increment the value by 1

-> incrby key n : to increment value by n

incr

-> expire key nsec : will delete the key value after n sec

  • Implementing string data type in nodejs: code

example string

  • Additional Commands :

-> SETRANGE key offset value
-> GETRANGE key start end
-> SUBSTR key start end


Commands for List:

-> lpush key value: push the element into the list from left side

-> rpush key value: push element into the list from right side

-> lrange key start stop: return the element in range

-> llen key value: return the length of the list

push len

-> lpop key value: removes and return the head of the list

lpop

-> del key: to delete the list

del

  • Similar like it there is other command to do operation from right side such as rpush, rpop, etc.

  • Additional Commands :

-> LMOVE source destination : atomically moves elements from one list to another.
-> LRANGE key start stop : extracts a range of elements from a list.
-> LTRIM key start stop : reduces a list to the specified range of elements.

  • Implementing list data type in nodejs: code

list node


Commands for Set:

-> sadd key value: to add value to the set

->srem key value: to remove from the set

-> sismember key value: to check that value present in set or not

-> sinter key1 key2: to get the common element between two set

-> smembers key: to get the all element in set

set

get mem


Commands for Hash:

-> hset key field value field value: to add the data to the hash set

-> hget key field : to get the value of particular field

->hget all key: to get all the value of key

-> hmget key field1 field2: : to get the value of multiple field of key

-> hincrby key value n : to increment field value by n

  • Implementing hash data type in nodejs: code

hash node


Commands for Sorted Set:

->zadd key n value: sort with according to the lowest value of n

-> zrange key start stop: to get all the value

-> zrevrange key start stop: to get all the value in reverse order

-> zrank key value: to get the rank of the given value


Practical Example of Fetching todo list:

Code :

code

  • Before implementing Redis :

Before

  • After implementinf Redis:

After

  • Thus we can see that after implementing redis the time taken to fetch the todos is very less compare to the previous one. This is because the data is already cached and get it directly from there.

Conclusion

Conclusion

In this blog, we delved into the essential role of Redis in modern application development. By serving as a high-performance in-memory data store, Redis significantly reduces server load and enhances response times through effective caching mechanisms. We explored Redis's architecture, which efficiently handles cache lookups, cache misses, and cache priming to ensure swift data retrieval and improved system performance. Furthermore, we provided detailed installation steps, essential commands for various Redis data types, and practical examples to illustrate its implementation in Node.js. The practical comparison demonstrated how integrating Redis dramatically reduces data fetching times, highlighting its value in optimizing application performance and user experience. For those interested in hands-on implementation, the provided GitHub repository offers a comprehensive guide to getting started with Redis.

Top comments (0)