學(xué)會(huì)這15點(diǎn),讓你分分鐘拿下Redis數(shù)據(jù)庫(kù)
1、Redis簡(jiǎn)介
REmote DIctionary Server(Redis) 是一個(gè)由Salvatore Sanfilippo寫(xiě)的key-value存儲(chǔ)系統(tǒng)。Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、遵守BSD協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的API。它通常被稱(chēng)為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因?yàn)橹担╲alue)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等類(lèi)型。
大家都知道了redis是基于key-value的no sql數(shù)據(jù)庫(kù),因此,先來(lái)了解一下關(guān)于key相關(guān)的知識(shí)點(diǎn)。
(1)任何二進(jìn)制的序列都可以作為key使用
(2)Redis有統(tǒng)一的規(guī)則來(lái)設(shè)計(jì)key
(3)對(duì)key-value允許的最大長(zhǎng)度是512MB
2、支持的語(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
3、Redis的應(yīng)用場(chǎng)景到底有哪些??
(1)最常用的就是會(huì)話(huà)緩存
(2)消息隊(duì)列,比如支付
(3)活動(dòng)排行榜或計(jì)數(shù)
(4)發(fā)布、訂閱消息(消息通知)
(5)商品列表、評(píng)論列表等
4、Redis安裝
關(guān)于redis安裝與相關(guān)的知識(shí)點(diǎn)介紹請(qǐng)參考 Nosql數(shù)據(jù)庫(kù)服務(wù)之redis
安裝的大概步驟如下:
Redis是c語(yǔ)言開(kāi)發(fā)的,安裝redis需要c語(yǔ)言的編譯環(huán)境
如果沒(méi)有g(shù)cc需要在線(xiàn)安裝: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的安裝目錄
5、Redis數(shù)據(jù)類(lèi)型
Redis一共支持五種數(shù)據(jù)類(lèi)型
- string(字符串)
- hash(哈希)
- list(列表)
- set(集合)
- zset(sorted set 有序集合)
string(字符串)
它是redis最基本的數(shù)據(jù)類(lèi)型,一個(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類(lèi)型的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類(lèi)型的無(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類(lèi)型的有序集合,也不可重復(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)的所有元素
6、鍵值相關(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過(guò)期時(shí)間(單位秒)
persist #移除Key過(guò)期時(shí)間的配置
rename #重命名key
type #返回值的類(lèi)型
7、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
8、Redis的發(fā)布與訂閱
Redis發(fā)布與訂閱(pub/sub)是它的一種消息通信模式,一方發(fā)送信息,一方接收信息。
下圖是三個(gè)客戶(hù)端同時(shí)訂閱同一個(gè)頻道
下圖是有新信息發(fā)送給頻道1時(shí),就會(huì)將消息發(fā)送給訂閱它的三個(gè)客戶(hù)端
9、Redis事務(wù)
Redis事務(wù)可以一次執(zhí)行多條命令
(1)發(fā)送exec命令前放入隊(duì)列緩存,結(jié)束事務(wù)
(2)收到exec命令后執(zhí)行事務(wù)操作,如果某一命令執(zhí)行失敗,其它命令仍可繼續(xù)執(zhí)行
(3)一個(gè)事務(wù)執(zhí)行的過(guò)程中,其它客戶(hù)端提交的請(qǐng)求不會(huì)被插入到事務(wù)執(zhí)行的命令列表中
一個(gè)事務(wù)經(jīng)歷三個(gè)階段
開(kāi)始事務(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
10、Redis安全配置
可以通過(guò)修改配置文件設(shè)備密碼參數(shù)來(lái)提高安全性
#requirepass foobared
去掉注釋#號(hào)就可以配置密碼
沒(méi)有配置密碼的情況下查詢(xún)?nèi)缦?nbsp;
- 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"
11、Redis持久化
Redis持久有兩種方式:Snapshotting(快照),Append-only file(AOF)
Snapshotting(快照)
(1)將存儲(chǔ)在內(nèi)存的數(shù)據(jù)以快照的方式寫(xiě)入二進(jìn)制文件中,如默認(rèn)dump.rdb中
(2)save 900 1
#900秒內(nèi)如果超過(guò)1個(gè)Key被修改,則啟動(dòng)快照保存
(3)save 300 10
#300秒內(nèi)如果超過(guò)10個(gè)Key被修改,則啟動(dòng)快照保存
(4)save 60 10000
#60秒內(nèi)如果超過(guò)10000個(gè)Key被修改,則啟動(dòng)快照保存
Append-only file(AOF)
(1)使用AOF持久時(shí),服務(wù)會(huì)將每個(gè)收到的寫(xiě)命令通過(guò)write函數(shù)追加到文件中(appendonly.aof)
(2)AOF持久化存儲(chǔ)方式參數(shù)說(shuō)明
appendonly yes
#開(kāi)啟AOF持久化存儲(chǔ)方式
appendfsync always
#收到寫(xiě)命令后就立即寫(xiě)入磁盤(pán),效率最差,效果最好
appendfsync everysec
#每秒寫(xiě)入磁盤(pán)一次,效率與效果居中
appendfsync no
#完全依賴(lài)OS,效率最佳,效果沒(méi)法保證
12、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
13、Redis的備份與恢復(fù)
Redis自動(dòng)備份有兩種方式
第一種是通過(guò)dump.rdb文件實(shí)現(xiàn)備份
第二種使用aof文件實(shí)現(xiàn)自動(dòng)備份
dump.rdb備份
Redis服務(wù)默認(rèn)的自動(dòng)文件備份方式(AOF沒(méi)有開(kāi)啟的情況下),在服務(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)程來(lái)進(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ì)介紹過(guò)。
AOF文件備份,是備份所有的歷史記錄以及執(zhí)行過(guò)的命令,和mysql binlog很相似,在恢復(fù)時(shí)就是重新執(zhí)次一次之前執(zhí)行的命令,需要注意的就是在恢復(fù)之前,和數(shù)據(jù)庫(kù)恢復(fù)一樣需要手工刪除執(zhí)行過(guò)的del或誤操作的命令。
AOF與dump備份不同
1、aof文件備份與dump文件備份不同
2、服務(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,但是沒(méi)有aof文件,這個(gè)時(shí)候啟動(dòng)的數(shù)據(jù)庫(kù)會(huì)是空的
14、Redis 生產(chǎn)優(yōu)化介紹
(1)內(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ì)采用緊湊格式來(lái)存儲(chǔ),相對(duì)內(nèi)存開(kāi)銷(xiāo)也較小
在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)核允許分配超過(guò)所有物理內(nèi)存和交換空間總和的內(nèi)存
Redis官方的說(shuō)明是,建議將vm.overcommit_memory的值修改為1,可以用下面幾種方式進(jìn)行修改:
(1)編輯/etc/sysctl.conf 改vm.overcommit_memory=1,然后sysctl -p 使配置文件生效
(2)sysctl vm.overcommit_memory=1
(3)echo 1 > /proc/sys/vm/overcommit_memory
(2)內(nèi)存預(yù)分配
(3)持久化機(jī)制
定時(shí)快照:效率不高,會(huì)丟失數(shù)據(jù)
AOF:保持?jǐn)?shù)據(jù)完整性(一個(gè)實(shí)例的數(shù)量不要太大2G最大)
優(yōu)化總結(jié)
1)根據(jù)業(yè)務(wù)需要選擇合適的數(shù)據(jù)類(lèi)型
2)當(dāng)業(yè)務(wù)場(chǎng)景不需持久化時(shí)就關(guān)閉所有持久化方式(采用ssd磁盤(pán)來(lái)提升效率)
3)不要使用虛擬內(nèi)存的方式,每秒實(shí)時(shí)寫(xiě)入AOF
4)不要讓REDIS所在的服務(wù)器物理內(nèi)存使用超過(guò)內(nèi)存總量的3/5
5)要使用maxmemory
6)大數(shù)據(jù)量按業(yè)務(wù)分開(kāi)使用多個(gè)redis實(shí)例
15、Redis集群應(yīng)用
集群是將多個(gè)redis實(shí)例集中在一起,實(shí)現(xiàn)同一業(yè)務(wù)需求,或者實(shí)現(xiàn)高可用與負(fù)載均衡
到底有哪些集群方案呢??
(1)haproxy+keepalived+redis集群
1)通過(guò)redis的配置文件,實(shí)現(xiàn)主從復(fù)制、讀寫(xiě)分離
2)通過(guò)haproxy的配置,實(shí)現(xiàn)負(fù)載均衡,當(dāng)從故障時(shí)也會(huì)及時(shí)從集群中T除
3)利用keepalived來(lái)實(shí)現(xiàn)負(fù)載的高可用
(2)redis官方Sentinel集群管理工具
Redis集群生產(chǎn)環(huán)境高可用方案實(shí)戰(zhàn)過(guò)程
1)sentinel負(fù)責(zé)對(duì)集群中的主從服務(wù)監(jiān)控、提醒和自動(dòng)故障轉(zhuǎn)移
2)redis集群負(fù)責(zé)對(duì)外提供服務(wù)
關(guān)于redis sentinel cluster集群配置可參考
(3)Redis Cluster
Redis Cluster是Redis的分布式解決方案,在Redis 3.0版本正式推出的,有效解決了Redis分布式方面的需求。當(dāng)遇到單機(jī)內(nèi)存、并發(fā)、流量等瓶頸時(shí),可以采用Cluster架構(gòu)達(dá)到負(fù)載均衡的目的。
1)官方推薦,毋庸置疑。
2)去中心化,集群最大可增加1000個(gè)節(jié)點(diǎn),性能隨節(jié)點(diǎn)增加而線(xiàn)性擴(kuò)展。
3)管理方便,后續(xù)可自行增加或摘除節(jié)點(diǎn),移動(dòng)分槽等等。
4)簡(jiǎn)單,易上手。