學(xué)會(huì)這15點(diǎn),讓你分分鐘拿下Redis數(shù)據(jù)庫(kù)
REmote DIctionary Server(Redis) 是一個(gè)由 Salvatore Sanfilippo 寫的 Key-Value 存儲(chǔ)系統(tǒng)。
Redis 簡(jiǎn)介
Redis 是一個(gè)開源的使用 ANSI C 語(yǔ)言編寫、遵守 BSD 協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value 數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的 API。
它通常被稱為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因?yàn)橹担╲alue)可以是字符串(String),哈希(Map),列表(list),集合(sets) 和有序集合(sorted sets)等類型。
大家都知道 Redis 是基于 Key-Value 的 NoSQL 數(shù)據(jù)庫(kù),因此,先來了解一下關(guān)于 Key 相關(guān)的知識(shí)點(diǎn):
- 任何二進(jìn)制的序列都可以作為 key 使用
 - Redis 有統(tǒng)一的規(guī)則來設(shè)計(jì) key
 - 對(duì) Key-Value 允許的最大長(zhǎng)度是 512MB
 
支持的語(yǔ)言
- ActionScript Bash C C# C++ Clojure Common Lisp
 - Crystal D Dart Elixir emacs lisp Erlang
 - Fancy gawk GNU Prolog Go Haskell Haxe Io Java Javascript
 - Julia Lua Matlab mruby Nim Node.js Objective-C
 - OCaml Pascal Perl PHP Pure Data Python R Racket
 - Rebol Ruby Rust Scala Scheme Smalltalk Swift Tcl VB VCL
 
Redis 的應(yīng)用場(chǎng)景到底有哪些?
- 最常用的就是會(huì)話緩存
 - 消息隊(duì)列,比如支付
 - 活動(dòng)排行榜或計(jì)數(shù)
 - 發(fā)布、訂閱消息(消息通知)
 - 商品列表、評(píng)論列表等
 
Redis 安裝
關(guān)于 Redis 安裝與相關(guān)的知識(shí)點(diǎn)介紹請(qǐng)參考:Nosql數(shù)據(jù)庫(kù)服務(wù)之redis
安裝的大概步驟如下:
Redis 是 C 語(yǔ)言開發(fā)的,安裝 Redis 需要 C 語(yǔ)言的編譯環(huán)境,如果沒有 gcc 需要在線安裝:yum install gcc-c++
- 第一步,獲取源碼包:wget http://download.redis.io/releases/redis-3.0.0.tar.gz
 - 第二步,解壓縮 Redis:tar zxvf redis-3.0.0.tar.gz
 - 第三步,編譯。進(jìn)入redis源碼目錄(cd redis-3.0.0)。執(zhí)行 make
 - 第四步,安裝。make install PREFIX=/usr/local/redis
 
#PREFIX 參數(shù)指定 Redis 的安裝目錄
Redis 數(shù)據(jù)類型
Redis 一共支持五種數(shù)據(jù)類型:
- string(字符串)
 - hash(哈希)
 - list(列表)
 - set(集合)
 - zset(sorted set 有序集合)
 
String(字符串):它是 Redis 最基本的數(shù)據(jù)類型,一個(gè) Key 對(duì)應(yīng)一個(gè) Value,需要注意是一個(gè)鍵值最大存儲(chǔ) 512MB。
- 127.0.0.1:6379> set key "hello world"
 - OK
 - 127.0.0.1:6379> get key
 - "hello world"
 - 127.0.0.1:6379> getset key "nihao"
 - "hello world"
 - 127.0.0.1:6379> mset key1 "hi" key2 "nihao" key3 "hello"
 - OK
 - 127.0.0.1:6379> get key1
 - "hi"
 - 127.0.0.1:6379> get key2
 - "nihao"
 - 127.0.0.1:6379> get key3
 - "hello"
 
相關(guān)命令介紹:
- set 為一個(gè) Key 設(shè)置 Value(值)
 - get 獲得某個(gè) Key 對(duì)應(yīng)的 Value(值)
 - getset 為一個(gè) Key 設(shè)置 Value(值)并返回對(duì)應(yīng)的值
 - mset 為多個(gè) Key 設(shè)置 Value(值)
 
Hash(哈希):Redis Hash是一個(gè)鍵值對(duì)的集合, 是一個(gè) String 類型的 Field 和 Value 的映射表,適合用于存儲(chǔ)對(duì)象。
- 127.0.0.1:6379> hset redishash 1 "001"
 - (integer) 1
 - 127.0.0.1:6379> hget redishash 1
 - "001"
 - 127.0.0.1:6379> hmset redishash 1 "001" 2 "002"
 - OK
 - 127.0.0.1:6379> hget redishash 1
 - "001"
 - 127.0.0.1:6379> hget redishash 2
 - "002"
 - 127.0.0.1:6379> hmget redishash 1 2
 - 1) "001"
 - 2) "002"
 
相關(guān)命令介紹:
- hset 將 Key 對(duì)應(yīng)的 Hash 中的 Field 配置為 Value,如果 Hash 不存則自動(dòng)創(chuàng)建
 - hget 獲得某個(gè) Hash 中的 Field 配置的值
 - hmset 批量配置同一個(gè) Hash 中的多個(gè) Field 值
 - hmget 批量獲得同一個(gè) Hash 中的多個(gè) Field 值
 
List(列表):是 Redis 簡(jiǎn)單的字符串列表,它按插入順序排序。
- 127.0.0.1:6379> lpush word hi
 - (integer) 1
 - 127.0.0.1:6379> lpush word hello
 - (integer) 2
 - 127.0.0.1:6379> rpush word world
 - (integer) 3
 - 127.0.0.1:6379> lrange word 0 2
 - 1) "hello"
 - 2) "hi"
 - 3) "world"
 - 127.0.0.1:6379> llen word
 - (integer) 3
 
相關(guān)命令介紹:
- lpush 向指定的列表左側(cè)插入元素,返回插入后列表的長(zhǎng)度
 - rpush 向指定的列表右側(cè)插入元素,返回插入后列表的長(zhǎng)度
 - llen 返回指定列表的長(zhǎng)度
 - lrange 返回指定列表中指定范圍的元素值
 
Set(集合):是 String 類型的無(wú)序集合,也不可重復(fù)。
- 127.0.0.1:6379> sadd redis redisset
 - (integer) 1
 - 127.0.0.1:6379> sadd redis redisset1
 - (integer) 1
 - 127.0.0.1:6379> sadd redis redisset2
 - (integer) 1
 - 127.0.0.1:6379> smembers redis
 - 1) "redisset1"
 - 2) "redisset"
 - 3) "redisset2"
 - 127.0.0.1:6379> sadd redis redisset2
 - (integer) 0
 - 127.0.0.1:6379> smembers redis
 - 1) "redisset1"
 - 2) "redisset"
 - 3) "redisset2"
 - 127.0.0.1:6379> smembers redis
 - 1) "redisset1"
 - 2) "redisset3"
 - 3) "redisset"
 - 4) "redisset2"
 - 127.0.0.1:6379> srem redis redisset
 - (integer) 1
 - 127.0.0.1:6379> smembers redis
 - 1) "redisset1"
 - 2) "redisset3"
 - 3) "redisset2"
 
相關(guān)命令介紹:
- sadd 添加一個(gè) String 元素到 Key 對(duì)應(yīng)的 set 集合中,成功返回 1,如果元素存在返回 0
 - smembers 返回指定的集合中所有的元素
 - srem 刪除指定集合的某個(gè)元素
 
Zset(sorted set 有序集合):是 String 類型的有序集合,也不可重復(fù)。
sorted set 中的每個(gè)元素都需要指定一個(gè)分?jǐn)?shù),根據(jù)分?jǐn)?shù)對(duì)元素進(jìn)行升序排序,如果多個(gè)元素有相同的分?jǐn)?shù),則以字典序進(jìn)行升序排序,sorted set 因此非常適合實(shí)現(xiàn)排名。
- 127.0.0.1:6379> zadd nosql 0 001
 - (integer) 1
 - 127.0.0.1:6379> zadd nosql 0 002
 - (integer) 1
 - 127.0.0.1:6379> zadd nosql 0 003
 - (integer) 1
 - 127.0.0.1:6379> zcount nosql 0 0
 - (integer) 3
 - 127.0.0.1:6379> zcount nosql 0 3
 - (integer) 3
 - 127.0.0.1:6379> zrem nosql 002
 - (integer) 1
 - 127.0.0.1:6379> zcount nosql 0 3
 - (integer) 2
 - 127.0.0.1:6379> zscore nosql 003
 - "0"
 - 127.0.0.1:6379> zrangebyscore nosql 0 10
 - 1) "001"
 - 2) "003"
 - 127.0.0.1:6379> zadd nosql 1 003
 - (integer) 0
 - 127.0.0.1:6379> zadd nosql 1 004
 - (integer) 1
 - 127.0.0.1:6379> zrangebyscore nosql 0 10
 - 1) "001"
 - 2) "003"
 - 3) "004"
 - 127.0.0.1:6379> zadd nosql 3 005
 - (integer) 1
 - 127.0.0.1:6379> zadd nosql 2 006
 - (integer) 1
 - 127.0.0.1:6379> zrangebyscore nosql 0 10
 - 1) "001"
 - 2) "003"
 - 3) "004"
 - 4) "006"
 - 5) "005"
 
相關(guān)命令介紹:
- zadd 向指定的 sorteset 中添加 1 個(gè)或多個(gè)元素
 - zrem 從指定的 sorteset 中刪除 1 個(gè)或多個(gè)元素
 - zcount 查看指定的 sorteset 中指定分?jǐn)?shù)范圍內(nèi)的元素?cái)?shù)量
 - zscore 查看指定的 sorteset 中指定分?jǐn)?shù)的元素
 - zrangebyscore 查看指定的 sorteset 中指定分?jǐn)?shù)范圍內(nèi)的所有元素
 
鍵值相關(guān)的命令
- 127.0.0.1:6379> exists key
 - (integer) 1
 - 127.0.0.1:6379> exists key1
 - (integer) 1
 - 127.0.0.1:6379> exists key100
 - (integer) 0
 - 127.0.0.1:6379> get key
 - "nihao,hello"
 - 127.0.0.1:6379> get key1
 - "hi"
 - 127.0.0.1:6379> del key1
 - (integer) 1
 - 127.0.0.1:6379> get key1
 - (nil)
 - 127.0.0.1:6379> rename key key0
 - OK
 - 127.0.0.1:6379> get key
 - (nil)
 - 127.0.0.1:6379> get key0
 - "nihao,hello"
 - 127.0.0.1:6379> type key0
 - string
 
- exists:#確認(rèn) Key 是否存在
 - del:#刪除 Key
 - expire:#設(shè)置 Key 過期時(shí)間(單位秒)
 - persist:#移除 Key 過期時(shí)間的配置
 - rename:#重命名 Key
 - type:#返回值的類型
 
Redis 服務(wù)相關(guān)的命令
- 127.0.0.1:6379> select 0
 - OK
 - 127.0.0.1:6379> info
 - # Server
 - redis_version:3.0.6
 - redis_git_sha1:00000000
 - redis_git_dirty:0
 - redis_build_id:347e3eeef5029f3
 - redis_mode:standalone
 - os:Linux 3.10.0-693.el7.x86_64 x86_64
 - arch_bits:64
 - multiplexing_api:epoll
 - gcc_version:4.8.5
 - process_id:31197
 - run_id:8b6ec6ad5035f5df0b94454e199511084ac6fb12
 - tcp_port:6379
 - uptime_in_seconds:8514
 - uptime_in_days:0
 - hz:10
 - lru_clock:14015928
 - config_file:/usr/local/redis/redis.conf
 - -------------------省略N行
 - 127.0.0.1:6379> CONFIG GET 0
 - (empty list or set)
 - 127.0.0.1:6379> CONFIG GET 15
 - (empty list or set)
 
- slect:#選擇數(shù)據(jù)庫(kù)(數(shù)據(jù)庫(kù)編號(hào) 0-15)
 - quit:#退出連接
 - info:#獲得服務(wù)的信息與統(tǒng)計(jì)
 - monitor:#實(shí)時(shí)監(jiān)控
 - config get:#獲得服務(wù)配置
 - flushdb:#刪除當(dāng)前選擇的數(shù)據(jù)庫(kù)中的 Key
 - flushall:#刪除所有數(shù)據(jù)庫(kù)中的 Key
 
Redis 的發(fā)布與訂閱
Redis 發(fā)布與訂閱(pub/sub)是它的一種消息通信模式,一方發(fā)送信息,一方接收信息。
下圖是三個(gè)客戶端同時(shí)訂閱同一個(gè)頻道:
下圖是有新信息發(fā)送給頻道 1 時(shí),就會(huì)將消息發(fā)送給訂閱它的三個(gè)客戶端:
Redis 事務(wù)
Redis 事務(wù)可以一次執(zhí)行多條命令:
- 發(fā)送 exec 命令前放入隊(duì)列緩存,結(jié)束事務(wù)
 - 收到 exec 命令后執(zhí)行事務(wù)操作,如果某一命令執(zhí)行失敗,其它命令仍可繼續(xù)執(zhí)行
 - 一個(gè)事務(wù)執(zhí)行的過程中,其它客戶端提交的請(qǐng)求不會(huì)被插入到事務(wù)執(zhí)行的命令列表中
 
一個(gè)事務(wù)經(jīng)歷三個(gè)階段:
- 開始事務(wù)(命令:multi)
 - 命令執(zhí)行
 - 結(jié)束事務(wù)(命令:exec)
 
- 127.0.0.1:6379> MULTI
 - OK
 - 127.0.0.1:6379> set key key1
 - QUEUED
 - 127.0.0.1:6379> get key
 - QUEUED
 - 127.0.0.1:6379> rename key key001
 - QUEUED
 - 127.0.0.1:6379> exec
 - 1) OK
 - 2) "key1"
 - 3) OK
 
Redis 安全配置
可以通過修改配置文件設(shè)備密碼參數(shù)來提高安全性:#requirepass foobared。
去掉注釋#號(hào)就可以配置密碼,沒有配置密碼的情況下查詢?nèi)缦拢?/p>
- 127.0.0.1:6379> CONFIG GET requirepass
 - 1) "requirepass"
 - 2) ""
 
配置密碼之后,就需要進(jìn)行認(rèn)證:
- 127.0.0.1:6379> CONFIG GET requirepass
 - (error) NOAUTH Authentication required.
 - 127.0.0.1:6379> AUTH foobared #認(rèn)證
 - OK
 - 127.0.0.1:6379> CONFIG GET requirepass
 - 1) "requirepass"
 - 2) "foobared"
 
Redis 持久化
Redis 持久有兩種方式:
- Snapshotting(快照)
 - Append-only file(AOF)
 
Snapshotting(快照):
- 將存儲(chǔ)在內(nèi)存的數(shù)據(jù)以快照的方式寫入二進(jìn)制文件中,如默認(rèn) dump.rdb中。
 - save 900 1,#900 秒內(nèi)如果超過 1 個(gè) Key 被修改,則啟動(dòng)快照保存。
 - save 300 10,#300 秒內(nèi)如果超過 10 個(gè) Key 被修改,則啟動(dòng)快照保存。
 - save 60 10000,#60 秒內(nèi)如果超過 10000 個(gè) Key 被修改,則啟動(dòng)快照保存。
 
Append-only file(AOF):
- 使用 AOF 持久時(shí),服務(wù)會(huì)將每個(gè)收到的寫命令通過 write 函數(shù)追加到文件中(appendonly.aof)
 - AOF 持久化存儲(chǔ)方式參數(shù)說明:
 
appendonly yes,#開啟 AOF 持久化存儲(chǔ)方式
appendfsync always,#收到寫命令后就立即寫入磁盤,效率最差,效果最好
appendfsync everysec,#每秒寫入磁盤一次,效率與效果居中
appendfsync no,#完全依賴 OS,效率最佳,效果沒法保證
Redis 性能測(cè)試
自帶相關(guān)測(cè)試工具:
- [root@test ~]# redis-benchmark --help
 - Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]
 - -h <hostname> Server hostname (default 127.0.0.1)
 - -p <port> Server port (default 6379)
 - -s <socket> Server socket (overrides host and port)
 - -a <password> Password for Redis Auth
 - -c <clients> Number of parallel connections (default 50)
 - -n <requests> Total number of requests (default 100000)
 - -d <size> Data size of SET/GET value in bytes (default 2)
 - -dbnum <db> SELECT the specified db number (default 0)
 - -k <boolean> 1=keep alive 0=reconnect (default 1)
 - -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD
 - Using this option the benchmark will expand the string __rand_int__
 - inside an argument with a 12 digits number in the specified range
 - from 0 to keyspacelen-1. The substitution changes every time a command
 - is executed. Default tests use this to hit random keys in the
 - specified range.
 - -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).
 - -q Quiet. Just show query/sec values
 - --csv Output in CSV format
 - -l Loop. Run the tests forever
 - -t <tests> Only run the comma separated list of tests. The test
 - names are the same as the ones produced as output.
 - -I Idle mode. Just open N idle connections and wait.
 - Examples:
 - Run the benchmark with the default configuration against 127.0.0.1:6379:
 - $ redis-benchmark
 - Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
 - $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20
 - Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
 - $ redis-benchmark -t set -n 1000000 -r 100000000
 - Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
 - $ redis-benchmark -t ping,set,get -n 100000 --csv
 - Benchmark a specific command line:
 - $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0
 - Fill a list with 10000 random elements:
 - $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__
 - On user specified command lines __rand_int__ is replaced with a random integer
 - with a range of values selected by the -r option.
 
實(shí)際測(cè)試同時(shí)執(zhí)行 100 萬(wàn)的請(qǐng)求:
- [root@test ~]# redis-benchmark -n 1000000 -q
 - PING_INLINE: 152578.58 requests per second
 - PING_BULK: 150308.14 requests per second
 - SET: 143266.47 requests per second
 - GET: 148632.58 requests per second
 - INCR: 145857.64 requests per second
 - LPUSH: 143781.45 requests per second
 - LPOP: 147819.66 requests per second
 - SADD: 138350.86 requests per second
 - SPOP: 134282.27 requests per second
 - LPUSH (needed to benchmark LRANGE): 141302.81 requests per second
 - LRANGE_100 (first 100 elements): 146756.67 requests per second
 - LRANGE_300 (first 300 elements): 148104.27 requests per second
 - LRANGE_500 (first 450 elements): 152671.75 requests per second
 - LRANGE_600 (first 600 elements): 148104.27 requests per second
 - MSET (10 keys): 132731.62 requests per second
 
Redis 的備份與恢復(fù)
Redis 自動(dòng)備份有兩種方式:
- 通過 dump.rdb 文件實(shí)現(xiàn)備份
 - 使用 aof 文件實(shí)現(xiàn)自動(dòng)備份
 
dump.rdb 備份
Redis 服務(wù)默認(rèn)的自動(dòng)文件備份方式(AOF 沒有開啟的情況下),在服務(wù)啟動(dòng)時(shí),就會(huì)自動(dòng)從 dump.rdb 文件中去加載數(shù)據(jù)。
具體配置在 redis.conf:
- save 900 1
 - save 300 10
 - save 60 10000
 
也可以手工執(zhí)行 save 命令實(shí)現(xiàn)手動(dòng)備份。
- 127.0.0.1:6379> set name key
 - OK
 - 127.0.0.1:6379> SAVE
 - OK
 - 127.0.0.1:6379> set name key1
 - OK
 - 127.0.0.1:6379> BGSAVE
 - Background saving started
 
Redis 快照到 dump 文件時(shí),會(huì)自動(dòng)生成 dump.rdb 的文件。
- # The filename where to dump the DB
 - dbfilename dump.rdb
 - -rw-r--r-- 1 root root 253 Apr 17 20:17 dump.rdb
 
- SAVE 命令表示使用主進(jìn)程將當(dāng)前數(shù)據(jù)庫(kù)快照到 dump 文件
 - BGSAVE 命令表示,主進(jìn)程會(huì) fork 一個(gè)子進(jìn)程來進(jìn)行快照備份
 - 兩種備份不同之處,前者會(huì)阻塞主進(jìn)程,后者不會(huì)。
 
恢復(fù)舉例
- # Note that you must specify a directory here, not a file name.
 - dir /usr/local/redisdata/
 - 備份文件存儲(chǔ)路徑
 - 127.0.0.1:6379> CONFIG GET dir
 - 1) "dir"
 - 2) "/usr/local/redisdata"
 - 127.0.0.1:6379> set key 001
 - OK
 - 127.0.0.1:6379> set key1 002
 - OK
 - 127.0.0.1:6379> set key2 003
 - OK
 - 127.0.0.1:6379> save
 - OK
 
將備份文件備份到其他目錄:
- [root@test ~]# ll /usr/local/redisdata/
 - total 4
 - -rw-r--r-- 1 root root 49 Apr 17 21:24 dump.rdb
 - [root@test ~]# date
 - Tue Apr 17 21:25:38 CST 2018
 - [root@test ~]# cp ./dump.rdb /tmp/
 
刪除數(shù)據(jù):
- 127.0.0.1:6379> del key1
 - (integer) 1
 - 127.0.0.1:6379> get key1
 - (nil)
 
關(guān)閉服務(wù),將原備份文件拷貝回 save 備份目錄:
- [root@test ~]# redis-cli -a foobared shutdown
 - [root@test ~]# lsof -i :6379
 - [root@test ~]# cp /tmp/dump.rdb /usr/local/redisdata/
 - cp: overwrite ‘/usr/local/redisdata/dump.rdb’? y
 - [root@test ~]# redis-server /usr/local/redis/redis.conf &
 - [1] 31487
 
登錄查看數(shù)據(jù)是否恢復(fù):
- [root@test ~]# redis-cli -a foobared
 - 127.0.0.1:6379> mget key key1 key2
 - 1) "001"
 - 2) "002"
 - 3) "003"
 
AOF 自動(dòng)備份
Redis服務(wù)默認(rèn)是關(guān)閉此項(xiàng)配置:
- ###### APPEND ONLY MODE ##########
 - appendonly no
 - # The name of the append only file (default: "appendonly.aof")
 - appendfilename "appendonly.aof"
 - # appendfsync always
 - appendfsync everysec
 - # appendfsync no
 
配置文件的相關(guān)參數(shù),前面已經(jīng)詳細(xì)介紹過。AOF 文件備份,是備份所有的歷史記錄以及執(zhí)行過的命令,和 MySQL binlog 很相似,在恢復(fù)時(shí)就是重新執(zhí)次一次之前執(zhí)行的命令。
需要注意的就是在恢復(fù)之前,和數(shù)據(jù)庫(kù)恢復(fù)一樣需要手工刪除執(zhí)行過的del或誤操作的命令。
AOF 與 dump 備份不同:
- AOF 文件備份與 dump 文件備份不同
 - 服務(wù)讀取文件的優(yōu)先順序不同,會(huì)按照以下優(yōu)先級(jí)進(jìn)行啟動(dòng)
 
如果只配置 AOF,重啟時(shí)加載 AOF 文件恢復(fù)數(shù)據(jù)
如果同時(shí) 配置了 RBD 和 AOF,啟動(dòng)是只加載 AOF 文件恢復(fù)數(shù)據(jù)
如果只配置 RBD,啟動(dòng)時(shí)將加載 dump 文件恢復(fù)數(shù)據(jù)
注意:只要配置了 AOF,但是沒有 AOF 文件,這個(gè)時(shí)候啟動(dòng)的數(shù)據(jù)庫(kù)會(huì)是空的。
Redis 生產(chǎn)優(yōu)化介紹
內(nèi)存管理優(yōu)化
- hash-max-ziplist-entries 512
 - hash-max-ziplist-value 64
 - list-max-ziplist-entries 512
 - list-max-ziplist-value 64
 
#list 的成員與值都不太大的時(shí)候會(huì)采用緊湊格式來存儲(chǔ),相對(duì)內(nèi)存開銷也較小
在 Linux 環(huán)境運(yùn)行 Redis 時(shí),如果系統(tǒng)的內(nèi)存比較小,這個(gè)時(shí)候自動(dòng)備份會(huì)有可能失敗,需要修改系統(tǒng)的 vm.overcommit_memory 參數(shù),這個(gè)參數(shù)是 Linux 系統(tǒng)的內(nèi)存分配策略。
- 0 表示內(nèi)核將檢查是否有足夠的可用內(nèi)存供應(yīng)用進(jìn)程使用;如果有足夠的可用內(nèi)存,內(nèi)存申請(qǐng)?jiān)试S;否則,內(nèi)存申請(qǐng)失敗,并把錯(cuò)誤返回給應(yīng)用進(jìn)程。
 - 1 表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當(dāng)前的內(nèi)存狀態(tài)如何。
 - 2 表示內(nèi)核允許分配超過所有物理內(nèi)存和交換空間總和的內(nèi)存。
 
Redis官方的說明是,建議將 vm.overcommit_memory 的值修改為 1,可以用下面幾種方式進(jìn)行修改:
- 編輯 /etc/sysctl.conf 改 vm.overcommit_memory=1,然后 sysctl -p 使配置文件生效
 - sysctl vm.overcommit_memory=1
 - echo 1 > /proc/sysm/overcommit_memory
 
內(nèi)存預(yù)分配
持久化機(jī)制
- 定時(shí)快照:效率不高,會(huì)丟失數(shù)據(jù)
 - AOF:保持?jǐn)?shù)據(jù)完整性(一個(gè)實(shí)例的數(shù)量不要太大2G最大)
 
優(yōu)化總結(jié):
- 根據(jù)業(yè)務(wù)需要選擇合適的數(shù)據(jù)類型
 - 當(dāng)業(yè)務(wù)場(chǎng)景不需持久化時(shí)就關(guān)閉所有持久化方式(采用ssd磁盤來提升效率)
 - 不要使用虛擬內(nèi)存的方式,每秒實(shí)時(shí)寫入 AOF
 - 不要讓 Redis 所在的服務(wù)器物理內(nèi)存使用超過內(nèi)存總量的 3/5
 - 要使用 maxmemory
 - 大數(shù)據(jù)量按業(yè)務(wù)分開使用多個(gè) Redis 實(shí)例
 
Redis 集群應(yīng)用
集群是將多個(gè) Redis 實(shí)例集中在一起,實(shí)現(xiàn)同一業(yè)務(wù)需求,或者實(shí)現(xiàn)高可用與負(fù)載均衡到底有哪些集群方案呢?
Haproxy+Keepalived+Redis 集群
- 通過 Redis 的配置文件,實(shí)現(xiàn)主從復(fù)制、讀寫分離
 - 通過 Haproxy 的配置,實(shí)現(xiàn)負(fù)載均衡,當(dāng)從故障時(shí)也會(huì)及時(shí)從集群中T除
 - 利用 Keepalived 來實(shí)現(xiàn)負(fù)載的高可用
 
Redis 官方 Sentinel 集群管理工具
Redis集群生產(chǎn)環(huán)境高可用方案實(shí)戰(zhàn)過程:
- Sentinel 負(fù)責(zé)對(duì)集群中的主從服務(wù)監(jiān)控、提醒和自動(dòng)故障轉(zhuǎn)移
 - Redis 集群負(fù)責(zé)對(duì)外提供服務(wù)
 - 關(guān)于 Redis Sentinel Cluster 集群配置可參考
 
Redis Cluster
Redis Cluster 是 Redis 的分布式解決方案,在 Redis 3.0 版本正式推出的,有效解決了 Redis 分布式方面的需求。
當(dāng)遇到單機(jī)內(nèi)存、并發(fā)、流量等瓶頸時(shí),可以采用Cluster架構(gòu)達(dá)到負(fù)載均衡的目的。
- 官方推薦,毋庸置疑。
 - 去中心化,集群最大可增加 1000 個(gè)節(jié)點(diǎn),性能隨節(jié)點(diǎn)增加而線性擴(kuò)展。
 - 管理方便,后續(xù)可自行增加或摘除節(jié)點(diǎn),移動(dòng)分槽等等。
 - 簡(jiǎn)單,易上手。
 





















 
 
 










 
 
 
 