Getting Started with Caching (Redis + NodeJS)
This post is a basic introduction to caching and how it works. We will be using Redis for caching with Nodejs to show you how we can take advantage of caching to improve the speed and performance of the application and to reduce the overhead from server resources.
What is Caching ?
Caching (pronounced “cashing”) is the process of storing data in a cache. A cache is a temporary storage area. A cache as a data store is easier for the client (or server) to reach, as opposed to a permanent data store that might be located on a different service, which takes more time and resources to reach (a database or an external API endpoint).
For example: The files you automatically request by looking at a Web page are stored on your hard disk in a cache subdirectory under the directory for your browser.
Now let’s take a look on Redis. (Source: Redis Official)
What is Redis ?
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Let’s build very basic project to implement caching using redis
Install Redis:
If you are an OSX user, install it using the command below.
brew install redis
Other helpful commands:
1. start Redis server: brew services start redis
2. stop Redis server: brew services stop redis
3. restart Redis server: brew services restart redis
For other platforms,
To install, please follow the guide on https://redis.io/download.
For other commands: Check this.
Create a new directory
mkdir redis-tutorial
Navigate to new directory
cd redis-tutorial
Setup npm package.json file
npm init -y
-y
means using default option when creating package.json file
After running above command, you should have package.json file in your redis-tutorial directory.
Create a index.js
file in directory.
Install express, node-fetch, redis npm modules
npm install express node-fetch redis --save
Now we have Redis installed and up add following code to index.js
file.
Now start your node server with node index.js
command. (Make sure redis is up and running)
Go to localhost:3000/posts
on Postman, You will get result like this
Second Request:
Now if you look into the time took by 1st request is 761ms but the time took by 2nd request is only 28ms and the source of 1st request is API and 2nd is Redis.
Conclusion:
Redis request is much faster than normal data fetching from API. Redis is very powerful in-memory data-store that we can use in our applications for caching. It’s very simple to save and get data without much overhead.
Refer https://www.npmjs.com/package/redis for more use cases.
Refer https://redis.io/commands for more redis commands.