A SnapShot of Redis

E.Y.
4 min readJul 19, 2020

--

Photo by Heather Ford on Unsplash

What is it?

Redis- full name Remote Dictionary Server, is an open-source in-memory database, that works with key values and written in C language. So for each value, there is one key. Redis’ unique structure enables reading and writing data really fast and scalable. However for important data, it’s better to save it in a more persistent database due to Redis’ volatility.

Popular methods:

Connect:

//app.jsvar redis = require('redis');
var client = redis.createClient(); //creates a new client

By default, redis.createClient() will use 127.0.0.1 and 6379 as the hostname and port respectively. But normally we might make it more flexible like:

const PORT = process.env.PORT || 3000;
const REDIS_PORT = process.env.REDIS_PORT || 6379;

You can also listen for connect event like below:

client.on('connect', function() {
console.log('connected');
});
//console: connected

Storage

//store simple string 
client.set('email', 'elfi@test.com');
//store string with callback
client.set('email', 'elfi@test.com', function(err, result) {
console.log(result);
});
//store hash table
client.hset("email", data.id, JSON.stringify(data));
//store hash with expiry time
client.hset(key, fieldname, result, "EX", 10);
//store list/array
client.rpush(['email', 'elfi@test.com', 'julia@test.com'], function(err, result) {
console.log(result); //prints 2 --> the len of the list
});
//"email" as the first item is the key
//store set (non duplicated list)
client.sadd(['email', 'elfi@test.com', 'julia@test.com'], function(err, result) {
console.log(result); // 2
});

Retrieve

//retrieve simple key-value
client.get('email', function(err, res) {
console.log(res);
});
//retrieve hash table
client.hgetall('email', function(err, result) {
console.log(result);
});
//retrieve list
client.lrange('email', 0, -1, function(err, result) {
console.log(result); // ['elfi@test.com', 'julia@test.com']
});
//retrieve set
client.smembers('email', 0, -1, function(err, result) {
console.log(result); // ['elfi@test.com', 'julia@test.com']
});

Other

//check if a key-value pair existsclient.exists('key', function(err, result) {
if (result === 1) {
//note this will come back as 1 or 0
//1==> exist; 0 ==> not exist
console.log('exists');
} else {
console.log('doesn\'t exist');
}
});
//delete certain key-value
client.del(‘key’)

Note that in JavaScript we often need to get object/JSON, however Redis store everything as string, so make sure to serialise and de-serialise object before you save it to redis.

client.set(‘user’, 
JSON.stringify({ firstname: ‘elfi’, email: ‘elfi@test.com’})
)
client.get(‘user’, (err, val) => {
console.log(JSON.parse(val))
})

Common Use Cases

  • Session Cache: Per AWS suggests, Redis serves as session cache can help bypassing problems with storing data in the web server by paying the small price of a tiny network latency. For example, you can store the user profiles as Redis hashes with each user field (e.g. name, email ) as a hash field. This essentially represents an object shape.
  • Ranking system: for websites/forums that need a ranking/weighting system, like stackoverflow or reddit, they can use Redis sorted set to give the most voted answer with the highest weight and lowest with the smallest weight. And incrementing/decrementing in Redis is also super fast(check more on here).
  • Data feed: Since Redis enables you to insert data from head / tail (lpush, rpush,) this can help create a news feed system. For example, social media will use this feature to create timeline feeds, and push the import/trending posts to the top of the feeds. RSS subscription can also use this feature, where you can insert the most recent update to the top of your list.
  • Real-time/Complex analysing: A lot of real-time analysis requires fast operation of data relationship using joins like union, intersection, subtraction. And this can be done using Redis set. For example, e-commerce store might want to see the percentage of female customers purchased certain product at one time. Also ntoed that with Redis set, only unique keys will be added. So if the key already exists, then the add is ignored.

Example implementation of caching

The following example is a super basic implementation of using Redis to cache blogs belong to certain user. It’s been used within an express request function.

So if the user’s blogs are cached they are return from Redis; if not, they will hit the MongoDB database to get the blogs, cache in Redis, and return the blogs.

You may want to extract this part to a separate module in real life. But I just group here to make it easier to see.

app.get(“/api/blogs”, requireLogin, async (req, res) => {// 1. init Redis client
const redis = require(“redis”);
const redisUrl = “redis://127.0.0.1:6379”;
const client = redis.createClient(redisUrl);
const util = require(“util”);
client.get = util.promisify(client.get); //instead of callback //returns a promise//2. do we have cache data in redis?
const cachedBlogs = await client.get(req.user.id);
//3. if yes ==> return data
if (cachedBlogs) {
console.log(“serving from Redis”);
return res.send(JSON.parse(cachedBlogs));
}
//4. if no ==> query MongoDB, return response, and update redis
const blogs = await Blog.find({ _user: req.user.id });
console.log(“serving from Mongo”);
res.send(blogs);
client.set(req.user.id, JSON.stringify(blogs));
});

That’s it!

Happy Reading!

🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄 🍬 🍭🦄

--

--