Using Redis Cache with Nodejs to make application super fast

Redis is used as a database and for cache since it’s super fast due to the fact that the data is stored “in-memory” contrary to other databases in which the data is usually stored “on-disk”.

What is Redis ?

Redis is a data structures server. It runs in memory and you can communicate with it over the network thanks to the Redis protocol.

It shines when you want to use data structures such as sets, lists or hashes in a distributed program, be it across different machines or processes in the same machine.

Redis can also do other things such as publish/subscribe.

There are lots of use cases for such a piece of software. Here are a few typical ones in the context of a complex Web application:

* storing user session data;

* caching things like user feeds (timelines);

* implementing rate-limiting;

* implementing a URL shortener.

What are other use cases of Redis ?

Pub/Sub: You can keep track of subscribers and what information they are interested in. You can use SUBSCRIBEUNSUBSCRIBE, and PUBLISH.

Queues: You can use normal queue data structure, Redis has blocking queuecommands as well.

Real time analysis of events(stats, anti spam etc) – Using Redis primitives it’s really simple to implement a real-time tracking filtering system or a spam filtering system.

Order by user votes and time: Leaderboard type functionality(Quora/Reddit/Digg), where the score is changes over time.

Transactional support: Provides transactional support. See Transactions – Redis.

Caching: Can be used as a general purpose cache. keys can strings, or more complex types (lists, sets, hashes etc). Also allows LRU eviction of items.

Due to it’s persistence capabilities, it can be used as a datastore and not just a cache. Redis has many other builtin operations which you would otherwise have to develop at your own.


Before we begin, let’s look at the installation of Redis on the various platform.

Installing Redis

To install Redis in Mac and Linux you can either do the manual build which is mentioned here OR use these commands to install it on go.

On ubuntu

sudo apt-get install redis-server

On Mac using brew

brew install redis

Windows has no official package from Redis team but there is some port available which you can use at your own risk.


You can check whether redis service is running with the following command:

$ sudo systemctl status redis

As you can see, redis service is running.

If redis service is not running in your case, start it with the following command:

$ sudo systemctl start redis

After installation, you can use the following command to start the Redis server.


You should see the following screen.

To access the Redis command line interface, run the following command from a separate terminal.


You should see the following screen.

Try running “PING” command and you should recieve “PONG”.

Using Redis in your NodeJS application

First you need to install the Redis client for NodeJS via npm.

npm install redis

Now create a file called redisDemo.js in your NodeJS project.

// redisDemo.js 
var redis = require('redis'); 
var client = redis.createClient(); // this creates a new client

By default redis.createClient() will use and port 6379. If you have a customized ip and and a port use

var client = redis.createClient(port, host);

Now, we want to listen for the connect event to see whether we successfully connected to the redis-server. We can check for a successful connection like this.

client.on(‘connect’, function() {
console.log(‘Redis client connected’);

Likewise, we want to check if we failed to connect to the redis-server. Well we can listen for the error event for that.

client.on(‘error’, function (err) {
console.log('Something went wrong ’ + err);

Enough of Introduction. Let see example now.

Create an index.js file with the code below and run it with node index.js:

var redis = require(‘redis’);
var client = redis.createClient();

client.on(‘error’, function(err){
console.log('Something went wrong ', err)

client.set(‘my test key’, ‘my test value’, redis.print);
client.get(‘my test key’, function(error, result) {
if (error) throw error;
console.log(‘GET result ->’, result)

This will create a record in the database which you can access with Redis Desktop

or in the command line:

redis-cli get ‘my test key’

And that is it for a simple record creation in redis using node.js. You can find more of redis with node.js here.

Data types

The data types include:

  • Strings
  • Lists
  • Sets (sorted or otherwise)
  • Hashes
  • Bitmaps
  • HyperLogLogs

There are various ways to access and set those data types in redis, to show you a couple:

Storing Strings

All the Redis commands are exposed as different functions on the client object. To store a simple string use the following syntax:

client.set(‘framework’, ‘AngularJS’);


client.set([‘framework’, ‘AngularJS’]);

The above snippets store a simple string AngularJS against the key framework. You should note that both the snippets do the same thing. The only difference is that the first one passes a variable number of arguments while the later passes an argsarray to client.set() function. You can also pass an optional callback to get a notification when the operation is complete:

client.set(‘framework’, ‘AngularJS’, function(err, reply) {

If the operation failed for some reason, the err argument to the callback represents the error. To retrieve the value of the key do the following:

client.get(‘framework’, function(err, reply) {

client.get() lets you retrieve a key stored in Redis. The value of the key can be accessed via the callback argument reply. If the key doesn’t exist, the value of reply will be empty.

Storing Hash

Many times storing simple values won’t solve your problem. You will need to store hashes (objects) in Redis. For that you can use hmset() function as following:

client.hmset(‘frameworks’, ‘javascript’, ‘AngularJS’, ‘css’, ‘Bootstrap’, ‘node’, ‘Express’);

client.hgetall(‘frameworks’, function(err, object) {

The above snippet stores a hash in Redis that maps each technology to its framework. The first argument to hmset() is the name of the key. Subsequent arguments represent key-value pairs. Similarly, hgetall() is used to retrieve the value of the key. If the key is found, the second argument to the callback will contain the value which is an object.

Note that Redis doesn’t support nested objects. All the property values in the object will be coerced into strings before getting stored.

You can also use the following syntax to store objects in Redis:

client.hmset(‘frameworks’, {
‘javascript’: ‘AngularJS’,
‘css’: ‘Bootstrap’,
‘node’: ‘Express’

An optional callback can also be passed to know when the operation is completed.

All the functions (commands) can be called with uppercase/lowercase equivalents. For example, client.hmset() and client.HMSET() are the same.

Storing Lists

If you want to store a list of items, you can use Redis lists. To store a list use the following syntax:

client.rpush([‘frameworks’, ‘angularjs’, ‘backbone’], function(err, reply) {
console.log(reply); //prints 2

The above snippet creates a list called frameworks and pushes two elements to it. So, the length of the list is now two. As you can see I have passed an args array to rpush. The first item of the array represents the name of the key while the rest represent the elements of the list. You can also use lpush() instead of rpush()to push the elements to the left.

To retrieve the elements of the list you can use the lrange() function as following:

client.lrange(‘frameworks’, 0, -1, function(err, reply) {
console.log(reply); // [‘angularjs’, ‘backbone’]

Just note that you get all the elements of the list by passing -1 as the third argument to lrange(). If you want a subset of the list, you should pass the end index here.

Storing Sets

Sets are similar to lists, but the difference is that they don’t allow duplicates. So, if you don’t want any duplicate elements in your list you can use a set. Here is how we can modify our previous snippet to use a set instead of list.

client.sadd([‘tags’, ‘angularjs’, ‘backbonejs’, ‘emberjs’], function(err, reply) {
console.log(reply); // 3

As you can see, the sadd() function creates a new set with the specified elements. Here, the length of the set is three. To retrieve the members of the set, use the smembers() function as following:

client.smembers(‘tags’, function(err, reply) {

This snippet will retrieve all the members of the set. Just note that the order is not preserved while retrieving the members.

This was a list of the most important data structures found in every Redis powered app. Apart from strings, lists, sets, and hashes, you can store sorted sets, hyperLogLogs, and more in Redis. If you want a complete list of commands and data structures, visit the official Redis documentation. Remember that almost every Redis command is exposed on the client object offered by the node_redis module.

Now let’s have a look at some more important operations supported by node_redis.

Checking the Existence of Keys

Sometimes you may need to check if a key already exists and proceed accordingly. To do so you can use exists() function as shown below:

client.exists(‘key’, function(err, reply) {
if (reply === 1) {
} else {
console.log(‘doesn’t exist’);

Deleting and Expiring Keys

At times you will need to clear some keys and reinitialize them. To clear the keys, you can use del command as shown below:

client.del(‘frameworks’, function(err, reply) {

You can also give an expiration time to an existing key as following:

client.set(‘key1’, ‘val1’);
client.expire(‘key1’, 30);

The above snippet assigns an expiration time of 30 seconds to the key key1.

Incrementing and Decrementing

Redis also supports incrementing and decrementing keys. To increment a key use incr() function as shown below:

client.set(‘key1’, 10, function() {
client.incr(‘key1’, function(err, reply) {
console.log(reply); // 11

The incr() function increments a key value by 1. If you need to increment by a different amount, you can use incrby() function. Similarly, to decrement a key you can use the functions like decr() and decrby().



Redis is very powerful in-memory data-store that we can use in our applications. It’s very simple to save and get data without much overhead. refer for more use cases and refer for more redis commands.

#node-js #javascript

What is GEEK

Buddha Community

Using Redis Cache with Nodejs to make application super fast
Loma  Baumbach

Loma Baumbach


Redis Transactions & Long-Running Lua Scripts

Redis offers two mechanisms for handling transactions – MULTI/EXEC based transactions and Lua scripts evaluation. Redis Lua scripting is the recommended approach and is fairly popular in usage.

Our Redis™ customers who have Lua scripts deployed often report this error – “BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE”. In this post, we will explain the Redis transactional property of scripts, what this error is about, and why we must be extra careful about it on Sentinel-managed systems that can failover.

Redis Lua Scripts Diagram - ScaleGrid Blog

Transactional Nature of Redis Lua Scripts

Redis “transactions” aren’t really transactions as understood conventionally – in case of errors, there is no rollback of writes made by the script.

Atomicity” of Redis scripts is guaranteed in the following manner:

  • Once a script begins executing, all other commands/scripts are blocked until the script completes. So, other clients either see the changes made by the script or they don’t. This is because they can only execute either before the script or after the script.
  • However, Redis doesn’t do rollbacks, so on an error within a script, any changes already made by the script will be retained and future commands/scripts will see those partial changes.
  • Since all other clients are blocked while the script executes, it is critical that the script is well-behaved and finishes in time.

The ‘lua-time-limit’ Value

It is highly recommended that the script complete within a time limit. Redis enforces this in a weak manner with the ‘lua-time-limit’ value. This is the maximum allowed time (in ms) that the script is allowed to run. The default value is 5 seconds. This is a really long time for CPU-bound activity (scripts have limited access and can’t run commands that access the disk).

However, the script is not killed when it executes beyond this time. Redis starts accepting client commands again, but responds to them with a BUSY error.

If you must kill the script at this point, there are two options available:

  • SCRIPT KILL command can be used to stop a script that hasn’t yet done any writes.
  • If the script has already performed writes to the server and must still be killed, use the SHUTDOWN NOSAVE to shutdown the server completely.

It is usually better to just wait for the script to complete its operation. The complete information on methods to kill the script execution and related behavior are available in the documentation.

#cloud #database #developer #high availability #howto #redis #scalegrid #lua-time-limit #redis diagram #redis master #redis scripts #redis sentinel #redis servers #redis transactions #sentinel-managed #server failures

Gordon  Taylor

Gordon Taylor


Fasten Your Node JS Application with A Powerful Caching Mechanism using Redis

Why caching is so important:

Caching is the process of storing copies of files and data in a cache or some temporary space. By the use of caching, we fetch data very quickly. The best example of a caching tool is Cloudflare . I know Cloudflare is not the only caching tool, it is having other functionality also.

There are two main reasons for using caching in the application:

  1. Save the cost. Less amount of data sent over the network.
  2. Less response time.

Process of Caching:

The process of caching is so simple. Suppose you want to get some data from an API. In the first call, you will hit the API and get the data after that you will set or save that data in cache. In the future, if you want to get the same data, you don’t need to hit the API. First, you will check the data available in cache or not. If data available in the cache, then you will fetch data from the cache otherwise you will hit the API.

In this tutorial, first, we will get the data from fake API without Redis and then with Redis and check the difference of response time with visual representation.

Create a simple server in Express JS:

If you are new to the NodeJS world then please read my tutorial . In this tutorial, you will learn how to create an express js server and CRUD operations. Otherwise, follow the below steps to create a simple server in express js.

Create the package.json file:

npm init -y

#nodejs #redis #javascript #cache #typescript

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications?

Hire NodeJs Developer to create a real-time, faster, and scalable application to accelerate your business. At, we have a team of expert Node.JS developers, who have experience in working with Bootstrap, HTML5, & CSS, and also hold the knowledge of the most advanced frameworks and platforms.

Contact our experts:

#hire nodejs developer #nodejs developer #nodejs development company #nodejs development services #nodejs development #nodejs

Redis Caching inNode.js

Modern apps need to reply in less than 200ms end to end at any scale -> less than 100ms should be spent inside the data center

So now you get the point of this whole article. Yes right we would be talking about blazing fast REmote DIctionary SErver

So what is Redis?

Redis is an open-source in-memory data structure store which can be used as a database, cache, and message broker.

Redis supports multiple data structures and can perform around 110,000 sets per second and about 81000 gets per second.

Redis can be considered as a mix of MongoDB and Memcached this is why it provides rich datatypes support, caching, and disk persistence.

Is it only about the speed and data type support?


Redis has these features too

  • very flexible
  • no schemas & column names
  • designed to be accessed by trusted clients
  • do not allow external access/internet exposure.
  • simple authentication can be set up.
  • can be restricted to certain interfaces

So let’s dive into some of the data structures and commands but before that, you have to install Redis.

follow the official link

#redis-node #nodejs #scaling #redis #caching

Redis Labs Partners with Microsoft to Deliver a New Redis Cache

In a recent blog post, Microsoft announced a new partnership with Redis Labs to deliver Redis Enterprise as newly, fully integrated tiers of Azure Cache for Redis. The enhanced service offering, currently in private preview, will provide customers with two new Enterprise tiers – which include Redis on Flash, modules, and the ability to create an active geo-redundant cache for hybrid-cloud architectures in the future.

Microsoft started their collaboration with Redis Labs back in 2014 with the launch of Redis Cloud on Azure. Since then the service has evolved with updates such as geo-replication support and reserved-capacity. Now, the public cloud vendor incorporates two existing offerings of Redis Labs as additional Enterprise tiers in Azure Redis Cache service providing customers with more features, higher availability, and security capabilities.

Ofer Bengal, CEO and co-founder of Redis Labs, wrote in his blog post on the new partnership announcement:

Throughout the development process, three key customer drivers were consistently top of mind: improve developer productivity, ensure operational resiliency, and ease cloud migration. Teams at both organizations were committed to building an integration that delivers these values to our customers. With the announcement of Redis Enterprise integration into Azure Cache for Redis, we meet these needs.
With the new tiers, developers can use the most up-to-date version of Redis, including its native data structures, probabilistic data structures, streams, time-series, and search data models. Furthermore, they can benefit from the native integration with other Azure services, and easily deploy a Redis cluster and scale to terabyte-sized data sets at the cost of a disk-based data store by utilizing Redis on Flash technology. Also, with the added support of the Redis modules

RediSearch, RedisTimeSeries, and RedisBloom developers can build applications for a wide variety of use cases with a single technology.

#microsoft azure #clustering & caching #redis #microsoft #cloud #distributed cache #caching #devops #architecture & design #development #news