Skip to content

Instantly share code, notes, and snippets.

@karolosk
Last active May 25, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karolosk/5242490ca4b54ad44d3d6e72686d369f to your computer and use it in GitHub Desktop.
Save karolosk/5242490ca4b54ad44d3d6e72686d369f to your computer and use it in GitHub Desktop.

Redis CLI

Basic usage of the redis-cli for the supported data structures.

Basics

Start the cli in terminal:

redis-cli

Set and get key value pairs

127.0.0.1:6379> SET a 1
OK
127.0.0.1:6379> GET a
"1"

Increase or decrease a numeric value of a key

127.0.0.1:6379> INCR a
(integer) 2
127.0.0.1:6379> DECR a
(integer) 1

Check if a key exists

127.0.0.1:6379> EXISTS a
(integer) 1
127.0.0.1:6379> EXISTS b
(integer) 0

Remove a key valye pair

127.0.0.1:6379> DEL a
(integer) 1

Clear memory

127.0.0.1:6379> FLUSHALL
OK

Set TTL for keys

We can set expiration time to key values to not keep the values as permanent and also check how much time we have till the expiration (ttl will return -2 if expiration has passed and -1 if there is not timer set-persisted)

127.0.0.1:6379> TTL food
(integer) -1
127.0.0.1:6379> EXPIRE food 30
(integer) 1
127.0.0.1:6379> TTL food
(integer) 24
127.0.0.1:6379> SETEX foo 10 bar
OK

Renaming keys

127.0.0.1:6379> SET food pizza
OK
127.0.0.1:6379> RENAME food foods
OK
127.0.0.1:6379> GET food
(nil)
127.0.0.1:6379> GET foods
"pizza"

Retrieving kyes

KEYS

Iterates through a set of keys that match a specific pattern Not viable for production environments because it returns everything at once

127.0.0.1:6379> KEYS *
1) "foods"

SCAN

Iterates through a set of keys based on the index and a counter (offset).

127.0.0.1:6379> SCAN 0 COUNT 4
1) "5"
2) 1) "e"
   2) "b"
   3) "a"
   4) "foods"
127.0.0.1:6379> SCAN 5 COUNT 4
1) "0"
2) 1) "f"
   2) "c"
   3) "d"

Getting info for Redis

INFO

Can be used stand alone or by getting any specified parameter:

server | clients | memory | persistence | stats | replication | cpu | commandstats | cluster | all | default

127.0.0.1:6379> INFO clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

Strings

Setting and getting string keys

Single

127.0.0.1:6379> SET "super pizza" "panepistimiou 32"
OK
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32"

Multiple

127.0.0.1:6379> MSET "best burger" "lampraki 3" "second best burger" "athinwn 12" "sushi" "somewhere 12"
OK
127.0.0.1:6379> MGET "best burger" "sushi" "whatever"
1) "lampraki 3"
2) "somewhere 12"
3) (nil)

Get length

127.0.0.1:6379> STRLEN "super pizza"
(integer) 16

Set keys if it does not exists

127.0.0.1:6379> SET "yummy pizza" "stadiou 2" NX
OK
127.0.0.1:6379> SET "yummy pizza" "stadiou 2" NX
(nil)
127.0.0.1:6379> SETNX "yummy pizza" "stadiou 2"
(integer) 0

Manipulating strings

APPEND

127.0.0.1:6379> APPEND "super pizza" " 10010"
(integer) 22
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32 10010"

OVERWRITING

127.0.0.1:6379> SETRANGE "super pizza" 16 " Athens "
(integer) 24
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32 Athens "

SUBSTRING

127.0.0.1:6379> GETRANGE "super pizza" 0 12
"panepistimiou"

Encoding

Redis uses three different encodings to store string objects and will decide the encoding automatically per string value:

  • int: For strings representing 64-bit signed integers
  • embstr: For strings whose length is less or equal to 44 bytes this type of encoding is more efficient in memory usage and performance
  • raw: For strings whose length is greater than 44 bytes
127.0.0.1:6379> SET myKey 12345
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"int"
127.0.0.1:6379> SET myKey "a string"
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"embstr"
127.0.0.1:6379> SET myKey "a long string whose length is more than 39 bytes"
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"raw"

Increase/Descreasing integer values

127.0.0.1:6379> SET a 10
OK
127.0.0.1:6379> GET a
"10"
127.0.0.1:6379> INCRBY a 30
(integer) 40
127.0.0.1:6379> INCR a
(integer) 41
127.0.0.1:6379> DECR a
(integer) 40
127.0.0.1:6379> DECRBY a 35
(integer) 5

Lists

Insert items to a list (head or tail)

127.0.0.1:6379> LPUSH fav_foods pizza burger fruits
(integer) 3
127.0.0.1:6379> RPUSH fav_foods spaghetti steak potatoes
(integer) 6

Get list items

Notice the order of the items based on the previous insertion commands.

127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "fruits"
2) "burger"
3) "pizza"
4) "spaghetti"
5) "steak"
6) "potatoes"

Insert item before or after specific element

127.0.0.1:6379> LINSERT fav_foods BEFORE burger salad
(integer) 7
127.0.0.1:6379> LINSERT fav_foods AFTER burger "ice cream"
(integer) 8
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "fruits"
2) "salad"
3) "burger"
4) "ice cream"
5) "pizza"
6) "spaghetti"
7) "steak"
8) "potatoes"

Retrieve by index

127.0.0.1:6379> LINDEX fav_foods 0
"fruits"
127.0.0.1:6379> LINDEX fav_foods -2
"steak

Inserting items to existing list

127.0.0.1:6379> LPUSHX ad "value"
(integer) 0
127.0.0.1:6379> LPUSHX fav_foods lasanga
(integer) 9

Removing list items

LPOP/RPOP

Removes the first element from the left or right end of the list, and returns its value. Executing LPOP or RPOP on a non_existent key will return (nil)

127.0.0.1:6379> LPOP fav_foods
"lasanga"
127.0.0.1:6379> LPOP fav_foods2
(nil)

LTRIM

Used to remove multiple elements in a list while only keeping the range of the list specified by start and end indexes

127.0.0.1:6379> LTRIM fav_foods 3 -1
OK
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "ice cream"
2) "pizza"
3) "spaghetti"
4) "steak"
5) "potatoes"

Renaming/Replacing value in a list

127.0.0.1:6379> LSET fav_foods 0 salad
OK
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "salad"
2) "pizza"
3) "spaghetti"
4) "steak"
5) "potatoes"

Hash

Setting/Getting a hash

Single value

127.0.0.1:6379> HSET some_pizza address "some random address 122"
(integer) 1
127.0.0.1:6379> HGET some_pizza address
"some random address 122"
127.0.0.1:6379> HGET some_pizza menu
(nil

Multiple key values

127.0.0.1:6379> HMSET "dream pizza" "address" "stadiou 34" "phone" "2107777777" "rating" "4.5"
OK
127.0.0.1:6379> HMGET "dream pizza" "address" "phone" "rating"
1) "stadiou 34"
2) "2107777777"
3) "4.5"

Get all fields

127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "4.5"

Update an existing key

127.0.0.1:6379> HSET "dream pizza" rating 3.2
(integer) 0
127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "3.2"

Setting a key only if not exists to not update it

127.0.0.1:6379> HSETNX "dream pizza" "test5" "test3"
(integer) 1
127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "3.2"
7) "test5"
8) "test3"
127.0.0.1:6379> HSETNX "dream pizza" "test5" "test3"
(integer) 0

Set

Setting items in a set

127.0.0.1:6379> SADD "pepperoni pizza" "spicy" "affordable" "great taste"
(integer) 3

Check if something exists

127.0.0.1:6379> SISMEMBER "pepperoni pizza" "family"
(integer) 0
127.0.0.1:6379> SISMEMBER "pepperoni pizza" "great taste"
(integer) 1

Remove items from set

127.0.0.1:6379> SREM "pepperoni pizza" "affordable" "spicy"
(integer) 2

Check legnth of set

127.0.0.1:6379> SCARD "pepperoni pizza"
(integer) 1

View set's members

127.0.0.1:6379> SADD "pepperoni pizza" "spicy" "affordable" "great taste"
(integer) 2
127.0.0.1:6379> SMEMBERS "pepperoni pizza"
1) "affordable"
2) "spicy"
3) "great taste"

Union, intersection and difference

Redis provides a group of commands for union (SUNION, SUNIONSTORE), intersection (SINTER, SINTERSTORE) and difference (SDIFF, SDIFFSTORE) operations between sets

127.0.0.1:6379> SADD numbers 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD numbers2 3 4 5 6 7
(integer) 5

Intersection

127.0.0.1:6379> SINTER numbers numbers2
1) "3"
2) "4"
3) "5"
127.0.0.1:6379> SINTERSTORE common numbers numbers2
(integer) 3
127.0.0.1:6379> SMEMBERS common
1) "3"
2) "4"
3) "5"

Union

127.0.0.1:6379> SUNION numbers numbers2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
127.0.0.1:6379> SUNIONSTORE all  numbers numbers2
(integer) 7
127.0.0.1:6379> SMEMBERS all
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"

Difference

127.0.0.1:6379> SDIFF numbers numbers2 # missing from second set
1) "1"
2) "2"
127.0.0.1:6379> SDIFFSTORE insecond  numbers numbers2
(integer) 2
127.0.0.1:6379> SMEMBERS insecond
1) "1"
2) "2"

Move items between sets

127.0.0.1:6379> SADD food tomato potato
(integer) 2
127.0.0.1:6379> SADD shop bread
(integer) 1
127.0.0.1:6379> SMOVE food shop tomato
(integer) 1
127.0.0.1:6379> SMEMBERS food
1) "potato"
127.0.0.1:6379> SMEMBERS shop
1) "bread"
2) "tomato"

Retrieve a random set item

127.0.0.1:6379> SRANDMEMBER shop
"bread"

Remove and return a random set member (or more if asked)

127.0.0.1:6379> SADD shop "towel" "meat" "fish"
(integer) 3
127.0.0.1:6379> SPOP shop
"meat"
127.0.0.1:6379> SPOP shop 2
1) "tomato"
2) "fish"

Sorted Sets

Sorted sets are similar to sets but every member has a “score”.

Score:

  • Required
  • Must be numeric (represented as float)
  • Is not unique

Setting items

127.0.0.1:6379> ZADD ranking:food 100 pizza 75 spagheti 99 burger 10 rice 55 tuna
(integer) 5

Get items

Sorted set members

127.0.0.1:6379> ZREVRANGE ranking:food 0 -1
1) "pizza"
2) "burger"
3) "spagheti"
4) "tuna"
5) "rice"

Sorted set members with scores

127.0.0.1:6379> ZREVRANGE ranking:food 0 -1 WITHSCORES
 1) "pizza"
 2) "100"
 3) "burger"
 4) "99"
 5) "spagheti"
 6) "75"
 7) "tuna"
 8) "55"
 9) "rice"
10) "10"

Increase/Descrease scores

127.0.0.1:6379> ZINCRBY ranking:food 1 "rice"
"11"
127.0.0.1:6379> ZINCRBY ranking:food -2 "rice"
"9"

Get ranking of a specific item

127.0.0.1:6379> ZREVRANK ranking:food pizza
(integer) 0
127.0.0.1:6379> ZREVRANK ranking:food burger
(integer) 1

Get score of a specific item

127.0.0.1:6379> ZSCORE ranking:food burger
"99"

Union

Different sorted sets

127.0.0.1:6379> ZADD ranking:drinks 100 beer 71 water 99 vodka  10 tequila 55 soda
(integer) 5
127.0.0.1:6379> ZADD ranking:food 100 pizza 75 spagheti 99 burger 10 rice 55 tuna
(integer) 5
127.0.0.1:6379> ZUNIONSTORE "total rankings" 2  ranking:drinks ranking:food
(integer) 10
127.0.0.1:6379> ZREVRANGE "total rankings" 0 -1 WITHSCORES
 1) "pizza"
 2) "100"
 3) "beer"
 4) "100"
 5) "vodka"
 6) "99"
 7) "burger"
 8) "99"
 9) "spagheti"
10) "75"
11) "water"
12) "71"
13) "tuna"
14) "55"
15) "soda"
16) "55"
17) "tequila"
18) "10"
19) "rice"
20) "9"

Same keys

127.0.0.1:6379> ZADD some 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD some2 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZUNIONSTORE "some3" 2  some some2
(integer) 3
127.0.0.1:6379> ZREVRANGE "some3" 0 -1 WITHSCORES
1) "c"
2) "6"
3) "b"
4) "4"
5) "a"
6) "2"

Intersection

127.0.0.1:6379> ZADD test 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD test2 1 a 2 d 3 e
(integer) 3
127.0.0.1:6379> ZINTERSTORE "some3" 2  test test2
(integer) 1
127.0.0.1:6379> ZREVRANGE "some3" 0 -1 WITHSCORES
1) "a"
2) "2"
127.0.0.1:6379> ZSCAN test 0
1) "0"
2) 1) "a"
   2) "1"
   3) "b"
   4) "2"
   5) "c"
   6) "3"

Geo

Adding coordinates for places

127.0.0.1:6379> GEOADD landmarks 37.970833  23.726110  acropolis 37.970833 23.72611 philopapou 37.6775239 23.937746 poseidon
(integer) 3

Get members data

127.0.0.1:6379> GEOPOS landmarks acropolis
1) 1) "37.97083407640457153"
   2) "23.7261091308044314"

Get distance between members

When the coordinates are set via GEOADD, they will be internally converted into a 52-bit GEOHASH, which is a Geo-encoding system that is commonly accepted. You should take into consideration that there is a slight difference between the Geo stored and the coordinates returned by GEOPOS, so you should not expect the two to be exactly the same.

127.0.0.1:6379> GEODIST landmarks philopapou poseidon
"38008.7042"

Retrieve members

The Geo set is actually stored as a sorted set (zset in Redis) and all the commands in a sorted set can be applied to the Geo data type. For example, removing a Geo index from a Geo set can be done by ZREM, and retrieving all the members of a Geo set can be achieved by ZRANGE.

127.0.0.1:6379> ZRANGE landmarks 0 -1
1) "acropolis"
2) "philopapou"
3) "poseidon"

HyperLogLog

Counting distinct values is a common task in various kinds of daily data processing scenarios. In Redis, while it's fine and sometimes necessary to implement distinct counting using set, memory consumption and performance degradation should be taken into consideration when the size of the set increases to tens of millions

Setting HyperLogLogs, merging them and getting distinc count

127.0.0.1:6379> PFADD "visit:super pizza:20200101" "1" "2" "3" "4"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200102" "1" "5" "6" "7"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200103" "5" "2" "12" "31"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200104" "1" "122" "321" "432"
(integer) 1
127.0.0.1:6379> PFMERGE "visit:super pizza:firstweek" "visit:super pizza:20200101" "visit:super pizza:20200102" "visit:super pizza:20200103" "visit:super pizza:20200104"
OK
127.0.0.1:6379> PFCOUNT "visit:super pizza:firstweek"
(integer) 12
127.0.0.1:6379>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment