DEV Community

Ravi Bhuvan
Ravi Bhuvan

Posted on • Edited on

Redis

what is it?

  • is a REmote DIctionary Server.
  • it is a open-source in memory data store.
  • so, it like the cache stored memory, it is usually way too much faster than requesting querying the data.
  • usually when the query is made, first it finds in cache(redis memory) if not query is sent to DB, then the data is sent back to user while also saving it in the redis memory.
  • it is a structured key-value pair, but with various data structure.
redis port number : 6379
Enter fullscreen mode Exit fullscreen mode

installing redis

  • redis can be a standalone application, but usually in production we use it through the docker.

  • first u need to create a redis-stack image in docker

  • map the port to the computer port number so that we can use it on the computer.
    for executing the redis stack container we

docker exec -it <continer_ID> bash
Enter fullscreen mode Exit fullscreen mode
  • this gives you a bash inside the redis stack, now for redis cli u should use redis-cli ping this will return pong

  • instead we can simply use redis-cli and skip writting the redis-cli each time

Data types in redis

  1. Redis Strings
set name bhuv # setting the key-value pair
ok # output...
get name
"bhuv" # value which was saved 
Enter fullscreen mode Exit fullscreen mode

in redis DB it is not recommended that name as the key, we should always use the convention like mentioned below

set <entity>:<id> <key> <value>
set name:1 bhuv
Enter fullscreen mode Exit fullscreen mode

now these are grouped according to the name (for visualizing purpose)

for setting multiple values

mset name:1 bhu name:2 rav name:3 olaa name:3 bhoo
Enter fullscreen mode Exit fullscreen mode

for using redis in nodejs application

const {Redis} = require('ioredis'); // uses ioredis for connecting DB

const client = new Redis(); creates a redis instance 

module.exports = client; 
// exports it, so that it can be used in other modules.
Enter fullscreen mode Exit fullscreen mode

for creating and setting in node js

const client = require('./client') // import connected redis module

async function init() {
    await client.set("name:4", "boy"); // setter
    const res = await client.get("name:4"); // getter

    console.log('Result :', res);
}
init();
Enter fullscreen mode Exit fullscreen mode

in redis there is TTL

  • Time To Live sets an expiry time to the key in redis memory, this specifies how much time would it leave in the redis memory.
  • helps in freeing up used up memory, improve data retrival.
  • to set expiry expiry <key> <time in seconds>.
  1. Redis Lists
    • lists are similar to vectors in cpp, in redis we use lists for stacks and queues.
    • in this we push the data from left or right according to the command.
    • lpush and rpush pushes the data from left and right respectively.
    • lpop and rpop pops element from left and right respectively.
    • llen gives you the length of lists
    • blpop removes element from left, if empty waits for specified time to delete the element
blpop <list> <time_in_sec>
blpop msg 20
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vpospichal profile image
Vlastimil Pospíchal

Very weak article. Redis can do much more than just key-value. You can find all the usual collections, TTL and many other useful structures.

Collapse
 
algon31 profile image
Ravi Bhuvan

yeahh, i just posted it by mistake, i am still wrting this article