這里的Redis主從結(jié)構(gòu)可以是簡(jiǎn)單的主從,sentinel,redis cluster中的主從等。
wait命令的作用:
此命令將阻塞當(dāng)前客戶(hù)端,直到當(dāng)前Session連接(主節(jié)點(diǎn)上)所有的寫(xiě)命令都被傳送到指定數(shù)據(jù)量的slave節(jié)點(diǎn)。
如果到達(dá)超時(shí)(以毫秒為單位),則即使尚未完全傳送到達(dá)指定數(shù)量的salve節(jié)點(diǎn),該命令也會(huì)返回(成功傳送到的節(jié)點(diǎn)的個(gè)數(shù))。
該命令將始終返回確認(rèn)在WAIT命令之前發(fā)送的寫(xiě)命令的副本數(shù)量,無(wú)論是在達(dá)到指定數(shù)量的副本的情況下,還是在達(dá)到超時(shí)的情況下。
具體說(shuō)就是:比如對(duì)于1主2從的結(jié)構(gòu),Wait要求3秒鐘之內(nèi)傳送到2個(gè)節(jié)點(diǎn),但是達(dá)到超時(shí)時(shí)間3秒鐘之后只成功傳送到了1個(gè)slave節(jié)點(diǎn)上,此時(shí)wait也不會(huì)繼續(xù)阻塞,而是返回成功傳送的節(jié)點(diǎn)個(gè)數(shù)(1)。
有點(diǎn)類(lèi)似于MySQL的半同步復(fù)制,但是效果完全不能跟半同步相比,因?yàn)镽edis本身沒(méi)有回滾的功能,這里的wait命令發(fā)起之后,即便是超時(shí)時(shí)間之后沒(méi)有送到任何一個(gè)slave節(jié)點(diǎn),主節(jié)點(diǎn)也不會(huì)回滾。
wait命令無(wú)法保證Redis主從之間的強(qiáng)一致,不過(guò),在主從、sentinel和Redis群集故障轉(zhuǎn)移中,wait能夠增強(qiáng)(僅僅是增強(qiáng),但不是保證)數(shù)據(jù)的安全性。
既然wait命令在當(dāng)前連接之后會(huì)等待指定數(shù)量的從節(jié)點(diǎn)確認(rèn),其主節(jié)點(diǎn)的寫(xiě)入效率必然會(huì)收到一定程度的影響,那么這個(gè)影響有多大?
這里做一個(gè)簡(jiǎn)單的測(cè)試,環(huán)境2核4G的宿主機(jī),docker下的集群3主3從的Redis集群,因此不用考慮網(wǎng)絡(luò)延遲,在執(zhí)行寫(xiě)入操作之后,使用兩個(gè)Case,對(duì)比使不使用wait命令等待傳送到salve的效率,
1,單線(xiàn)程循環(huán)寫(xiě)入100000個(gè)key值
2,多線(xiàn)程并發(fā),10個(gè)線(xiàn)程每個(gè)線(xiàn)程寫(xiě)入10000個(gè)key,一共寫(xiě)入100000個(gè)key
Case1:單線(xiàn)程循環(huán)寫(xiě)入100000個(gè)key值
結(jié)論:不使用wait命令,整體耗時(shí)33秒,集群中單個(gè)節(jié)點(diǎn)的TPS為1000左右;使用wait命令,整體耗時(shí)72秒,集群中單個(gè)節(jié)點(diǎn)的TPS為480左右,整體效率下降了50%多一點(diǎn)
單線(xiàn)程不使用WAIT
單線(xiàn)程使用WAIT(redis_conn.execute_command('wait', 1, 0))
Case2:多線(xiàn)程循環(huán)寫(xiě)入100000個(gè)key值
結(jié)論:不使用wait命令,整體耗時(shí)19秒,集群中單個(gè)節(jié)點(diǎn)的TPS為1700左右;使用wait命令,整體耗時(shí)36秒,集群中單個(gè)節(jié)點(diǎn)的TPS為900左右,整體效率與單線(xiàn)程基本上一致,下降了50%多一點(diǎn)
多線(xiàn)程不使用WAIT,單節(jié)點(diǎn)上TPS可達(dá)到1700左右
多線(xiàn)程使用WAIT,單節(jié)點(diǎn)上TPS可達(dá)到850左右
鑒于在多線(xiàn)程模式下,CPU負(fù)載接近于瓶頸,因此不能再加更多的線(xiàn)程數(shù),測(cè)試數(shù)據(jù)也僅供參考。
總結(jié):
wait能夠在主節(jié)點(diǎn)寫(xiě)入命令之后,通過(guò)阻塞的方式等待數(shù)據(jù)傳送到從節(jié)點(diǎn),wait能夠增強(qiáng)(但不保證)數(shù)據(jù)的安全性。
其代價(jià)或者說(shuō)性能損耗也是不小的,通過(guò)以上測(cè)試可以看出,即便是不考慮網(wǎng)絡(luò)傳輸延遲的情況下,其性能損耗也超出了50%。
#!/usr/bin/env python# coding:utf-8import sysimport timeimport datetimefrom rediscluster import StrictRedisClusterimport threadingfrom time import ctime,sleepdef redis_cluster_write(): redis_nodes = [ {'host':'172.18.0.11','port':8888}, {'host':'172.18.0.12','port':8888}, {'host':'172.18.0.13','port':8888}, {'host':'172.18.0.14','port':8888}, {'host':'172.18.0.15','port':8888}, {'host':'172.18.0.16','port':8888}] try: redis_conn = StrictRedisCluster(startup_nodes=redis_nodes,password='******') except Exception: raise Exception redis_conn.config_set('cluster-require-full-coverage', 'yes') counter = 0 for i in range(0,100000): counter = counter+1 redis_conn.set('key_'+str(i),'value_'+str(i)) #redis_conn.execute_command('wait', 1, 0) if counter == 1000: print('insert 1000 keys '+str(str(datetime.datetime.now()))) counter = 0def redis_concurrence_test(thread_id): redis_nodes = [ {'host':'172.18.0.11','port':8888}, {'host':'172.18.0.12','port':8888}, {'host':'172.18.0.13','port':8888}, {'host':'172.18.0.14','port':8888}, {'host':'172.18.0.15','port':8888}, {'host':'172.18.0.16','port':8888}] try: redis_conn = StrictRedisCluster(startup_nodes=redis_nodes, password='******') except Exception: raise Exception redis_conn.config_set('cluster-require-full-coverage', 'yes') counter = 0 for i in range(0, 10000): counter = counter + 1 redis_conn.set('key_' + str(thread_id)+'_'+str(counter), 'value_' + str(i)) #redis_conn.execute_command('wait', 1, 0) if counter == 1000: print(str(thread_id)+':insert 1000 keys ' + str(str(datetime.datetime.now()))) counter = 0if __name__ == '__main__': #redis_cluster_write() threads = [] for i in range(10): t = threading.Thread(target=redis_concurrence_test, args=(i,)) threads.append(t) begin_time = ctime() for t in threads: t.setDaemon(True) t.start() for t in threads: t.join()
聯(lián)系客服