redis屬於單程式的服務,它主要受記憶體、CPU、磁碟IO(主要是做持久化),如果伺服器配置比較高,多核CPU、高記憶體的伺服器,可以考慮做redis多例項。做多例項之前,首先要考慮CPU和記憶體的利用,我在測試的時候發現,redis在QPS為6-8W左右的時候,這個redis所在的邏輯CPU核的負載就在100%左右,所以要優化CPU使用這塊,目前一般是是做網路卡軟中斷來實現平衡這種單程式使用CPU過高的情況,不過需要網路卡支援網路卡軟中斷,效果不錯。

   多例項redis的管理,涉及到監控、服務的管理等,這裡只說redis多例項的重啟。以下是多例項的重啟指令碼,僅供交流參考:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import redis,threading,sys,socket,time,os
from multiprocessing import Pool
def port_check(host,port):
    POOL = redis.ConnectionPool(host=host, port=port)
    my_server = redis.Redis(connection_pool=POOL)
    try:
        a = my_server.ping()
        if a:
            return 1
        else:
            return 0
    except Exception,e:
        return 0
def shutdown_server(host,port):
    flag = port_check(host,port)
    if flag == 1:
        POOL = redis.ConnectionPool(host=host, port=port)
        my_server = redis.Redis(connection_pool=POOL)
        my_server.shutdown()
        while 1:
            flag = port_check(host,port)
            if flag == 0:
                print "%s:%s is Shutdown OK" % (host,port)
                break
            else:
                time.sleep(1)
    else:
        print "%s:%s is Shutdown alreaday" % (host,port)
def start_server(host,port):
    flag = port_check(host,port)
    if flag == 0:
        start_conm = "/usr/local/bin/redis-server /usr/local/redis/etc/redis_%s.conf" % port
        os.popen(start_conm)
        time.sleep(3)
        i = 0
        while 1:#每5s檢測一次redis ping,持續一分鐘,1分鐘沒有起來認為啟動失敗
            flag = port_check(host,port)
            if flag == 1:
                print "%s:%s is Start OK" % (host,port)
                break
            else:
                if i > 12:
                    print "%s:%s is Start Error" % (host,port)
                    break
                else:
                    time.sleep(5)
                    i = i + 1
    else:
        print "%s:%s is Start alreaday" % (host,port)
def restart(host,port):
    shutdown_server(host,port)
    start_server(host,port)
def main():
    server_list = ["127.0.0.1:6379","127.0.0.1:16379"]
    pool = Pool(processes=3)
    for i in server_list:
        aa = i.split(`:`)
        host = aa[0]
        port = int(aa[1])
        results = pool.apply_async(restart, (host,port))
    pool.close()
    pool.join()
    if results.successful():
        print `successful`
if __name__=="__main__":
    main()