Engineer bro!

Sat Mar 19 2022 (2 years ago)

Engineer by mistake!

I am a software engineer by passion

Getting started with Redis and NodeJs

Redis is an in-memory key/value data store, which is used for caching purposes and fast retrieval of data. Redis can be used with any programming language but throughout this article, we'll use NodeJs.

What is Redis?

Redis is an in-memory data structure store, used as a distributed, in-memory key-value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices.

Other features of Redis

Although Redis is mainly known for in-memory cache, it also provides a few other interesting features out of the box.

We'll not explore these features.

  • Pipelining - We can execute multiple commands at once to reduce round-trip time.

  • Pub/Sub - Pub/Sub is an architecture that is used in event-driven applications.

  • Expires - Redis allows setting a time to live different for every key so that the key will be automatically removed from the server when it expires.

What is NodeJs?

Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project! Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant. A Node.js app runs in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behaviour the exception rather than the norm.

Features of NodeJs

  • Single-Threaded

  • Asynchronous

  • Event-Driven

  • Open Source

  • ....

What is caching?

In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere.

Let's take the example of Facebook posts. In every Facebook post, you can see the user profile pictures. For every post, a new request is being made to the server to fetch user profile information.

Untitled Diagram drawio

For every user profile request, the server has to read the profile information from DB then respond back to the client. Suppose our DB is far from the server, then, in this case, we would have latency in reading from DB.

Suppose a request is taking 1s to reach from user to serer and 1s to reach from server to DB. Then to get the profile page, we need a total of 4s.(go/back).

This is just an example.

So, every profile picture will take on an average of 4s to display. To solve this problem, we have an open-source tool that can help us to cache the responses.

Untitled Diagram drawio (2)

On the above diagram. you can see that we have introduced Redis as a cache DB. So, whenever we get a request from the client. We first check the user id into Redis and ask whether we have a cache for that user id or not. If it has the cache, then it'll return the response immediately. Otherwise, the server will fetch the data from DB and it'll also store the data into Redis.

Accessing data from Redis is about milliseconds of tasks. So, it'll dramatically reduce the server latency.

Implemention in NodeJs

Let's implement Redis into NodeJs.

Install redis client

npm i redis

Connect to the redis

const client = createClient();
client.on("error", (err) => console.log("Redis Client Error", err));
  .then(() => console.log("Redis connected"))

Now, you are ready to get and set data to and from Redis.

async function test(){
    const data = await client.get(key);
    await client.set(key,value);

There is more in Redis than just get/set you can explore

The implementation demo is available in here.

I have used delay to simulate heavy computation task and i have used sleep to simulate acynchronous long running task, such as fetching resourses from an upstream server.

Thank You ❤️

EngineeringSoftware EngineeringNodeJsRedis

© 2021 dsabyte. All rights reserved