Redis 鏄竴涓唴瀛樺瀷銆屾暟鎹簱銆嶏紝闄ゅ瓨鍌ㄤ箣澶栵紝瀹冭繕鏈夎澶氬己澶х殑鍛戒护锛屼娇涔嬭繙杩滆秴鍑轰簡鏁版嵁搴撶殑瀹氫箟锛屾墍浠ュ畼鏂圭О涔嬩负銆宒ata structure store銆嶏紝鏁版嵁缁撴瀯瀛樺偍绯荤粺銆 閫氳繃 Redis 鎻愪緵鐨勬寚浠わ紝鎴戜滑鍙互瀹炵幇缂撳瓨銆佹秷鎭槦鍒椼佷簨浠堕氱煡銆佹帓琛屾銆佸簱瀛樼鐞嗐佸垎甯冨紡閿佺瓑鍔熻兘銆
Redis 鏍稿績鏄崟杩涚▼鍗曠嚎绋嬫湇鍔★紝閫氳繃 epoll銆乻elect 绛夊疄鐜颁簡 IO 澶氳矾澶嶇敤锛屽彲浠ュ苟鍙戝鐞嗙綉缁滀簨浠躲
Redis 鎻愪緵浜嗕互涓嬪嚑绉嶅吀鍨嬬殑鏁版嵁缁撴瀯
Redis 瀹炵幇浜嗗悕涓 SDS(Simple Dynamic String) 鐨勫瓧绗︿覆绫诲瀷锛屼笌 C 瀛楃涓插尯鍒細
瀹炵幇瀛楃涓叉嫾鎺ワ紝鍑忓皯鍐呭瓨閲嶅垎閰
缁存姢浜嗗瓧绗︿覆鐨勯暱搴︼紝浠ヤ究蹇熻幏鍙栧強閬垮厤缂撳啿鍖烘孩鍑
浜岃繘鍒跺畨鍏紝鍗虫敮鎸佸瓨鍌ㄧ┖鏍(\0)
Redis 瀹炵幇浜嗗弻鍚戞棤鐜摼琛紝骞朵娇鐢ㄦ鏁版嵁缁撴瀯瀹炵幇浜 list銆
Redis 瀹炵幇浜嗙鍚堣嚜韬娇鐢ㄥ満鏅殑 HashMap锛屽嵆鏁扮粍鍔犻摼琛ㄧ殑瀹炵幇銆傛鏁版嵁缁撴瀯瀹炵幇浜 Redis 涓殑 Hash銆丼et 鏁版嵁绫诲瀷銆傜壒鐐瑰涓嬶細
浣跨敤 MurmurHash3 Hash 绠楁硶锛岄拡瀵硅寰嬫у己鐨勫瓧绗︿覆鏈夋洿濂藉垎甯冩с
鏂拌妭鐐规彃鍏ュ埌琛ㄥご鑰岄潪琛ㄥ熬锛屽洜涓虹紦瀛樹竴瀹氱▼搴︿笂浼氬瓨鍦紝銆屽悗鍔犲叆鐨勭紦瀛樹細姣斿厛鍓嶅姞鍏ョ殑缂撳瓨鏇村鏄撹璁块棶銆嶇殑鐗圭偣銆
娓愯繘寮 rehash銆俁edis 鏁版嵁搴撴湰韬槸涓法澶х殑 Hash 琛紝姣忔 rehash 瑕佹搷浣滃嚑鐧句笂鍗冧竾鐨 key锛屾笎杩涘紡 rehash 鍒欐槸鍏朵腑蹇呬笉鍙皯鐨勪繚闅溿 rehash 鐨勬柟寮忔槸缁存姢涓ゅ紶琛ㄥ拰绱㈠紩锛岄渶瑕 rehash 鏃跺皢 rehashIndex 缃负 0锛岀劧鍚庢瘡娆¢櫎 insert 鎿嶄綔澶栵紝閮戒細灏 oldTable 鐨 rehashIndex 涓暟鎹浆绉诲埌 newTable 涓紝鐩村埌 rehashIndex == oldTable.length() – 1锛屽啀灏 rehashIndex 缃负 -1锛宺ehash 瀹屾垚銆
skiplist
璺宠穬琛ㄩ氳繃缁欓摼琛ㄥ垎灞傦紝瀹炵幇浜嗗钩鍧 O(logN),鏈鍧 O(N) 鐨勬椂闂村鏉傚害銆俁edis 浣跨敤璇ユ暟鎹粨鏋勫疄鐜颁簡 Sorted Set 鏁版嵁绫诲瀷銆傚彟澶 Sorted Set 涓繕闇瑕佷娇鐢 HashTable 鏉ュ疄鐜 O(1) 鐨勬煡璇€
鏁存暟闆嗗悎锛屽嵆鍙繚瀛樻暣鏁扮殑闆嗗悎銆俁edis 浣跨敤璇ユ暟鎹粨鏋勫疄鐜颁簡 Set銆
鍘嬬缉鍒楄〃銆傚帇缂╁垪琛ㄦ槸涓绉嶇壓鐗叉ц兘鑺傜害绌洪棿鐨勬暟鎹粨鏋勶紝鐩告瘮閾捐〃锛屽畠鑺傜害浜嗘寚閽堢殑绌洪棿锛孯edis 灏嗗畠浣滀负 List銆丠ash銆丼orted Set 鐨勫疄鐜帮紝骞朵娇鐢 hash-max-ziplist-entries(512)銆乭ash-max-ziplist-value(64)銆乴ist-max-ziplist-size(8 Kb)銆亃set-max-ziplist-entries(128)銆亃set-max-ziplist-value(64) 閰嶇疆鏉ュ喅瀹氭槸鍚︿娇鐢 ziplist銆
涓嶈鏄唴瀛樺瀷鐨勬暟鎹簱杩樻槸鍏崇郴鍨嬫暟鎹簱锛屽畷鏈恒佸仠鐢靛悗鏁版嵁鏃犳硶鎭㈠閮芥槸涓嶅彲鎺ュ彈鐨勩俁edis 鏈変袱绉嶅浠芥暟鎹殑鏂瑰紡锛
鍗 Append-Only-File锛屽綋寮鍚浠芥椂锛孯edis 浼氬垱寤哄嚭涓涓粯璁ゅ悕绉颁负 appendonly.aof 鐨勬枃浠躲傚苟灏嗗唴瀛樹腑鎵鏈夋暟鎹互鍛戒护鐨勫舰寮忓啓鍏ユ枃浠朵腑锛屽悗缁墽琛屾柊鐨勬搷浣滄暟鎹殑鍛戒护鏃讹紝浼氭斁鍏ョ紦鍐插尯涓畾鏃跺啓鍏ユ枃浠讹紙appendfsync 涓嶄负 always 鏃讹級銆 鍦 redis.conf 涓敤浠ヤ笅鍙傛暟閰嶇疆 AOF 绛栫暐锛
appendonly yes/no 鏄惁寮鍚 AOF 妯″紡
appendfilename appendonly.aof
appendfsync always/everysec/no #鍐欏叆纾佺洏鏃舵満锛宎lways 琛ㄧず姣忔閮戒細鍚屾鍒扮鐩橈紝鐢变簬鏄悓姝ユ搷浣滐紝鎬ц兘涓嬮檷涓ラ噸銆俥verysec 琛ㄧず姣忕鍒风洏銆俷o 琛ㄧず鍙斁鍏ョ紦瀛樺尯涓紝鐢辨搷浣滅郴缁熸寚瀹氬埛鐩樻椂鏈猴紙Linux 涓鑸槸 30 绉掞級
澶嶅埗浠g爜
褰撴垜鎵ц浜嗕互涓嬪懡浠ゆ椂锛
set liuzhiguo 123
set liuzhiguo abc
set liuzhiguo 456
set liuzhiguo 1231 ex 30
澶嶅埗浠g爜
AOF 鏂囦欢闀胯繖鏍凤細
*2 娑堟伅琛屾暟
$6 绗竴鏉℃秷鎭暱搴
SELECT 娑堟伅鍐呭
$1 绗簩鏉℃秷鎭暱搴
0 娑堟伅鍐呭
*3
$3
set
$9
liuzhiguo
$3
123
*3
$3
set
$9
liuzhiguo
$3
abc
*3
$3
set
$9
liuzhiguo
$3
456
*3
$3
set
$9
liuzhiguo
$4
1231
*3
$9
PEXPIREAT
$9
liuzhiguo
$13
1544420872751
澶嶅埗浠g爜
鍙互鐪嬪嚭 AOF 妯″紡鏄洿鎺ュ皢鍛戒护鍐欏叆鏂囦欢涓紝鎵浠ュ湪鎭㈠鏁版嵁鏃讹紝Redis 浼氶愭潯鎵ц鍛戒护鏉ユ仮澶嶆暟鎹傛墍浠 AOF 妯″紡鎭㈠鏁版嵁鐨勬晥鐜囧苟涓嶉珮锛岃屼笖褰撻噸澶嶅涓涓 key 杩涜鎿嶄綔鏃讹紝涔熼渶瑕佹墽琛屾墍鏈夋搷浣滃懡浠ゃ 閽堝鍚屼竴鏁版嵁閲嶅鎿嶄綔鐨勯棶棰橈紝Redis 鎻愪緵浜 AOF 閲嶅啓鐨勫姛鑳斤紝鍗充涪寮冨師鏈夌殑 appendonly.aof 鏂囦欢锛岄噸鏂板皢鍐呭瓨涓殑鏁版嵁浣滀负鍛戒护鍐欏叆鏂囦欢涓
鍗 Redis DataBase锛屾鎸佷箙鍖栨ā寮忛粯璁ゅ紑鍚 寮濮嬪浠芥椂锛孯edis 浼 fork 鍑轰竴涓瓙杩涚▼(bgsave)锛屽垱寤洪粯璁ゅ悕涓 dump.rdb 鐨勪簩杩涘埗鏂囦欢锛岄愪釜瀵瑰唴瀛樹腑鐨勬暟鎹繘琛屽浠姐傛瘡娆″浠芥椂閮戒細鎶涘純鍘熸湁鐨 RDB 鏂囦欢锛岄噸鏂板皢鏁版嵁鍏ㄩ噺澶囦唤銆 瀵逛簬澶囦唤鐨勬椂鏈猴紝鍦 redis.conf 鏈変互涓嬮夐」鏉ヨЕ鍙戝浠斤細
save 900 1 900 绉掑唴鏈 1 娆″彉鍔
save 300 10 300 绉掑唴鏈 10 娆″彉鍔
save 60 10000 60 绉掑唴鏈 10000 娆″彉鍔
澶嶅埗浠g爜
RDB 鐢变簬浣撶Н鍜屽ぉ鐒剁殑鎸囦护鍘嬬缉鑳藉姏锛屾仮澶嶆暟鎹熷害瑕佸ぇ澶у揩浜 AOF銆備絾鏄洜涓烘瘡娆″彧鑳藉叏閲忓浠斤紝璧勬簮娑堣楁瘮 AOF 澶э紝涓嶅 AOF 鐏垫椿銆傚苟涓斿洜涓哄浠芥椂鏈虹殑涓嶇‘瀹氭э紝鏁版嵁瀹屾暣涓嶅 AOF銆
Redis 鍦 4.0 涔嬪悗鎻愬嚭浜 RDB-AOF 娣峰悎妯″紡鎸佷箙鍖栵紝鍙互鍦 redis.conf 涓氳繃 aof-use-rdb-preamble 閫夐」寮鍚 姝ゆā寮忎笅锛屽叏閲忓浠姐侀噸鍐 AOF 鏃朵細浣跨敤 RDB 鏍煎紡锛岄殢鍚庢墽琛屽懡浠よ繕鏄互 AOF 鐨勬牸寮忚拷鍔犲埌鏂囦欢涓
杩欐牱涓鏉ワ紝鎭㈠鏁版嵁鏃舵ц兘姣斿崟绾 AOF 寮猴紝鍏ㄩ噺澶囦唤姣 AOF 蹇紝澶囦唤浣撶Н姣 AOF 灏忥紝閮ㄥ垎澶囦唤鎬ц兘姣 RDB 楂樸
楂樺彲鐢
Redis 閫氳繃鍝ㄥ叺锛圫entinel锛変笌澶嶅埗鐨勬柟寮忓疄鐜颁簡楂樺彲鐢
澶嶅埗
閫氳繃鍦 redis.conf 鏂囦欢涓厤缃宻laveof ip port銆嶆垨缁欒繍琛屼腑鐨 redis 鑺傜偣鎵ц鍛戒护銆宻laveof ip port銆嶏紝鍗冲彲浣垮緱璇ヨ妭鐐规垚涓烘煇涓 redis 瀹炰緥鐨勪粠鑺傜偣銆
浠庤妭鐐(slave)鍚姩鏃朵細鍚戜富鑺傜偣(master)鍙戦 sync 鎸囦护锛屼富鑺傜偣浣跨敤 bgsave 鏂规硶鐢熸垚 RDB 鏂囦欢锛屽苟寤虹珛缂撳啿鍖鸿褰曞啓鍛戒护銆俁DB 鏂囦欢鐢熸垚浼氬嵆鍙戦佺粰浠庤妭鐐癸紝浠庤妭鐐瑰紑濮嬭浇鍏 RDB 鏂囦欢锛屾鍔ㄤ綔鍚屾鎵ц銆 浠庤妭鐐瑰畬鎴愯浇鍏ュ悗锛屼富鏈嶅姟鍣ㄤ細灏嗙紦鍐插尯鐨勮褰曞彂閫佺粰浠庢湇鍔″櫒锛屾鍚庝富鑺傜偣姣忓綋鏈夋墽琛屽懡浠ゆ椂锛岄兘浼氫紶鎾粰浠庤妭鐐逛竴浠姐
鏂嚎閲嶈繛鍚庯紝浠庤妭鐐瑰啀娆′笂绾挎椂浼氬悜涓昏妭鐐瑰彂閫 psync 鍛戒护鎵ц閮ㄥ垎閲嶅悓姝ワ紝涓昏妭鐐逛細灏嗘鏈熼棿鐨勫懡浠ゅ彂閫佺粰浠庤妭鐐规墽琛屻備负瀹炵幇姝ゅ姛鑳斤紝涓讳粠鑺傜偣缁存姢浜嗐屽鍒跺亸绉婚噺銆嶃
浣跨敤 info 鍙互鏌ョ湅澶嶅埗鐨勭姸鎬侊細
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=280,lag=0 // 浠庤妭鐐逛俊鎭
master_replid:6088224db78515c7c2cbef387fb90cefd459f0d5
master_repl_offset:280 // 涓昏妭鐐瑰亸绉婚噺
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1 // 涓庝富鑺傜偣 1 绉掑墠鍚屾
master_sync_in_progress:0 // 鏄惁鍦ㄨ繘琛 sync 鍚屾
slave_repl_offset:280 // 浠庤妭鐐瑰亸绉婚噺
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6088224db78515c7c2cbef387fb90cefd459f0d5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
澶嶅埗浠g爜
Sentinel
涓哄疄鐜伴珮鍙敤锛屽彧鏈夊鍒舵槸涓嶅鐨勶紝杩橀渶瑕佷富鑺傜偣鏈嶅姟涓嶅彲鐢ㄥ悗锛屼粠鑺傜偣鑳借嚜鍔ㄨˉ浣嶃 Redis 閫氳繃 Sentinel 鏉ュ疄鐜拌妭鐐圭洃鎺т笌鍗忚皟锛孲entinel 鏄竴涓壒娈婄殑 Redis 鑺傜偣锛岄渶瑕佸惎鍔ㄦ椂鎸囧畾鍙傛暟 –sentinel 鍜 sentinel.conf 閰嶇疆鏂囦欢锛屽苟鍦ㄩ厤缃枃浠朵腑鎸囧畾涓昏妭鐐圭殑 ip銆乭ost銆 Sentinel 鍚姩鍚庝細鍚戜富鑺傜偣鍙戦 info 鍛戒护锛岃幏鍙栧埌鐩稿簲鐨勪粠鑺傜偣淇℃伅锛屽苟涓庝粠鑺傜偣寤虹珛杩炴帴銆 褰撲富鑺傜偣涓嶅搷搴旀椂锛孲entinel 浼氱瓑寰呰嚦閰嶇疆涓寚瀹氱殑 timeout 鏃堕棿锛岄殢鍚庡皢浠庤妭鐐规彁鍗囦负涓昏妭鐐广備富鑺傜偣鍐嶆鍚姩鏃讹紝Sentinel 浼氬悜涓昏妭鐐瑰彂閫 slaveof 鍛戒护锛岃姹傚叾鎴愪负浠庤妭鐐广
Sentinel 鏈韩鍚屾牱鏀寔楂樺彲鐢紝澶氫釜 Sentinel 浼氬悜姣忎釜涓讳粠鑺傜偣 publish 鑷繁鐨勪俊鎭紝浠ユ鏉ュ緱鐭ュ叾浠 Sentinel 鐨勫瓨鍦ㄥ苟寤虹珛杩炴帴銆傚涓 Sentinel 鍏卞瓨鏃讹紝瀵逛富浠庤妭鐐圭姸鎬併佽韩浠界殑鍏辫瘑浼氭湁鏇村鏉傜殑鍗忚皟杩囩▼锛岃繖灏辨槸鍙﹀涓涓极闀跨殑鏁呬簨浜嗐
瀵 Sentinel 鐨勮缁嗕粙缁嶏紝鍙互瑙侊細https://redis.io/topics/sentinel 锛屼互鍙婂弬鑰冦奟edis 璁捐涓庡疄鐜帮紙绗簩鐗堬級銆.
闆嗙兢
Redis 鍥犱负鏄唴瀛樺瀷鏁版嵁搴擄紝鍦ㄥ瓨鍌ㄧ┖闂翠笂瀹规槗鎹夎瑙佽倶锛屼簬鏄骇鐢熶簡璁稿鎵╁鏂规銆
瀹㈡埛绔垎鐗
濡 ShardedJedis锛岄氳繃鍦ㄥ鎴风瀵 key 杩涜 hash锛屽啀鍒嗙粰鎸囧畾鐨勮妭鐐广 浼樼偣锛氭棤闇鏀瑰姩 Redis 鍗冲彲鎵╁ 缂虹偣锛氬彧鑳芥墿瀹逛竴娆★紝鏃犳硶骞虫粦鍗囩骇
浠g悊灞傚垎鐗
濡 Twemproxy銆備唬鐞嗗眰鎺ユ敹瀹㈡埛绔殑璇锋眰锛屼唬鐞嗗埌瀵瑰簲鐨 Redis 鑺傜偣涓婏紝閫氬父涔熸槸浣跨敤涓鑷存 hash 鏉ュ垎鐗囥傚苟鐢变簬浠g悊灞傚彲浠ョ粺涓閰嶇疆鎴栬鍙栧悓涓鏁版嵁婧愶紝鍋氬埌鍙嫇灞曚唬鐞嗗眰銆 浼樼偣锛氬鎴风鏃犻渶鍏冲績 Redis 鏈嶅姟鐘舵侊紝涔熸棤闇鍒嗙墖銆 缂虹偣锛氶毦浠ユ墿瀹广
Redis Cluster
Redis 鑷繁瀹炵幇鐨勯泦缇わ紝鍙疄鐜版棤鐥涙墿瀹癸紝骞虫粦杩佺Щ銆傚惎鍔ㄩ泦缇ゆā寮忛渶瑕佸湪閰嶇疆鏂囦欢涓厤缃細
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 1500
澶嶅埗浠g爜
闆嗙兢妯″紡涓嬶紝浼氬垱寤哄嚭 16384 涓Ы锛屽苟缁欓泦缇や腑姣忎釜鑺傜偣鍒嗛厤鑷繁鐨勬Ы鏁帮紝妲藉繀椤昏鍏ㄩ儴鎸囧畾鎵嶈兘宸ヤ綔锛屼竴涓妭鐐规渶浣庢寚瀹氫竴涓Ы銆傛墍浠 Redis 闆嗙兢鐞嗚涓婃渶澶ф槸 16384 涓妭鐐广
褰撻渶瑕佹坊鍔/鑾峰彇鏌愪釜 key 鏃讹紝閫氳繃 crc16(key) & 16384 寰楀埌杩欎釜 key 搴斿湪鐨勬Ы锛岄殢鍚庢壘鍑鸿繖涓Ы鎵鍦ㄧ殑鑺傜偣锛屽鏋滆妭鐐规槸鑷繁鐩存帴鎵ц锛屽惁鍒欎細杩斿洖缁欏鎴风瀵瑰簲鐨勮妭鐐圭殑 ip + port銆
Redis 闆嗙兢鏄幓涓績鍖栫殑锛屽郊姝や箣闂寸姸鎬佸悓姝ラ潬 gossip 鍗忚閫氫俊锛岄泦缇ょ殑娑堟伅鏈変互涓嬪嚑绉嶇被鍨嬶細
Meet銆傞氳繃銆宑luster meet ip port銆嶅懡浠わ紝宸叉湁闆嗙兢鐨勮妭鐐逛細鍚戞柊鐨勮妭鐐瑰彂閫侀個璇凤紝鍔犲叆鐜版湁闆嗙兢銆
Ping銆傝妭鐐规瘡绉掍細鍚戦泦缇や腑鍏朵粬鑺傜偣鍙戦 ping 娑堟伅锛屾秷鎭腑甯︽湁鑷繁宸茬煡鐨勪袱涓妭鐐圭殑鍦板潃銆佹Ы銆佺姸鎬佷俊鎭佹渶鍚庝竴娆¢氫俊鏃堕棿绛夈
Pong銆傝妭鐐规敹鍒 ping 娑堟伅鍚庝細鍥炲 pong 娑堟伅锛屾秷鎭腑鍚屾牱甯︽湁鑷繁宸茬煡鐨勪袱涓妭鐐逛俊鎭
Fail銆傝妭鐐 ping 涓嶉氭煇鑺傜偣鍚庯紝浼氬悜闆嗙兢鎵鏈夎妭鐐瑰箍鎾鑺傜偣鎸傛帀鐨勬秷鎭傚叾浠栬妭鐐规敹鍒版秷鎭悗鏍囪宸蹭笅绾裤
鐢变簬鍘讳腑蹇冨寲鍜岄氫俊鏈哄埗锛孯edis Cluster 閫夋嫨浜嗘渶缁堜竴鑷存у拰鍩烘湰鍙敤銆備緥濡傚綋鍔犲叆鏂拌妭鐐规椂(meet)锛屽彧鏈夐個璇疯妭鐐瑰拰琚個璇疯妭鐐圭煡閬撹繖浠朵簨锛屽叾浣欒妭鐐硅绛夊緟 ping 娑堟伅涓灞備竴灞傛墿鏁c傞櫎浜 Fail 鏄珛鍗冲叏缃戦氱煡鐨勶紝鍏朵粬璇稿鏂拌妭鐐广佽妭鐐归噸涓婄嚎銆佷粠鑺傜偣閫変妇鎴愪负涓昏妭鐐广佹Ы鍙樺寲绛夛紝閮介渶瑕佺瓑寰呰閫氱煡鍒般
鍥犳锛岀敱浜 gossip 鍗忚锛孯edis Cluster 瀵规湇鍔″櫒鏃堕棿鐨勮姹傝緝楂橈紝鍚﹀垯鏃堕棿鎴充笉鍑嗙‘浼氬奖鍝嶈妭鐐瑰垽鏂秷鎭殑鏈夋晥鎬с傚彟澶栬妭鐐规暟閲忓澶氬悗鐨勭綉缁滃紑閿涔熶細瀵规湇鍔″櫒浜х敓鍘嬪姏銆傚洜姝ゅ畼鏂规帹鑽愭渶澶ц妭鐐规暟涓 1000銆傚浜 Redis 闆嗙兢鐨勮繍缁达紝鍙互鍙傝 浼橀叿钃濋哺杩戝崈鑺傜偣鐨 Redis 闆嗙兢杩愮淮缁忛獙鎬荤粨銆
浼樼偣锛
鐪熸鐨勫脊鎬ф墿瀹圭缉瀹广
鎵╁鏈熼棿涓嶅奖鍝嶄娇鐢ㄣ
缂虹偣锛
缂轰箯绠$悊骞冲彴銆
瀹㈡埛绔鍙﹀仛鍏煎銆
閮ㄥ垎鍛戒护涓嶆敮鎸
Redis 閫氳繃 Cluster 瑙e喅浜嗘墿瀹逛箣鍚庯紝瀹㈡埛绔鎬庝箞浣跨敤鍛紵 濡 JedisCluster锛屾瘡娆¤姹傚墠浼氭媺鍙栬妭鐐圭殑 cluster info 鏉ヨ绠楀簲璇ュ埌鍝釜鑺傜偣璇锋眰锛屽苟闇瑕佸閿欒鑺傜偣杩斿洖鐨 ASK 娑堟伅鍋氱浉搴旂殑澶勭悊銆傜敱姝や骇鐢熺殑闂鏄
姣忔鎿嶄綔鏈灏戣姹備袱娆°
姣忔濡傛灉鍙姹傛煇涓涓妭鐐癸紝涔熶細褰㈡垚鍗曠偣鍘嬪姏銆
瀵归棶棰 1锛岃В鍐冲姙娉曟槸瀹㈡埛绔紦瀛橀泦缇ょ姸鎬併傚闂 2锛孞edisCluster 鏀寔閰嶇疆澶氫釜鑺傜偣锛屾媺鍙栬妭鐐逛俊鎭椂浼氶殢鏈洪夋嫨鏌愯妭鐐逛互鍒嗘憡鍘嬪姏銆傚闂 2 鐨勫鐞嗘柟寮忥紝闇瑕佸皢 Redis 鑺傜偣淇℃伅鍚屾鍒板鎴风閰嶇疆涓紝浜х敓浜嗚﹀悎銆
鍙﹀鐨勯棶棰樻槸锛岄泦缇ょ姸鎬佷笅鏄笉鏀寔 mget銆乵set 绛夐渶瑕佽法鑺傜偣鎵ц鐨勫懡浠ゃ傝闂鐨勮В鍐虫柟妗堟槸鍔犱竴灞 Proxy锛屾帹鑽 浼橀叿鍦熻眴鐨凴edis鏈嶅姟骞冲彴鍖栦箣璺紝鍏朵娇鐢 Nginx + Redis Cluster 鐨勬濊矾浠や汉璧炲徆锛屽苟鐢ㄨ姹傝仛鍚堢殑鏂瑰紡瀹炵幇浜嗚法鑺傜偣鎵ц鍛戒护鐨勯棶棰樸
闃块噷浜戞彁渚涚殑 Redis 鏈嶅姟鍚屾牱瀹炵幇浜嗛泦缇ゆā寮忎笅鐨勮法鑺傜偣鍛戒护锛岄噰鐢ㄤ唬鐞 + 鍒嗙墖鏈嶅姟鍣 + 鍒嗙墖閰嶇疆鏈嶅姟鍣紙寰堝彲鑳芥槸 zookeeper锛夛紝浣嗘槸娌℃湁浣跨敤 Redis Cluster 鏈哄埗锛岃屾槸鑷繁瀹炵幇鐨勩屽垎鐗囥嶏紝淇濈暀浜 slot銆傞樋閲屼簯鐨 Redis 濂藉鏄泦缇ょ増鏃犻渶瀹㈡埛绔仛鍏煎锛屽彲浠ュ綋鎴愬崟鏈 Redis 浣跨敤锛屽嚭浜嗛棶棰樻柟渚跨敥閿呫
绗笁鏂归瓟鏀 Redis
鍦ㄧ瓑寰 Redis 鍑哄畼鏂归泦缇ゆ柟妗堜箣鍓嶏紝浜轰滑杩笉鍙婂緟鎯宠闆嗙兢鐗堢殑 Redis锛屼竴浜涗笉婊′簬鐜扮姸浠ュ強涓嶆弧浜 Redis Cluster 瀹炵幇鐨勪汉浠紑濮嬪 Redis 杩涜鏀归犮傚墠闈㈡彁鍒扮殑闃块噷浜 Redis 涔熷睘浜庨瓟鏀瑰悗鐨 Redis銆
Codis
Codis 鍑犱箮鏄渶鐭ュ悕鐨勭涓夋柟 Redis锛屽 Redis 杩涜浜嗗ぇ閲忔敼閫犮 鍏舵灦鏋勪负 zookeeper + proxy + server-group(master + slave)锛屽苟鎻愪緵浜嗘帶鍒跺彴浠ヤ究鍙鍖栬繍缁淬
閫氳繃 zookeeper 璁板綍鍙敤鐨 proxy 鑺傜偣锛屽啀浣跨敤 Codis 寮鍙戠粍鍩轰簬 Jedis 淇敼鐨 Jodis 瀹㈡埛绔埌 zookeeper 涓鎵惧彲鐢ㄧ殑 proxy 鑺傜偣杩涜璋冪敤銆傚鏋滀娇鐢ㄧ殑鏄 jedis 鎴栧叾浠栧鎴风锛屽垯鍙兘鍒拌繛鎺ヤ竴涓 proxy锛屾垨鑰呮兂鍔炴硶杩炴帴鍒 zookeeper 鑾峰彇鑺傜偣锛屽啀杩涜杞璋冪敤銆
Codis 鏀寔寮规ф墿瀹癸紝鍒嗙墖鏂瑰紡涓 Redis Cluster 绫讳技锛岄氳繃 crc32(key) % 1024 鍒嗘垚 1024 涓Ы锛屾瘡鍙板疄渚嬩繚瀛樺搴旀Ы鐨勬暟鎹
LedisDB 鍜 SSDB
LedisDB 鍜 SSDB 闈炲父鐩镐技锛岄兘鏄敤 LevelDB 搴曞眰锛岄噸鏂板疄鐜颁簡 Redis锛屾垨鑰呰鍙疄鐜颁簡 Redis 鍗忚銆傞氳繃澶氱嚎绋 + 纭洏鐨勬柟寮忥紝瀹炵幇浜嗗拰鍗曟満 Redis 鐩镐技鐨 QPS 鎬ц兘锛屽苟鍙互寰堝ぇ绋嬪害涓婂瀹归噺杩涜鎵╁銆 LedisDB/SSDB 涓 Redis 鐨勫叧绯伙紝鐩稿綋浜 TiDB 涓 MySQL 鐨勫叧绯汇
缂虹偣鏄嚭浜嗗閲忎笂鐨勬垚鏈紭鍔匡紝鍏朵粬娌℃湁浠讳綍浼樺娍銆
浜嬪姟
Redis 鎻愪緵 watch銆乵ulti銆乪xec 绛夋柟娉曞疄鐜颁箰瑙傞攣浜嬪姟銆備娇鐢ㄤ簨鍔$殑娴佺▼濡備笅锛
watch key1 key2
multi 寮鍚簨鍔
set key1 value1銆乻et key2 value2锛屽皢鎸囦护鍏ラ槦銆
exec锛屾墽琛屾寚浠ゃ
濡傛灉 multi ~ exec 涔嬮棿 key1/key2 琚叾浠栧鎴风淇敼杩囷紝exec 鏃朵細杩斿洖 nil锛 set key1 value1銆乻et key2 value2 鍧囦笉浼氭墽琛屻 Redis 浼氫繚瀛樹竴涓 watch_keys 瀛楀吀锛岀粨鏋勪负锛 client -> keys銆乮s_dirty銆俁edis 鍦ㄥ鐞嗘瘡涓涓細淇敼鏁版嵁鐨勫懡浠ゆ椂锛屼細妫鏌 watch_keys 鏄惁瀛樺湪璇 key锛屽鏋滄湁锛屽垯淇敼 is_dirty 涓 true銆
鎵ц浜嬪姟鐨勫鎴风鍦ㄦ墽琛 exec 鏃讹紝浼氭鏌 is_dirty 瀛楁锛屽鏋滃彂鐜颁负 false锛屾墍鏈夌殑绉疮鐨勬寚浠や細鐩存帴涓㈠純涓嶆墽琛屻
浜嬪姟鍦 Redis 涓殑浣跨敤鍦烘櫙涓嶅锛屽苟鍙戦噺澶х殑鎯呭喌涓嬮渶瑕佸弽澶嶉噸璇曪紝澶ч儴鍒嗘儏鍐典笅鏈夋洿濂界殑浣跨敤鏂瑰紡锛
Lua
Redis 鎻愪緵浜嗗 Lua 鑴氭湰鐨勬敮鎸侊紝鍘熷瓙鎬ф墽琛屼竴绯诲垪鎸囦护锛屽苟鍙互鍐欎唬鐮佸仛閫昏緫鍒ゆ柇銆 渚嬪闇瑕佸ぇ閲忔彃鍏ユ暟鎹殑鍦烘櫙锛
for i=1,10000000,1 do
local num = math.random(1000000,999999999);
redis.call(“set”,num,i)
end
澶嶅埗浠g爜
鎵ц涓鍗冧竾鏉″懡浠ゅ湪鏈満澶ф鐢ㄤ簡 12 绉掞紝QPS 83w銆 Redis 鍦ㄦ墽琛 Lua 鑴氭湰鏃舵槸鍗曠嚎绋嬶紝鏃犳硶澶勭悊鍏朵粬璇锋眰锛岃繖涔熸槸 Redis 鍘熷瓙鎬х殑鍘熷洜銆備笅闈㈡槸鎶㈢孩鍖呮椂鍒╃敤璇ョ壒鎬у疄鐜扮殑 Lua 鑴氭湰锛
// 璇ヨ剼鏈紶鍏 4 涓弬鏁
// KEYS[1] = 鏈鍙栫殑绾㈠寘鍒楄〃 key
// KEYS[2] = 宸查鍙栫殑绾㈠寘鍒楄〃 key
// KEYS[3] = 绾㈠寘宸查鍙栦汉ID鍒楄〃 key
// KEYS[4] = 棰嗗彇浜篒D
// 妫鏌ラ鍙栦汉鏄惁鍦ㄥ凡棰嗗彇鍒楄〃鍐
if redis.call(‘hexists’, KEYS[3], KEYS[4]) ~= 0 then
return nil
else
// 鍙栧嚭涓涓湭棰嗗彇鐨勭孩鍖
local redEnvelop = redis.call(‘rpop’, KEYS[1]);
if redEnvelop then
// 绾㈠寘涓殑 receiver 濉叆棰嗗彇浜 ID
local x = cjson.decode(redEnvelop);
x[‘receiver’] = KEYS[4];
local re = cjson.encode(x);
// 棰嗗彇浜烘斁鍏ュ凡棰嗗彇浜篒D鍒楄〃锛屽皢绾㈠寘鏀惧叆宸查鍙栫孩鍖呭垪琛
redis.call('hset', KEYS[3], KEYS[4], KEYS[4]);
redis.call('lpush', KEYS[2], re);
// 缁欑浉搴旂殑 key 缁湡
if redis.call('llen', KEYS[2]) == 1 then
redis.call('expire', KEYS[2], 172800);
end
if redis.call('hlen', KEYS[3]) == 1 then
redis.call('expire', KEYS[3], 172800);
end
return re;
end
end
return nil
澶嶅埗浠g爜
闇瑕佹敞鎰忕殑鏄紝鐢变簬 Lua 鑴氭湰鍙兘鍦ㄥ崟涓 Redis 瀹炰緥鎵ц锛屾墍浠ュ湪闆嗙兢鐘舵佷笅鎵ц Lua 鏃讹紝Redis 浼氬瑕佹墽琛岀殑 key 杩涜妫鏌ャ備负浜嗕繚璇佹墍鏈 key 涓瀹氬湪鏌愪竴鍙版満鍣ㄤ笂锛孯edis 闄愬埗浜嗘墍鏈 key 閮藉繀椤诲湪鍚屼竴涓 slot 鍐呮墠琛屻
鎵浠ラ拡瀵圭孩鍖呯殑鍦烘櫙锛屽 Lua 涓紶鍏ョ殑 key 鍋氫簡xxx{redpacketId}鐨勫鐞嗭紝浠ヤ繚璇佹墍鏈 key 钀藉湪涓涓 slot 涓娿
绠¢亾锛坧ipeline锛
Redis 鏀寔浣跨敤绠¢亾鎵归噺鎵ц鍛戒护锛屽啀缁熶竴杩斿洖锛屽噺灏戝線杩旀鏁帮紝閫氬父鐢ㄤ簬鎵归噺鎻掑叆鏁版嵁锛屾壒閲忚幏鍙栨暟鎹
瀹炴垬
缂撳瓨
缂撳瓨鏂瑰紡
缂撳瓨鏄 Redis 鏈甯歌鐨勫満鏅傞氬父缂撳瓨鐨勮繃绋嬩负锛
鏈懡涓細浠庢暟鎹簮涓彇寰楁暟鎹紝鏀惧叆缂撳瓨涓
鍛戒腑锛氳繑鍥炴暟鎹
鏇存柊锛氬厛鎶婃暟鎹瓨鍏ユ暟鎹簱锛屽啀浣跨紦瀛樺け鏁堛
涓嶆帹鑽愭洿鏂版暟鎹椂鍚屾椂鏇存柊鍒扮紦瀛橈紝鍥犱负鍙兘骞跺彂鏇存柊瀵艰嚧鑴忔暟鎹傝 涓轰粈涔 Facebook 鍒犻櫎缂撳瓨鑰屼笉鏄洿鏂扮紦瀛橈紵 浠ュ強 Scaling Memcache at Facebook锛屽叾涓彁鍒般學e choose to delete cached data instead of updating it because deletes are idempotent銆嶃 浣嗗垹闄ょ紦瀛樺苟涓嶆槸瀹屽叏涓嶄細瀵艰嚧鑴忔暟鎹紝鍙槸姒傜巼浼氱浉瀵瑰皬寰堝銆
鎵归噺鏌ヨ
鏌ヨ鏃跺彲鑳戒細闇瑕佺被浼 where id in (xx,yy,zz) 鐨勬儏鍐碉紝杩欐椂鏌ヨ缂撳瓨鍙互浣跨敤 mget 鍚屾椂鏌ヨ澶氫釜 key锛屽彲浠ュぇ澶ф彁楂樻晥鐜囥備笅闈㈡槸 benchmark 鏁版嵁锛
get 81833.06 requests per second
mget 10 73475.39 requests per second 734,753
mget 20 64226.07 requests per second 642,260
mget 30 59559.26 requests per second 1,786,770 99% < 1 milliseconds
mget 50 48995.59 requests per second 2,449,750 99% < 1.5 milliseconds
mget 100 29214.14 requests per second 2,921,414 99% < 2.5 milliseconds
mget 200 16730.80 requests per second 3,346,000 99% < 3 milliseconds
mget 500 7222.30 requests per second 3,611,150 99% < 9 milliseconds
澶嶅埗浠g爜
鏍规嵁鎬昏幏鍙栨暟鎹釜鏁般佸钩鍧囧搷搴旀椂闂达紝閫氬父璁や负 mget 鏁伴噺鎺у埗鍦 100 浠ヤ笅鏄瘮杈冨潎琛$殑銆
鎸夋瘡娆 mget 100 涓 get 鐩告瘮锛屾ц兘鐩稿綋浜庢彁楂樹簡 35 鍊嶃傚啀鍔犱笂璺ㄦ満鍣ㄨ皟鐢ㄥ線杩旂殑鏃堕棿娑堣楋紝瀹為檯鎯呭喌鎬ц兘鎻愬崌寰堝彲鑳 100 鍊嶄互涓娿
鍒嗗竷寮忛攣
Redis 鍙互閫氳繃 SET key randomValue NX EX 30 缁欐煇涓 key 璧嬪硷紝骞跺悓鏃跺垽鏂 key 鏄惁瀛樺湪锛屼互鍙婄粰瀹氳繃鏈熸椂闂淬傝繃鏈熸椂闂磋鏍规嵁涓氬姟鍙樺寲銆
閲婃斁閿佸彲浠ョ洿鎺 del 鎺夎繖涓 key銆備絾鏄 del 鏄湁椋庨櫓鐨勶細
渚嬪 A 鑾峰彇鍒伴攣锛岃繃鏈熸椂闂 30 绉掋傚洜涓烘煇浜涘師鍥 30 绉掓病鑳藉鐞嗗畬璇锋眰锛孊 杩囨潵涔熻幏鍙栧埌浜嗛攣銆傛鏃 A 澶勭悊瀹屾墽琛岄噴鏀鹃攣鐨勬搷浣滐紝灏变細閲婃斁鎺 B 鎵鎸佹湁鐨勯攣銆
涓轰簡閬垮厤杩欎釜闂锛岄渶瑕佸垽鏂 value 鏄笉鏄 set 鏃剁殑 value锛屽鏋滄槸鎵嶆墽琛 del 鎿嶄綔銆備负浜嗚杩欎袱鏉″懡浠ゅ師瀛愭ф墽琛岋紝闇瑕佷娇鐢ㄥ埌 lua 鑴氭湰锛
璁℃暟鍣
Redis 鍙︿竴涓煎緱绉伴亾鐨勫懡浠ゅ氨鏄嚜澧炰簡锛屽叾鎻愪緵浜 incr/incrby/incrbyfloat(string)銆乭incrby/hincr(hash)銆亃incrby锛坺set锛夋柟娉曚緵涓嶅悓鏁版嵁绫诲瀷浣跨敤銆
閫氳繃杩欎簺鍛戒护鍙互瀹炵幇瀵瑰簱瀛樼殑鎵e噺锛岃褰曟帴鍙h闂娆★紝璁板綍涓绡囨枃绔犵殑鐐硅禐鏁般佽瘎璁烘暟銆佽浆鍙戞暟锛屾姠绾㈠寘鎵e噺鏁伴噺绛夈
鎺掕姒
鍒╃敤 zset 鏈夊簭鍒楄〃锛屾瘮濡傝璁$畻鐢ㄦ埛绉垎鎺掕姒滐細
zadd/zincrby 淇濆瓨鎴栬嚜澧炵敤鎴风殑绉垎
zrevrank 鑾峰彇鐢ㄦ埛鐨勬帓鍚
zscore 鑾峰彇鐢ㄦ埛鐨勭Н鍒
zrevrange 鑾峰彇鎺掕
娑堟伅闃熷垪
鍒╃敤 list 鐨 lpush(Left Push) 鍜 brpop(Blocked Right Pop) 鎺ュ彛鍙互瀹炵幇娑堟伅闃熷垪鍔熻兘锛
灏嗘秷鎭 lpush 鍒伴槦鍒椾腑銆
鎵鏈夊疄渚嬮氳繃 brpop 鐩戝惉闃熷垪骞跺彇鍑烘暟鎹繘琛屾秷璐广
鍦ㄦ秷璐圭殑杩囩▼涓彲浠ラ氳繃閰嶇疆绾跨▼姹狅紝鏍规嵁涓氬姟鎯呭喌鍐冲畾娑堣垂閫熺巼銆
寮傛寤惰繜鍚堝苟闃熷垪
姣斿绉掓潃銆佹姠绾㈠寘鏃讹紝搴撳瓨鏁版嵁闇瑕佸紓姝ュ叆搴撱備絾浠呬粎寮傛鍏ュ簱涔熸槸涓嶅鐨勶紝骞朵笉浼氬噺灏戝鏁版嵁搴撴搷浣滅殑娆℃暟銆傝繖鏃跺欏彲鑳介渶瑕佸皢 100 娆¤姹傚帇缂╂垚涓娆¤姹傦紝鍙彇鏈鍚庣殑鏁版嵁钀藉簱銆
姝ょ被闇姹傚垯鍙互鐢 zset + list 瀹炵幇锛屾垜浠渶瑕佸嚑涓笢瑗匡細
闇瑕佸欢杩熸墽琛岀殑浠诲姟鏀惧叆 zset 鍒楄〃涓紝score 涓洪渶瑕佹墽琛岀殑鏃堕棿鎴炽
鍚庡彴璧蜂竴涓嚎绋嬫瘡绉掗挓鎷夊彇 zset 锛屾墽琛 zrangeByScore, score 鑼冨洿涓 0 ~ 褰撳墠鏃堕棿鎴筹紝濡傛灉鍙栧埌鏁版嵁鍒欐斁鍏ユ墽琛岄槦鍒 list 涓紝鏈鍚 zrem 鏌ュ嚭鏉ョ殑鏁版嵁銆
鐩戝惉 list 闃熷垪鐨勬墽琛屽櫒锛屾鏃跺紑濮嬫墽琛屼换鍔°
杩欎篃鏄孩鍖呬腑鐨勫紓姝ユ洿鏂扮殑瀹炵幇鏂瑰紡銆傚湪鎶㈢兢绾㈠寘鏃讹紝濡傛灉姣忔閮芥洿鏂版暟鎹簱涓殑鏁版嵁锛屽娍蹇呬細澧炲姞鍝嶅簲鏃堕棿銆備娇鐢ㄨ繖绉嶆洿鏂版柟寮忕殑璇濓紝鍙湪鏈鍚庝竴娆℃姠绾㈠寘鐨 30s 鍚庢洿鏂帮紝30s 涔嬪唴鍙戠敓鐨勬暟鎹洿鏂帮紝閮藉彧浼氬悎骞朵负 1 鏉°
浜嬩欢閫氱煡
Redis 鎻愪緵浜唒ublish銆乻ubscribe 绛夊懡浠ゅ疄鐜颁簡骞挎挱鍔熻兘锛宲ublish 鏃跺彲浠ュ皢娑堟伅閫氱煡鍒版煇涓閬擄紙channel锛夛紝姝ゆ椂 subscribe 浜嗚繖涓閬撶殑鑺傜偣鍧囪兘鏀跺埌娑堟伅銆
閫氳繃杩欎釜鏈哄埗鎴戜滑鑳藉仛鍒板鍏ㄨ妭鐐圭殑浜嬩欢閫氱煡銆
姣斿鍦ㄧН鍒嗙郴缁熶腑浼氬皢鎵鏈夋椿鍔ㄣ佹娊濂栥佺鍒般佹憞閽辨爲绛夋暟鎹簱閰嶇疆鏁版嵁鏀惧叆 JVM 缂撳瓨涓紝浠ヤ究鑾峰緱鏈楂樼殑鎬ц兘銆 涓轰簡鏇存柊鏁版嵁锛屼竴寮濮嬫槸姣忓垎閽熷埌鏁版嵁搴撴洿鏂颁竴娆°備絾闂鏄瘡鍙板疄渚嬫洿鏂扮殑鏃舵満閮戒笉鍚岋紝瀵艰嚧璇锋眰鍒 A 瀹炰緥鐨勬暟鎹紝涓 B 瀹炰緥涓婄殑涓嶅悓銆傞殢鍚庡皢瀹氭椂浠诲姟鐨勯厤缃敼鎴愪簡姣忓垎閽熺殑绗 0 绉掓墽琛岋紝鍒欏緢澶х▼搴︿笂鏀瑰杽浜嗛棶棰樸
浣嗘槸杞鐨勬柟寮忎粛鐒朵笉澶熶紭闆咃紝缁濆ぇ閮ㄥ垎鏃跺欏彇寰楃殑閰嶇疆骞舵病鏈夊彉鍖栵紝鏄棤鐢ㄧ殑璇锋眰銆傛洿鏂伴厤缃殑鏃舵満搴旇鏄厤缃彂鐢熶簡鍙樻洿鎵嶅銆
杩欐椂灏卞彲浠ヤ娇鐢 Redis 骞挎挱锛屾瘡褰撴暟鎹簱鏁版嵁鍙戠敓鍙樺寲鏃讹紝閫氳繃骞挎挱閫氱煡鎵鏈夎妭鐐规洿鏂版暟鎹紝鎴栬呭共鑴嗗皢瑕佹洿鏂扮殑鏁版嵁鏀惧叆骞挎挱涓
浼樺寲
Redis 铏界劧鎬ц兘寮烘倣锛屼絾鏄敱浜庡崟绾跨▼鐨勭壒鎬э紝涓鏃︿骇鐢熸參鏌ヨ锛屼細灏嗘墍鏈夋搷浣滈兘闃诲浣忋傛墍浠ヤ娇鐢ㄤ笂浠嶉渶瑕佹敞鎰忎細韪╁摢浜涘潙銆俁edis 鎻愪緵浜 slowlog get 鏌ョ湅鎱㈡煡璇€
甯歌闆峰尯
keys * keys 鍛戒护鐨勬椂闂村鏉傚害鏄 O(n)锛宯 鏄 Redis 涓墍鏈夐敭鐨勬暟閲忥紝杩欎釜鏄渶甯歌鐨勬ц兘鏈宸殑鍛戒护銆備竴鑸嚎涓婇兘鎶婅繖涓懡浠 block 鎺夛紙鍦ㄩ厤缃腑鍔 rename-command KEYS “”锛
澶 key銆備竴涓 key 閲屽瓨鍌ㄧ殑鏁版嵁瓒婂锛岄氬父鎬ц兘瓒婂樊锛屾瘮濡傚瓒呭ぇ鐨 List 杩涜 lindex 鍜 lrange銆傚彟澶栧ぇ value 鍦ㄩ泦缇ゆ暟鎹縼绉绘椂浼氶樆濉炲彲鑳藉鑷 fail over銆傜敋鑷冲湪鍒犻櫎鏃朵篃浼氶樆濉烇紝渚嬪鍒犻櫎涓涓 1kw 鏁版嵁閲忕殑 set锛岄渶瑕佽楁椂 5s銆傛垨鑰呭湪闆嗙兢涓ぇ key 浼氬鑷撮泦缇ゅ唴瀛樺垎閰嶄笉鍧囧寑銆傛墍浠ュ湪浣跨敤鏃堕渶瑕侀伩鍏嶅湪涓涓 key 涓斁鍏ヨ繃澶氭暟鎹
bgrewriteaof銆乥gsave锛岄噸鍐 aof 鏂囦欢鍙婂浠 RDB 鏂囦欢鏃讹紝浼 fork 鍑哄瓙杩涚▼鍜屽唴瀛橈紝姝ゆ湡闂存槸闃诲鐨勶紝鍙栧喅浜 Redis 鍐呭瓨澶у皬鍜屾満鍣ㄦц兘銆傛墍浠ヨ澶氫紒涓氱殑鍋氭硶鏄富鑺傜偣涓婂叧闂 aof 鍜 rdb锛屽彧鍦ㄤ粠鑺傜偣涓婂浠姐
澶 key 鐨勬媶鍒
绉垎绯荤粺涓瓨鍦ㄤ竴涓繘璐$殑浠诲姟锛岄個璇蜂汉鍙幏寰楄閭璇蜂汉鍋氫换鍔$殑濂栧姳锛屽苟鍦ㄦ瘡澶╁噷鏅ㄥ叆璐︺
瀵逛簬杩欎釜浠诲姟锛屾垜浠仛鐨勭涓姝ヤ紭鍖栧氨鏄瘡澶╁皢鑾峰緱浜嗚繘璐″鍔辩殑鐢ㄦ埛锛屼繚瀛樺湪 set 閲岋紝閫氳繃 sscan 閬嶅巻闇瑕佽繘璐$殑鐢ㄦ埛锛屾墽琛屼换鍔°備互姝ら伩鍏嶄簡鎵簱锛屼繚璇佹瘡娆″彇寰楃殑 userId 閮芥槸纭垏鏈夋晥鐨勩傞棶棰樺湪浜庝竾涓瀛愬脊鐭俊鐏簡锛宻et 涓殑 userId 浼氬師鏉ヨ秺澶氾紝涔熷氨閬囧埌浜嗗ぇ key 鐨勯棶棰橈紝闇瑕佸皢 set 鎷嗗垎涓哄涓 set銆
鎷嗗垎鐨勬濊矾鍜 Redis 闆嗙兢鍒嗙墖绫讳技锛岄氳繃 hash(userId) % count 鐨勬柟寮忥紝寰楀埌 0 ~ count 涔嬮棿鐨勫垎鐗囨暟锛屽皢鍏跺姞鍒板師鏈殑 key 涓婏紝杩囩▼濡備笅锛
閫氳繃 hash(userID) % count锛屽緱鍒板垎鐗囨暟锛屽 16
鍘熸湰 key 涓恒孴RIBUTE:USER:SET:20181225銆嶏紝鍐嶅姞涓婂垎鐗囨暟鍗冲緱鍒般孴RIBUTE:USER:SET:20181225:16銆嶏紝鍐嶅皢 userId sadd 鏀惧叆鍗冲彲
鍙栧嚭鎵鏈 key 鏃 for 寰幆浠 0-count 鎷煎埌 key 涓婏紝鍐嶉拡瀵规瘡涓 key sscan銆
浣跨敤 Hash
渚嬪淇濆瓨涓绡囨枃绔犵殑鐐硅禐鏁般佽浆鍙戞暟銆佽瘎璁烘暟鏃讹紝鏃㈠彲浠ヤ繚瀛樹负 3 涓 value锛屽嵆 article:like銆乤rticle:repost銆乤rticle:comment銆備篃鍙互淇濆瓨涓轰竴涓 hash 瀵硅薄锛宬ey 涓 article锛宧ashKey 涓 like銆乺epost銆乧omment銆
濂藉锛
閫氳繃涓鏉 hgetall 灏辫兘鍙栧緱鎵闇鏁版嵁
鑺傜害鍐呭瓨銆
浣跨敤 value 锛
for i=1,1000000,1 do
redis.call(“set”,”article:like:”..i,1)
redis.call(“set”,”article:repost:”..i,1)
redis.call(“set”,”article:comment:”..i,1)
end
used_memory:226568704
used_memory_human:216.07M
used_memory_rss:282144768
used_memory_rss_human:269.07M
澶嶅埗浠g爜
浣跨敤 hash 锛
for i=1,1000000,1 do
redis.call(“HMSET”,”article:”..i, “like”, 1, “repost”, 1, “comment”, 1)
end
used_memory:121402896
used_memory_human:115.78M
used_memory_rss:132640768
used_memory_rss_human:126.50M
澶嶅埗浠g爜
value 鍑犱箮澶氫娇鐢ㄤ簡涓鍊嶅唴瀛樸傚師鍥犳槸 hash 绫诲瀷杩欐椂浼氶夋嫨 ziplist 鏁版嵁缁撴瀯瀹炵幇銆
鍙戣〃鍥炲