Skip to main content

Redis

 What is Redis?

Rdis is a database, most specifically it is NoSql database. Definitely not similar to other NoSql databases like Mongos and NoSql. Note that redis don't have idea of tables and documents. All data stored in key values pairs in JSON format. Other Databases run in disk but redis run/ work in RAM thats why it is incredibly fast. It run on top of other traditional databases. Fastly to get data from it.

This storage is not persistance storage mean dta lose if system crash or turn off, therefore is is use for caching. 

Before start redis service in redis

Before start redis service set Linux Kernal overcommit memory to 1 by adding vm.overcommit_memory = 1 to /etc/sysctl.conf file.

Why we should do that: As we know redis is in memory data store. It primarily uses RAM to store data. Redis and other processes use RAM memory. We set vm.overcommit_memory = 1 to make sure Kernal not overcommit the memory by allocating memory to new program which required more memory than available memory.

In this scenario OOM (Out Of Memory) killer terminate the rest of processes.

We also restricting Kernal not to terminate the important services running and also stop  swapping redis and other importance service to device i/o device, where performance of redis effected. 

What is redis and how is use and purpose and how to use it all these things will be discuss later on first practice different data type in redis-cli

we all know redis store data in key value pair.

set command use to store data in redis

String

set name Adnan

"OK"

get name

"Adnan"

Update the existing value of key

127.0.0.1:6379> set name "Adnan Zaib"

OK

127.0.0.1:6379> get name

"Adnan Zaib"


Set multiple key values.

127.0.0.1:6379> mset profile laraveldeveloper company BrainTel

OK

127.0.0.1:6379> mget profile company

1) "laraveldeveloper"

2) "BrainTel"

get the length of key
127.0.0.1:6379> strlen profile
(integer) 16

We can also increment in integer key
127.0.0.1:6379> incr count
(integer) 2

127.0.0.1:6379> incrby count 10
(integer) 12

We can also decrement key by number
127.0.0.1:6379> decr count
(integer) 11

127.0.0.1:6379> decrby count 6
(integer) 5

We can set expiration time for any key value pair
We use TTL(Time to live) which define time, after that time the value will no longer exist in database.
set session adnan
expire session 20 // time in seconds
no check remaining expiry time.
ttl session // session is key name
after expire time this key return nil

we can do same in single command
127.0.0.1:6379> setex session 15 adnan
OK

List
we can create list to add multiple data in sigle list. A list can contain 2^23 -1 more than 4 billion elements.

Create list
lpush databases mysli
add another element to list
lpush databases sql
get all element in the list
lrange databases 0  -1
get range in list
lrange 0 10 // it will return 0 to 10 elements
get specific index element in list
lindex databases 2 // index 2 value will be return

put element data at botton of list
rpush databases redis
To remove elemnet from list
lpop databases //first element will remove

remove at the bottom of list
rpop databases

Update value of element in list
lset <listname> <index> <newvalue>
lset databases 0 newvalue

Add After/Before element in list
lpush databses before/after uk uae

check if key(list) exist before add element
lpushx movies doctor_strainge 
add element at bottom of list but defore check
rpush movies avengers

Sort list accending order

sort databases ALPHA

Sort list in descending order

sort databases desc ALPHA

BLPOP

The blpop block the execution till it has an value to remove.
It remove element from list if elements are available. Or it wait till expire time for new element to come in list, than remove it.
If there is no element in list than it return nil after timeout frame.

Set in redis.

Create a new set
sadd coding java
it will create set with name 'coding' and add key named 'java'

Add multiple keys at a time

sadd coding php  python c++

Retraive all keys in set.

smembers coding // it will display all set members(keys).

Comments

Popular posts from this blog

Install MariaDB Latest Version 11.4 in Red Hat Version 9

 This this post i will show you step by step the installation process of mariaDB in red hat version 9. Step1 Run the command to pull the latest updated packages on applications installed in your system. -dnf update If you get Kernal update than reboot the system -reboot Step2 Go to official mariaDB site Make mariadb repository in /etc/yum.repos.d Place the configuration in this file # MariaDB 11.4 RedHatEnterpriseLinux repository list - created 2024-09-24 11:12 UTC # https://mariadb.org/download/ [mariadb] name = MariaDB # rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details. # baseurl = https://rpm.mariadb.org/11.4/rhel/$releasever/$basearch baseurl = https://mirrors.aliyun.com/mariadb/yum/11.4/rhel/$releasever/$basearch # gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB gpgkey = https://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck = 1 Now install the mariaDB with its dependencies package...

Redis installation in Red Hat Linux

 In this post we will see step by step installation of Redis service in Redhat/CentOS operation system. To install Redis, run this command in terminal -dnf install redis This command will install the redis service in your machine. Verify installation -systemctl status redis -systemctl start redis -systemctl enable redis Note: Before start redis service set Linux Kernal overcommit memory to 1 by adding vm.overcommit_memory = 1 to /etc/sysctl.conf file. Why we should do that: As we know redis is in memory data store. It primarily uses RAM to store data. Redis and other processes use RAM memory. We set vm.overcommit_memory = 1 to make sure Kernal not overcommit the memory by allocating memory to new program which required more memory than available memory. In this scenario OOM (Out Of Memory) killer terminate the rest of processes. We also restricting Kernal not to terminate the important services running and also stop swapping redis and other importance service to device i/o device,...

Prepared statements, form submission, form validation and displaying errors in php

PHP Code. Insert statement Query. INSERT INTO `users`(`user_id`, `user_first`, `user_last`, `user_email`, `user_uid`, `user_pwd`) VALUES (1,'Adnan','Zaib','text@.com','Admin',1234) What are Prepared statements and how to use them //Created a sql prepare template $sql = "SELECT * FROM users WHERE user_uid=?;"; //than send to database with certain values left(unspecified parameters). //Create a prepared statement $stmt = mysqli_stmt_init($conn);   //prepare the prepared statement if(!mysqli_stmt_prepare($stmt, $sql)){ echo "sql statement failed"; }else{ //bind values to parameters and further to placeholder mysqli_stmt_bind_param(#stmt, "s",); // ‘s’ stands for datatype of parameter that you are going to pass. }   while($row = mysqli_fetch_assoc($result)){ echo $row['user_uid'] . "<br>"; } PHP form Validation   <?php // define variables and set to empty values $name = $email = $gender = $comment = ...