Beginner Redis cloud console : AWS

Beginner Redis cloud console : AWS

ยท

2 min read

What is Redis?

Redis is an open-source, in-memory data structure store(i.e. it stores data in ram) that can be used as a database, cache, and message broker. It is often referred to as a data structure server because it allows you to store and manipulate data structures.

Redis cloud console setup

Connect db to RedisInsight

We will be using redis-client (Nodejs) and redisInsight for some visual representation.

Data types -

  1. Strings - strings store sequences of bytes, including text, serialized objects, and binary arrays.

     const redis = require('redis');
     const client = redis.createClient();
    
     // simple string value stored
     await client.set('key', 'value');
    
     // get value
     await client.get('key');
    

  2. Lists - linked lists of string values

     // pushes items into the list
     await client.lPush('bikes:repairs', 'bike:1');
     await client.lPush('bikes:repairs', 'bike:2');
    
     // get items out of the list
     await client.rPop('bikes:repairs'); // bike:1
     await client.rPop('bikes:repairs'); // bike:2
    
     // length of the list
     await client.lLen('bikes:repairs');
    

  3. Hashes - collections of field-value pairs

     // store hashes(object) in redis
     await client.hSet('obj', {
          'key1': 'value1',
          'key2': 'value2',
     })
    
     // retrieve whole hash
     await client.hGetAll('hash_key');
    
     // retrieve value for particular key
     await client.hGet('hash_key', 'key1'); // gives value 1
    

  4. Sets - unordered collection of unique strings

     // to add members
     await client.sAdd('set1', ['val1', 'val2', 'val3']);
    
     // to get members
     await client.sMembers('set1'); // returns an array
    

Storing data -

Here we are using json placeholder populate our database.

import axios from "axios";
import { createClient } from "redis";
import 'dotenv/config';


const uploadData = async () => {
    const client = createClient({
        password: process.env.PASSWORD,
        socket: {
            host: process.env.HOST,
            port: 11240
        }
    });

    const response = await axios.get('https://jsonplaceholder.typicode.com/users');

    await client.connect();
    response.data.forEach(async (post)=>{
        await client.set(`${post.username}`, `${post.email}`)
    })

    return response.data;
}

uploadData();

RedisInsight -

It's a great tool to visualize our database and offers a lot of features. Try to explore them on your own.

More advanced blogs will be coming upon this topic soon..


Follow up -

If you have any questions, you can comment below. Will try to come up with more interesting things ๐Ÿ˜„

ย