瀛愬脊redis

鍘熺悊

Redis 鏄竴涓唴瀛樺瀷銆屾暟鎹簱銆嶏紝闄ゅ瓨鍌ㄤ箣澶栵紝瀹冭繕鏈夎澶氬己澶х殑鍛戒护锛屼娇涔嬭繙杩滆秴鍑轰簡鏁版嵁搴撶殑瀹氫箟锛屾墍浠ュ畼鏂圭О涔嬩负銆宒ata structure store銆嶏紝鏁版嵁缁撴瀯瀛樺偍绯荤粺銆 閫氳繃 Redis 鎻愪緵鐨勬寚浠わ紝鎴戜滑鍙互瀹炵幇缂撳瓨銆佹秷鎭槦鍒椼佷簨浠堕氱煡銆佹帓琛屾銆佸簱瀛樼鐞嗐佸垎甯冨紡閿佺瓑鍔熻兘銆

鍩虹缁撴瀯

Redis 鏍稿績鏄崟杩涚▼鍗曠嚎绋嬫湇鍔★紝閫氳繃 epoll銆乻elect 绛夊疄鐜颁簡 IO 澶氳矾澶嶇敤锛屽彲浠ュ苟鍙戝鐞嗙綉缁滀簨浠躲

鏁版嵁缁撴瀯

Redis 鎻愪緵浜嗕互涓嬪嚑绉嶅吀鍨嬬殑鏁版嵁缁撴瀯

strings

Redis 瀹炵幇浜嗗悕涓 SDS(Simple Dynamic String) 鐨勫瓧绗︿覆绫诲瀷锛屼笌 C 瀛楃涓插尯鍒細

瀹炵幇瀛楃涓叉嫾鎺ワ紝鍑忓皯鍐呭瓨閲嶅垎閰
缁存姢浜嗗瓧绗︿覆鐨勯暱搴︼紝浠ヤ究蹇熻幏鍙栧強閬垮厤缂撳啿鍖烘孩鍑
浜岃繘鍒跺畨鍏紝鍗虫敮鎸佸瓨鍌ㄧ┖鏍(\0)

linkedlist

Redis 瀹炵幇浜嗗弻鍚戞棤鐜摼琛紝骞朵娇鐢ㄦ鏁版嵁缁撴瀯瀹炵幇浜 list銆

Hashtable

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) 鐨勬煡璇€

intset

鏁存暟闆嗗悎锛屽嵆鍙繚瀛樻暣鏁扮殑闆嗗悎銆俁edis 浣跨敤璇ユ暟鎹粨鏋勫疄鐜颁簡 Set銆

ziplist

鍘嬬缉鍒楄〃銆傚帇缂╁垪琛ㄦ槸涓绉嶇壓鐗叉ц兘鑺傜害绌洪棿鐨勬暟鎹粨鏋勶紝鐩告瘮閾捐〃锛屽畠鑺傜害浜嗘寚閽堢殑绌洪棿锛孯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 鏈変袱绉嶅浠芥暟鎹殑鏂瑰紡锛

AOF

鍗 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 鏂囦欢锛岄噸鏂板皢鍐呭瓨涓殑鏁版嵁浣滀负鍛戒护鍐欏叆鏂囦欢涓

RDB

鍗 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銆

RDB-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 鍙互鏌ョ湅澶嶅埗鐨勭姸鎬侊細

Replication

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

Replication

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 锛

lua

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

memory

used_memory:226568704
used_memory_human:216.07M
used_memory_rss:282144768
used_memory_rss_human:269.07M
澶嶅埗浠g爜
浣跨敤 hash 锛

lua

for i=1,1000000,1 do
redis.call(“HMSET”,”article:”..i, “like”, 1, “repost”, 1, “comment”, 1)
end

memory

used_memory:121402896
used_memory_human:115.78M
used_memory_rss:132640768
used_memory_rss_human:126.50M
澶嶅埗浠g爜
value 鍑犱箮澶氫娇鐢ㄤ簡涓鍊嶅唴瀛樸傚師鍥犳槸 hash 绫诲瀷杩欐椂浼氶夋嫨 ziplist 鏁版嵁缁撴瀯瀹炵幇銆

All posts

Other pages


Deprecated: 鏂囦欢 娌℃湁 comments.php 鐨勪富棰 鑷増鏈 3.0.0 璧峰凡寮冪敤锛屼笖娌℃湁鍙敤鐨勬浛浠c 璇峰湪鎮ㄧ殑涓婚涓寘鍚竴涓 comments.php 妯℃澘銆 in /www/wwwroot/liguoqi.site/wp-includes/functions.php on line 6078

鍙戣〃鍥炲

鎮ㄧ殑鐢靛瓙閭鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢 * 鏍囨敞