redis
1. 安裝
1. 下載下傳
官網
redis.io
曆史下載下傳連結
https://download.redis.io/releases/
2. 編譯
采用
alpine
系統
# 更新系統
$ apk add update
$ apk add upgrade
# 安裝對應元件
$ apk add make gcc g++ gcc-c++ linux-headers tcl
# 下載下傳redis, 這裡采用4.09
$ wget https://download.redis.io/releases/redis-4.0.9.tar.gz
# 解壓
$ tar -xvf redis-4.0.9.tar.gz
# 編譯
$ cd redis-4.0.9
$ make
$ make test
# 安裝編譯好的
$ apk add redis=5.0.11-r0
# 開機自啟
$ rc-update add redis
2. 配置
1. Redis 可執行檔案說明
1. V5.0版本可執行檔案說明
可執行檔案 | 作用 |
---|---|
redis-benchmark | Redis 基準測試工具 |
redis-check-aof | Redis AOF 持久化檔案檢測和修複工具 |
redis-check-rdb | Redis RDB 持久化檔案檢測和修複工具 |
redis-cli | Redis 指令行用戶端 |
redis-sentinel | 啟動 Redis Sentinel |
redis-server | 啟動 Redis |
2. V3.0版本可執行檔案說明
redis-check-dump | |
2. 啟動 Redis
有三種方式啟動Redis
預設配置, 運作配置, 配置檔案啟動
1. 預設配置
這種方法會使用Redis的預設配置來啟動
$ redis-server
22933:C 22 Apr 2021 17:14:51.126 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22933:C 22 Apr 2021 17:14:51.127 # Redis version=5.0.11, bits=64, commit=a980b7f2, modified=0, pid=22933, just started
22933:C 22 Apr 2021 17:14:51.127 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
22933:M 22 Apr 2021 17:14:51.128 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.11 (a980b7f2/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 22933
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
22933:M 22 Apr 2021 17:14:51.128 # Server initialized
22933:M 22 Apr 2021 17:14:51.129 * Ready to accept connections
目前 Redis 版本是 5.0.11
Redis 預設端口是 6479
Warning 建議以配置檔案來啟動
2. 運作配置
# 使用其他端口啟動 Redis
$ redis-server --port 6380
如果有很多配置需要修改的話, 使用 配置檔案
更佳
3. 配置檔案啟動
Redis目錄下都會有一個redis.conf配置檔案
一般會在一台機器上面啟動多個redis, 并且配置集中管理在指定目錄下
4. Redis 設定
# 寫入參數
$ echo "vm.overcommit_memory = 1">> /etc/sysctl.conf
# 生效
$ sysctl -p
3. Redis 指令行用戶端
1. 互動式方式
通過 redis-cli -h{host} -p{prot}
的方式連接配接到Redis服務
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
2. 指令方式
redis-cli -h{host} -p{port} {command}
直接傳回指令結果
$ redis-cli -h 127.0.0.1 -p 6379 get hello
3. 停止Redis服務
# 停止不生成持久化檔案
$ redis-cli shutdown
28552:M 22 Apr 2021 18:47:03.306 # User requested shutdown...
28552:M 22 Apr 2021 18:47:03.306 * Saving the final RDB snapshot before exiting.
28552:M 22 Apr 2021 18:47:03.307 * DB saved on disk
28552:M 22 Apr 2021 18:47:03.307 # Redis is now ready to exit, bye bye...
# 停止, 生成持久化檔案
$ redis-cli shutdown {nosave|save}
- Redis關閉的過程: 斷開與用戶端的連接配接, 持久化檔案生成, 是一種相對優雅的關閉方式
- 除了通過
方式外, 也可以使用shutdown
方式關閉, 盡量不要使用kill
的方式關閉kill -9
- shudown還有一個參數, 代表是否在關閉前, 生成持久化檔案
4. Redis版本說明
非穩定版本: 2.7, 2.9, 3.1
穩定版本: 2.6, 2.8, 3.0, 3.2
3. 使用
1. 相關指令
Redis有5種資料結構, 它們是鍵值對中的值, 對于鍵來說有一些通用的指令
線上上環境中, 禁止使用檢視所有鍵
, 使用
keys
desize
1. 查詢所有鍵
$ redis-cli -h 127.0.0.1 -p 6379
# 插入字元串類型的鍵值對
$ set hello world
ok
$ set java jvm
ok
$ keys *
1) "hellow"
2) "java"
2. 查詢鍵總數
$ dbsize
(integer) 2
# 插入一個清單類型的鍵值對(值是多個元素組成)
$ rpush mylist a b c d e f g
(integer) 7
$ dbsize
(integer) 3
3. 檢測鍵是否存在
# exists key
$ exists hello
(integer) 1
$ exists aa
(integer) 0
4. 删除鍵
是一個通用的指令, 無論值是什麼資料結構類型,
del
del
指令都能删除
同時
指令可以删除多個鍵
del
# del key [key ...]
$ del hello
(integer) 1
5. 設定鍵過期
Redis 支援對鍵添加過期時間, 當超過過期時間後, 會自動删除鍵
# expire key time
$ set hello world
ok
$ expire hello 10
(integer) 1
6. 查詢鍵過期
ttl
指令會傳回鍵的剩餘過期時間, 它有三種傳回值
大于等于0的證書: 鍵剩餘的過期時間
-1: 鍵沒設定過期時間
-2: 鍵不存在
$ ttl hello
(integer) -2
# 傳回結果為-2, 說明鍵 hello 已經被删除
$ set hello world
OK
$ ttl hello
(integer) -1
# 傳回将結果為-1, 說明鍵 hello 沒有設定過期時間
$ expire hello 20
(integer) 1
$ ttl hello
(integer) 13
# 傳回大于等于0以上的數字, 說明鍵 hello 有設定過期時間,并且剩餘時間 13s
# 等待20s後查詢
$ ttl hello
(integer) -2
# 傳回結果為-2, 說明鍵 hello 已經被删除
7. 鍵的資料結構類型
type key
# 設定string類型的鍵值對
$ set a b
OK
# 結果與設定的一緻
$ type a
string
# 設定list類型的鍵值對
$ rpush mylist1 a b c d e f g
(integer) 7
# 結果與設定一緻
$ type mylist1
list
$ type aaaa
none
2. 資料結構和内部編碼
type
指令實際傳回的是目前鍵的資料結構類型, 分别有
string(字元串), hash(哈希), list(清單), set(集合), zset(有序集合)
查詢内部編碼
object encoding
$ object encoding java
"embstr"
$ object encoding mylist
"quicklist"
- 資料結構
- 内部編碼
結合了quicklist
和ziplist
兩者的優勢linkedlist
- list -> quicklist(快速清單)
3. 單線程架構
Redis使用了單線程架構和I/O多路複用模型來實作高性能記憶體資料庫服務
1. 引出單線程模型
Redis用戶端與服務端的模型可以簡化為如下所示
每次用戶端調用都經曆了發送指令, 執行指令, 傳回結果三個過程
-
用戶端與服務端請求過程Redis
- 所有指令在一個隊列裡排隊等待被執行
- 不存在多個指令被同時執行的情況
現在開啟了三個
redis-cli
用戶端同時執行指令
用戶端1設定一個字元串鍵值對
$ set hello world
用戶端2設定對counter做自增操作
$ incr counter
用戶端3對counter做自增操作
$ incr counter
2. 單線程解析
通常來講. 單線程要比多線程差, 但是在redis中, 有以下優勢
- 純記憶體通路
- 非阻塞I/O
- 單線程避免了線程切換和競态生産的消耗
4. 字元串
字元串類型是 Redis 最基礎的資料結構
4. 使用技巧
1. 慢查詢分析
用戶端指令的生命周期
- 發送指令
- 指令排隊
- 指令執行
- 傳回結果
慢查詢統計指令執行的時間
2. 慢查詢指令
有兩種方法, 配置檔案修改和慢查詢
showlog-log-slower-than=0 記錄所有指令
showlog-log-slower-than<0 不會記錄任何指令
slowlog-max-len 慢日志最多存儲多少條
1. 配置檔案
2. 指令行
# 在執行時間大于20000微秒時會被記錄
$ config set slowlog-log-slower-than 20000
# 1000條
$ config set slowlog-max-len 1000
$ config rewrite
3. 查詢慢日志
# slowlog get [n]
# 查詢所有慢日志
$ slowlog get
# id 發生時間戳 指令耗時 執行參數和指令
1) 1) (integer) 666
2) (integer) 1456786500
3) (integer) 11615
4) 1) "BGREWRITEAOF"
# 擷取慢日志清單的長度
# slowlog len
$ slowlog len
(integer) 45
# 重置慢日志
$ slowlog reset
5. 設定記憶體, 并發
1. 設定并發
1. 系統相關設定
# 設定最大打開檔案數
# Centos7
# 兩種方式
# 非systemd啟動
# 臨時生效
$ ulimit -n
# 永久生效
$ echo "redis soft nofile 10000
redis hard nofile 10000">> /etc/security/limits.conf
# systemd啟動
vim /usr/lib/systemd/system/redis.service
[Service]
LimitNOFILE=65536
# 重新開機daemon
$ systemctl daemon-reload
2. Redis 設定
2.6之後, 2.6之前需要注意
- 檢視連接配接數
# 檢視最大連接配接數
$ config get maxclients
1) "maxclients"
2) "10000"
# 檢視目前連接配接數
$ info clients
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
- 設定最大連接配接數
# 臨時生效最大連接配接數
# 目前設定為200
$ config set maxclients 200
# 永久設定, 重新開機生效
$ vim /etc/redis.conf
maxclients 10000
# 配合生效
# 先線上設定最大連結數, 然後在配置檔案中修改, 重新開機後依然生效
2. 設定記憶體
1. 查詢記憶體
$ info memory
# Memory
used_memory:818295
# 已經使用了的記憶體大小, 包括redis程序内部開銷和你的cache的資料所占用的記憶體, 機關byte
used_memory_human:799.12K
# 使用者資料所占用的記憶體, 就是你緩存的資料的大小
used_memory_rss:2449408
# 表示redis實體記憶體的大小, 即向OS申請了多少記憶體
used_memory_rss_human:2.34M
used_memory_peak:818295
# redis記憶體使用的峰值
used_memory_peak_human:799.12K
used_memory_peak_perc:100.00%
used_memory_overhead:817569
used_memory_startup:767579
used_memory_dataset:726
used_memory_dataset_perc:1.43%
allocator_allocated:785461
allocator_active:2411520
allocator_resident:2411520
total_system_memory:2093330432
total_system_memory_human:1.95G
used_memory_lua:37888
# redis lua記憶體使用的峰值
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:3.07
allocator_frag_bytes:1626059
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.02
# 記憶體碎片率
# >1 表示有碎片, 越大表明越多
# <1 表示使用虛拟記憶體
# 一般來說, 在1~1.5之間最好
rss_overhead_bytes:37888
mem_fragmentation_ratio:3.12
mem_fragmentation_bytes:1663947
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:libc
# 在編譯時指定的Redis使用的記憶體配置設定器
# libc、jemalloc、tcmalloc, 預設是jemalloc
active_defrag_running:0
lazyfree_pending_objects:0
# 機關大小bytes
# 臨時生效
$ config set maxmemory
# 永久生效(需要重新開機)
$ vim /etc/redis
# 設定1GB, 需要轉化為bytes=1073741824
maxmemory 1073741824