Miscellaneous Notes 
Miscellaneous notes for troubleshooting, gathering information about a redis/valkey setup, etc.

acool@acool-HP-EliteBook-8440p:~/redis-cluster$ date
Sat 06 Jul 2024 04:44:24 PM PDT
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli --version
redis-cli 5.0.7
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### Find out node's role
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7000 INFO|grep role
role:master
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### If node is master...
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7000 INFO|grep slave0
slave0:ip=127.0.0.1,port=7005,state=online,offset=7532,lag=0
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### Verify this node is slave...
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7005 INFO|grep role
role:slave
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### Find out node's master...
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7005 INFO | egrep "master_(host|port)"
master_host:127.0.0.1
master_port:7000
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### Find out node's mode...
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7003 INFO | egrep mode
redis_mode:cluster
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### List all cluster nodes
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7004 CLUSTER NODES
99f95692421cd04661c93ef9f09e058a7a80a6e4 127.0.0.1:7002@17002 master - 0 1720310325000 3 connected 10923-16383
8589237392750f2df8618bcda4e92e3996dda07a 127.0.0.1:7005@17005 slave 02c090059770ba140c14a1f26a16b3457fd58bf1 0 1720310324000 6 connected
c53fa6a32d9646c21549346716f01a5147cb8bb9 127.0.0.1:7001@17001 master - 0 1720310325468 2 connected 5461-10922
02c090059770ba140c14a1f26a16b3457fd58bf1 127.0.0.1:7000@17000 master - 0 1720310325000 1 connected 0-5460
ed206ea99f372d34dee37433b7747b5f2b2a3a40 127.0.0.1:7003@17003 slave c53fa6a32d9646c21549346716f01a5147cb8bb9 0 1720310325567 4 connected
05fe8b609e4b13d0510ed9b32b27b84502898595 127.0.0.1:7004@17004 myself,slave 99f95692421cd04661c93ef9f09e058a7a80a6e4 0 1720310323000 5 connected
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### This command seems to run INFO in all cluster nodes...
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli --cluster call 127.0.0.1:7000 INFO|grep role
role:master
role:master
role:slave
role:slave
role:slave
role:master
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ### monitor node
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7005 monitor
...

Find out data directory and config path:
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ ####### FIND PATH for rdb AND aof FILES!!!! #######
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli -c -p 7000 CONFIG GET dir
1) "dir"
2) "/home/acool/redis-cluster/7000"
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli --cluster call 127.0.0.1:7000 CONFIG GET dir
>>> Calling CONFIG GET dir
127.0.0.1:7000: dir
/home/acool/redis-cluster/7000
127.0.0.1:7001: dir
/home/acool/redis-cluster/7001
127.0.0.1:7004: dir
/home/acool/redis-cluster/7004
127.0.0.1:7005: dir
/home/acool/redis-cluster/7005
127.0.0.1:7003: dir
/home/acool/redis-cluster/7003
127.0.0.1:7002: dir
/home/acool/redis-cluster/7002
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ redis-cli --cluster call 127.0.0.1:7000 INFO | grep -i config_file
...


[ view entry ] ( 187 views )   |  print article
Valkey Cluster Demo 
[acool@localhost ~]$ date
Fri Jul 5 01:48:53 PM PDT 2024
[acool@localhost ~]$ cat /etc/redhat-release
Fedora release 38 (Thirty Eight)
[acool@localhost ~]$ sudo dnf install valkey
...
[acool@localhost ~]$ valkey-cli --version
valkey-cli 7.2.4
[acool@localhost ~]$
[acool@localhost ~]$ # just in case...
[acool@localhost ~]$ systemctl stop valkey.service
[acool@localhost ~]$ systemctl status valkey.service
...
[acool@localhost ~]$ which valkey-server
/usr/bin/valkey-server
[acool@localhost ~]$
[acool@localhost ~]$ mkdir valkey-cluster-demo
[acool@localhost ~]$ cd valkey-cluster-demo/
[acool@localhost valkey-cluster-demo]$ vim valkey.conf
...
[acool@localhost valkey-cluster-demo]$ cat valkey.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
[acool@localhost valkey-cluster-demo]$
[acool@localhost valkey-cluster-demo]$ mkdir cluster-test
[acool@localhost valkey-cluster-demo]$ cd cluster-test/
[acool@localhost cluster-test]$ mkdir -p {7000..7005}
[acool@localhost cluster-test]$ for i in {7000..7005}; do cp ../valkey.conf $i; done
[acool@localhost cluster-test]$ # open 7 tabs, update ports and start servers
[acool@localhost cluster-test]$ # TIP to update ports: for i in {0..5}; do sed -i 's/7000/700'"$i"'/g' "700$i/redis.conf" ; done
[acool@localhost cluster-test]$ cd 7000/
[acool@localhost 7000]$ /usr/bin/valkey-server ./valkey.conf
...
[acool@localhost valkey-cluster-demo] # create cluster
[acool@localhost valkey-cluster-demo]$ valkey-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
...
[acool@localhost valkey-cluster-demo]$ valkey-cli -p 7000 cluster nodes
384d585df521f8588a03b57a38e871549c0e58c4 127.0.0.1:7001@17001 master - 0 1720214911355 2 connected 5461-10922
5d2edcbba8f394a612df5253bd19a9399f97f4ff 127.0.0.1:7002@17002 master - 0 1720214912358 3 connected 10923-16383
4cf4150f20ae444422f0dabbc29b0a75878b1a2d 127.0.0.1:7004@17004 slave c88d6bea070f64023a2c7f27831bbbd113b2ff03 0 1720214912000 1 connected
e40a58e318f277ce2d420f7cb77480d5643c28c4 127.0.0.1:7005@17005 slave 384d585df521f8588a03b57a38e871549c0e58c4 0 1720214913362 2 connected
c88d6bea070f64023a2c7f27831bbbd113b2ff03 127.0.0.1:7000@17000 myself,master - 0 1720214912000 1 connected 0-5460
5b13cd8e34b47890e88fb48e30a99174ba19b751 127.0.0.1:7003@17003 slave 5d2edcbba8f394a612df5253bd19a9399f97f4ff 0 1720214911354 3 connected
[acool@localhost valkey-cluster-demo]$
[acool@localhost valkey-cluster-demo]$
[acool@localhost valkey-cluster-demo]$ # To connect to Valkey Cluster, you'll need a cluster-aware Valkey client...
[acool@localhost valkey-cluster-demo]$ # ...store some keys
[acool@localhost valkey-cluster-demo]$ valkey-cli -c -p 7000
127.0.0.1:7000> set firstname Angel
-> Redirected to slot [7108] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set lastname Cool
OK
127.0.0.1:7001>exit
[acool@localhost valkey-cluster-demo]$
[acool@localhost valkey-cluster-demo]$ # Retrive some keys
[acool@localhost valkey-cluster-demo]$ valkey-cli -c -p 7005
127.0.0.1:7005> get lastname
-> Redirected to slot [7726] located at 127.0.0.1:7001
"Cool"
127.0.0.1:7001> get firstname
"Angel"
127.0.0.1:7001>exit
[acool@localhost valkey-cluster-demo]$
[acool@localhost valkey-cluster-demo]$


TIP: Easy way to start all servers:
acool@acool-HP-EliteBook-8440p:~/redis-cluster$ for i in {0..5}; do  cd /home/acool/redis-cluster/700$i && /usr/bin/redis-server redis.conf --daemonize yes; done


Bonus: Pub/Sub demo

[acool@localhost valkey-cluster-demo]$ # TERMINAL 1
[acool@localhost valkey-cluster-demo]$ valkey-cli -c -p 7001
127.0.0.1:7001> SUBSCRIBE channel-a
...
[acool@localhost valkey-cluster-demo]$ # TERMINAL 2
[acool@localhost valkey-cluster-demo]$ valkey-cli -c -p 7002
127.0.0.1:7002> SUBSCRIBE channel-b
...

[acool@localhost valkey-cluster-demo]$ # TERMINAL 3
[acool@localhost valkey-cluster-demo]$ valkey-cli -c -p 7000
127.0.0.1:7000> PUBLISH channel-a "Hello channel a!"
(integer) 0
127.0.0.1:7000> PUBLISH channel-b "Hello channel b!"
(integer) 0
127.0.0.1:7000>
127.0.0.1:7000> exit
[acool@localhost valkey-cluster-demo]$ # Monitor output in TERMINAL 1 and 2


[ view entry ] ( 198 views )   |  print article
Redis 101 - My Humble Notes. 
Basic Commands:
[acool@localhost ~]$ redis-server --version
Redis server v=2.8.18 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=94164e7f0a3c47c9
[acool@localhost ~]$
[acool@localhost ~]$ sudo systemctl start redis
[acool@localhost ~]$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> INFO
127.0.0.1:6379>
127.0.0.1:6379> SET fullName "Angel Cool"
OK
127.0.0.1:6379> GET fullName
"Angel Cool"
127.0.0.1:6379> DEL fullName
(integer) 1
127.0.0.1:6379> GET fullName
(nil)
127.0.0.1:6379> INCR attempts
(integer) 1
127.0.0.1:6379> INCR attempts
(integer) 2
127.0.0.1:6379> GET attempts
"2"
127.0.0.1:6379> SETNX attempts 25 //set if not exist
(integer) 0
127.0.0.1:6379> GET attempts
"2"
127.0.0.1:6379> SET token abc123
OK
127.0.0.1:6379> EXPIRE token 60
(integer) 1
127.0.0.1:6379> TTL token
(integer) 51
127.0.0.1:6379> TTL token
(integer) 44
127.0.0.1:6379> SETEX token 120 321abc //set & expire but atomic ;)
OK
127.0.0.1:6379> TTL token
(integer) 116
127.0.0.1:6379> TTL token
(integer) 114
127.0.0.1:6379> keys * //get all keys in redis
1) "friends"
2) "attempts"
3) "TOKEN"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> type friends // get key type, possible types: string, list, set, zset, hash.
list
127.0.0.1:6379> type attempts
string


Lists:
127.0.0.1:6379> RPUSH friends Daniel  //push a new value at the end of a list
(integer) 1
127.0.0.1:6379> RPUSH friends William
(integer) 2
127.0.0.1:6379> RPUSH friends Alex
(integer) 3
127.0.0.1:6379> RPUSH friends Rene
(integer) 4
127.0.0.1:6379> LRANGE friends 0 -1 //get all entries in list
1) "Daniel"
2) "William"
3) "Alex"
4) "Rene"
127.0.0.1:6379> LPUSH friends Elizabeth //push a new value at the start of a list
(integer) 5
127.0.0.1:6379> LLEN friends //returns the current lenght of a list
(integer) 5
127.0.0.1:6379> LRANGE friends 2 3 //returns a subset of a list
1) "William"
2) "Alex"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> LPOP friends //removes the first element from the list and returns it
"Elizabeth"
127.0.0.1:6379> RPOP friends //removes the last element from the list and returns it
"Rene"
127.0.0.1:6379>


Sets:
127.0.0.1:6379> SADD countries Canada
(integer) 1
127.0.0.1:6379> SADD countries Chile
(integer) 1
127.0.0.1:6379> SADD countries Brazil
(integer) 1
127.0.0.1:6379> SADD countries Mexico
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS countries //output all members of a set
1) "Canada"
2) "Mexico"
3) "Brazil"
4) "Chile"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SISMEMBER countries Mexico //test if a value is in a set
(integer) 1
127.0.0.1:6379> SREM countries Mexico //removes a value from a set
(integer) 1
127.0.0.1:6379> SISMEMBER countries Mexico
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS cities
1) "London"
2) "Los Angeles"
3) "Merida"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SUNION cities countries //combines sets and returns all members
1) "Merida"
2) "London"
3) "Canada"
4) "Brazil"
5) "Los Angeles"
6) "Chile"


Sorted Sets:
127.0.0.1:6379> ZADD hackers 1940 "Alan Kay"
(integer) 1
127.0.0.1:6379> ZADD hackers 1906 "Grace Hopper"
(integer) 1
127.0.0.1:6379> ZADD hackers 1953 "Richard Stallman"
(integer) 1
127.0.0.1:6379> ZADD hackers 1965 "Yukihiro Matsumoyo"
(integer) 1
127.0.0.1:6379> ZADD hackers 1983 "Angel Cool"
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE hackers 2 3 WITHSCORES
1) "Richard Stallman"
2) "1953"
3) "Yukihiro Matsumoyo"
4) "1965"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZADD hackers 1980 "Karla Cool"
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE hackers 0 -1 WITHSCORES //all scores, see how it's sorted by score (date of birth)
1) "Grace Hopper"
2) "1906"
3) "Alan Kay"
4) "1940"
5) "Richard Stallman"
6) "1953"
7) "Yukihiro Matsumoyo"
8) "1965"
9) "Karla Cool"
10) "1980"
11) "Angel Cool"
12) "1983"
127.0.0.1:6379>
127.0.0.1:6379> ZREVRANGE hackers 0 -1 withscores // reverse score
1) "Angel Cool"
2) "1983"
3) "Yukihiro Matsumoyo"
4) "1965"
5) "Richard Stallman"
6) "1953"
7) "Alan Kay"
8) "1940"
9) "Grace Hopper"
10) "1906"
127.0.0.1:6379>
127.0.0.1:6379> ZCOUNT hackers -inf +inf // returns the number of items in zset
(integer) 5
127.0.0.1:6379>
127.0.0.1:6379> ZREMRANGEBYSCORE hackers 1940 1960
(integer) 2


Hashes
127.0.0.1:6379> HSET user:1000 firstName Angel
(integer) 1
127.0.0.1:6379> HSET user:1000 lastName Cool
(integer) 1
127.0.0.1:6379> HSET user:1000 website angelcool.net
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> HGET user:1000 firstName
"Angel"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> HMSET user:2000 firstName Richard lastName Stallman website stallman.org
OK
127.0.0.1:6379>
127.0.0.1:6379> HGET user:2000 website
"stallman.org"
127.0.0.1:6379>
127.0.0.1:6379> HGETALL user:2000
1) "firstName"
2) "Richard"
3) "lastName"
4) "Stallman"
5) "website"
6) "stallman.org"
127.0.0.1:6379>
127.0.0.1:6379>


[ view entry ] ( 1510 views )   |  print article

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |


2025 By Angel Cool