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
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
this gives you a bash inside the redis stack, now for redis cli u should use
redis-cli pingthis will returnponginstead we can simply use
redis-cliand skip writting the redis-cli each time
Data types in redis
- Redis Strings
set name bhuv # setting the key-value pair
ok # output...
get name
"bhuv" # value which was saved
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
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
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.
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();
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>.
- 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.
-
lpushandrpushpushes the data from left and right respectively. -
lpopandrpoppops element from left and right respectively. -
llengives you the length of lists -
blpopremoves element from left, if empty waits for specified time to delete the element
blpop <list> <time_in_sec>
blpop msg 20
Top comments (2)
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.
yeahh, i just posted it by mistake, i am still wrting this article