Sunday, July 1, 2018

Introduction to Redis and redis-cli


Redis is short for "Remote Dictionary Server"

Uses
----

- in-memory data structure store
- database
- cache
- message broker

Atomic Operations
-----------------

- appending to a string
- incrementing hash value
- pushing an element to a list
- computing set intersection
- union and difference
- getting the member with highest ranking in a sorted set



redis-cli
---------

- Can be run in command and interactive modes
- History is kept in ~/.rediscli_history
    * filename can be changed via  REDISCLI_HISTFILE environment variable
    * setting it to /dev/null will disable history

Command Mode
------------

Output
## human-readable if TTY is detected
redis-cli incr mycounter

## raw output
redis-cli incr mycounter > /tmp/output.txt

## forces raw output
redis-cli --raw incr mycounter

## CSV output (can't be run on whole DB)
redis-cli lpush mylist a b c d
redis-cli --csv lrange mylist 0 -1
Connecting
## connects 127.0.0.1 at 6379
redis-cli

## connects to another host/port
redis-cli -h redis15.localnet.org -p 6390 ping

## used for password protected instances
redis-cli -a myUnguessablePazzzzzword123 ping

## uses a different database number (default is 0)
redis-cli -n 4 incr a

## using a URI
redis-cli -u redis://myPass123@redishost.com:16379/0 ping
Writing

## adds a key value pair
redis-cli set A apple

## running same command overwrites previous value
redis-cli set A anatomy

## getting input from others
redis-cli -x set foo < /etc/services
Listing/
Displaying/
Querying
## prints server info
redis-cli info

## replication commands
redis-cli info replication
redis-cli role

## prints value of a key
redis-cli get food

## displays range of value only
redis-cli getrange food 0 50
Batch Mode
## commands are executed in order as typed in the file
$ cat /tmp/commands.txt
set foo 100
incr foo
append foo xxx
get foo
$ cat /tmp/commands.txt | redis-cli
OK
(integer) 101
(integer) 6
"101xxx"

## quotations can be used
$ cat /tmp/commands.txt
set foo "This is a single argument"
strlen foo
$ cat /tmp/commands.txt | redis-cli
OK
(integer) 25
Multiple
runs
## runs command 5 times with no delay in between
redis-cli -r 5 incr foo

## same command but with 1 second interval
redis-cli -r 5 -i 1 incr foo

## runs with 100 millisecond interval
redis-cli -r 100 -i 0.1 incr foo

## runs forever
redis-cli -r -1 incr foo
Running LUA
scripts
redis-cli --eval /tmp/script.lua foo , bar
Startup/Shutdown
## performs quit and save operation (similar to systemctl stop)
redis-cli shutdown

Interactive Mode
----------------

- REPL (Read Eval Print Loop)
- Advance capabilities:
  * simulate slave and receive streams from master
  * check latency of redis server
  * check statistics

Connection
## simple connection check
ping

## connecting to different instance
connect node3 6379

## selects DB number 2
select 2
Displaying
## prints db size
dbsize
Multiple
Runs
## running same command 5 times
5 get foo

Special Modes of Operation
--------------------------

Stat mode

$ redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
506        1015.00K 1       0       24 (+0)             7
506        1015.00K 1       0       25 (+1)             7
506        3.40M    51      0       60461 (+60436)      57
506        3.40M    51      0       146425 (+85964)     107
507        3.40M    51      0       233844 (+87419)     157
507        3.40M    51      0       321715 (+87871)     207
508        3.40M    51      0       408642 (+86927)     257
508        3.40M    51      0       497038 (+88396)     257

Scanning big keys

$ redis-cli --bigkeys

# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).

[00.00%] Biggest string found so far 'key-419' with 3 bytes
[05.14%] Biggest list   found so far 'mylist' with 100004 items
[35.77%] Biggest string found so far 'counter:__rand_int__' with 6 bytes
[73.91%] Biggest hash   found so far 'myobject' with 3 fields

-------- summary -------

Sampled 506 keys in the keyspace!
Total key length in bytes is 3452 (avg len 6.82)

Biggest string found 'counter:__rand_int__' has 6 bytes
Biggest   list found 'mylist' has 100004 items
Biggest   hash found 'myobject' has 3 fields

504 strings with 1403 bytes (99.60% of keys, avg size 2.78)
1 lists with 100004 items (00.20% of keys, avg size 100004.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
1 hashs with 3 fields (00.20% of keys, avg size 3.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)

Getting list of keys

redis-cli --scan | head -10
redis-cli --scan --pattern '*-11*'
redis-cli --scan --pattern 'user:*' | wc -l

PUB/SUB mode

$ redis-cli psubscribe '*'
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "*"
3) (integer) 1

Monitor mode

$ redis-cli monitor
OK
1460100081.165665 [0 127.0.0.1:51706] "set" "foo" "bar"
1460100083.053365 [0 127.0.0.1:51707] "get" "foo"

Checking latency

$ redis-cli --latency
min: 0, max: 1, avg: 0.19 (427 samples)

$ redis-cli --latency-history
min: 0, max: 1, avg: 0.14 (1314 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.18 (1299 samples) -- 15.00 seconds range
min: 0, max: 1, avg: 0.20 (113 samples)^C

Checking latency of the system running redis instance

$ ./redis-cli --intrinsic-latency 5
Max latency so far: 1 microseconds.
Max latency so far: 7 microseconds.
Max latency so far: 9 microseconds.
Max latency so far: 11 microseconds.
Max latency so far: 13 microseconds.
Max latency so far: 15 microseconds.
Max latency so far: 34 microseconds.
Max latency so far: 82 microseconds.
Max latency so far: 586 microseconds.
Max latency so far: 739 microseconds.

Backing up redis instance

$ redis-cli --rdb /tmp/dump.rdb
SYNC sent to master, writing 13256 bytes to '/tmp/dump.rdb'
Transfer finished with success.

Slave mode

$ redis-cli --slave
SYNC with master, discarding 13256 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"SELECT","0"
"set","foo","bar"
"PING"
"incr","mycounter"

No comments:

Post a Comment