Redis Commands

Abhishek Amralkar
3 min readJul 23, 2023

--

This blog post is more about commands and the data structures supported by Redis. Though the list is not full but contains what I have used.

Strings

# set a key of type string

set key value
set name "bhishma"
set email "aaa@domain.com"

# get a key

get keyname
get name
get email

# get a range

getrange key start end
getrange email 0 4

# set multiple keys

mset key value key value
mset database redis type in-memory language python

# get multiple keys

mget key key
mget database type language

# get length of value

strlen key
strlen language

# update the value

set language "golang"

Integers and Floats

# set a key type of integer

set key value
set sum 1

# increment integer value

incr key
incr sum

# decrement integer value

decr key
decr sum

# increment by a certain number

incrby key increment
incrby sum 50

# decrement by a certain number

decrby key decrement
decrby sum 50

# set a key type of float

set key value
set iamfloat 1.1

# increment float value

incr key
incr iamfloat

# decrement float value

decr key
decr iamfloat

# increment by a certain number

incrbyfloat key increment
incrbyfloat iamfloat 50

# decrement by a certain number

decrby key decrement
decrby iamfloat 50

# to get all the keys

keys *

# delete all the keys

flushall

TTL and Expire

# set a expiration for key

set cpuload "23.46"

expire key time in seconds

expire cpuload 10

# check TTL

ttl key

ttl cpuload

List Datatype

# push the data to the list
# lpush will push the data to the left of the list

lpush key value

lpush databases redis

lpush databases mongodb

# get the range from the list
lrange key start end

lrange databases 0 -1

# push the data to the right of the list

rpush key value

rpush databases mysql

# length of the list

llen key

llen databases

# remove the data from the left of the list

lpop key

lpop databases

# remove the data from the right of the list

rpop key

rpop databases

# indert data before or after in the list

linsert key before pivot value

linsert databases before mongodb "cassandra"

linsert databases after mongodb "kafka"

# get value by index from the list

lindex key index

lindex databases 3

# check if the list exist and insert

lpushx databases postgresql

rpushx databases arora

# sort the list

sort languages ALPHA

Sets

# create set and add member to it

sadd key member

sadd editor emacs vscode vim vi

# get the members of the set

smembers key

smembers editor

# get the numbers of member in the set

scard key

scard editor

# check if member exit in set or not
# will return boolean 1 or 0

sismember key member

sismember editor emacs

# diff between 2 sets

sdiff key key

sdiff editor languages

# store the diff of the 2 sets into other set

sdiffstore destination key key

sdiffstore difference leditor languages

# intersection between 2 sets

sinter key key

sinter editor languages

# store the intersection of the 2 sets into other set

sinterstore intersection key key

sinterstore intersection leditor languages

# union between 2 sets

sunion key key

sunion languages editor

# store all union data

sunionstore union languages editor

Pub-Sub

# to subscribe a channel
subscribe channels

subscribe languages

subscribe languages databases

# to publish to a channel/s

publish channel

publish labguages

# pattern based subscription

psubscribe patter

psubscribe langua*

--

--