Redis 數(shù)據(jù)遷移的項目實(shí)踐
redis遷移
通過Redis-shake的sync(同步)模式,可以將Redis的數(shù)據(jù)實(shí)時遷移至另一套Redis環(huán)境。
前提條件: Redis的版本為2.8、3.0、3.2、4.0或5.0版本。
redis-shake 介紹
- redis-shake是阿里云用于Redis數(shù)據(jù)傳輸與過濾的開源工具,該工具易于部署,靈活高效。
- redis-shake的Sync(同步)模式支持全量數(shù)據(jù)遷移和增量數(shù)據(jù)遷移;
- Restore(恢復(fù))模式支持將本地RDB文件中的數(shù)據(jù)恢復(fù)至目標(biāo)端;
- Scan模式是使用SCAN命令來獲取源端Redis中的Key,然后使用DUMP命令獲取Key的值,最終使用RESTORE命令將Key(與其值)恢復(fù)至目標(biāo)端。
不同模式的同步流程如下圖所示:
注意事項:
- 如果目標(biāo)庫的數(shù)據(jù)逐出策略(maxmemory-policy)配置為除noeviction以外的值,可能導(dǎo)致目標(biāo)庫的數(shù)據(jù)與源庫不一致。關(guān)于數(shù)據(jù)逐出策略詳情,請參見Redis數(shù)據(jù)逐出策略。
- 如果源庫中的部分Key使用了過期(Expire)機(jī)制,由于可能存在Key已過期但未被及時刪除的情形,在目標(biāo)庫中查看(如通過info命令)的Key數(shù)量會比源庫的Key數(shù)量少。
redis-shake 3.X 版本測試
redis-shake 3.X 下載
wget 'https://github.com/alibaba/RedisShake/releases/download/v3.1.7/redis-shake-linux-amd64.tar.gz'
redis-shake 3.X 解壓
mkdir redis-shake tar zxvf redis-shake-linux-amd64.tar.gz -C redis-shake
redis-shake 3.X 配置
我們使用redis-shake中sync模式,以下配置已sync配置為例
type = "sync" [source] version = 2.8 # redis version, such as 2.8, 4.0, 5.0, 6.0, 6.2, 7.0, ... address = "x.x.x.x:6379" username = "" # keep empty if not using ACL password = "xxxxxxx" # keep empty if no authentication is required tls = false elasticache_psync = "" # using when source is ElastiCache. ref: https://github.com/alibaba/RedisShake/issues/373 [target] type = "standalone" # "standalone" or "cluster" version = 2.8 # redis version, such as 2.8, 4.0, 5.0, 6.0, 6.2, 7.0, ... # When the target is a cluster, write the address of one of the nodes. # redis-shake will obtain other nodes through the `cluster nodes` command. address = "x.x.x.x:6379" username = "" # keep empty if not using ACL password = "xxxxxxx" # keep empty if no authentication is required tls = false [advanced] dir = "data" ncpu = 4 # runtime.GOMAXPROCS, 0 means use runtime.NumCPU() cpu cores pprof_port = 0 # pprof port, 0 means disable metrics_port = 0 # metric port, 0 means disable log_file = "redis-shake.log" log_level = "info" # debug, info or warn log_interval = 5 # in seconds rdb_restore_command_behavior = "skip" # panic, rewrite or skip pipeline_count_limit = 1024 # pipeline target_redis_client_max_querybuf_len = 1024_000_000 target_redis_proto_max_bulk_len = 512_000_000
redis-shake 3.X 啟動
./redis-shake sync.toml
日志字段說明:
- allowOps:表示每秒向目標(biāo)庫發(fā)送多少條命令
- disallowOps:表示每秒過濾的命令數(shù)。
- entryId:從1開始計數(shù),表示redis-shake共處理多少條命令。
- InQueueEntriesCount:表示還剩余多少條命令待發(fā)送。
Redis數(shù)據(jù)逐出策略
volatile-lru 按照LRU算法逐出原有數(shù)據(jù),但僅逐出設(shè)置了過期時間的數(shù)據(jù)。
volatile-ttl 僅逐出設(shè)置了過期時間的數(shù)據(jù),并且是按照TTL由小到大的順序進(jìn)行逐出。
allkeys-lru 按照LRU算法逐出原有數(shù)據(jù)。
volatile-random 隨機(jī)逐出原有數(shù)據(jù),但僅逐出設(shè)置了過期時間的數(shù)據(jù)。
allkeys-random 隨機(jī)逐出原有數(shù)據(jù)。
noeviction 不逐出任何數(shù)據(jù),當(dāng)內(nèi)存已滿時新數(shù)據(jù)的寫入會得到一個錯誤信息(DEL和某些其他的命令除外)。
volatile-lfu 按照LFU算法逐出原有數(shù)據(jù),只從設(shè)置了過期時間的key中選擇最不常用的key進(jìn)行刪除。
allkeys-lfu 按照LFU算法優(yōu)先逐出最不常用的key。
reids-shake 2.X 版本測試
reids-shake 2.X 下載
wget 'http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/120287/cn_zh/1608173646665/redis-shake-v2.0.3.tar.gz'
reids-shake 2.X 解壓
tar xzf redis-shake-v2.0.3.tar.gz
reids-shake 2.X 配置
conf.version = 1 #當(dāng)前配置文件版本號,請不要修改 id = redis-shake log.file = #指定日志文件,不指定將打印到stdout log.level = info pid_path = #指定pid路徑,不指定默認(rèn)/var/run system_profile = 9310 http_profile = 9320 # metric端口號 -1表示不啟用 parallel = 32 #啟動多少個并發(fā)線程同步一個RDB文件,默認(rèn)32 source.type = sentinel #支持standalone,sentinel,cluster和proxy四種模式 source.address = sourcemaster:master@x.x.x.x:26379 source.password_raw = XXXXXXXXXXXXXXXXXXX source.auth_type = auth source.tls_enable = false source.rdb.input = local source.rdb.parallel = 0 source.rdb.special_cloud = target.type = sentinel target.address = targetmaster:master@x.x.x.x:26379 target.password_raw = XXXXXXXXXXXXXXXXXXX target.auth_type = auth target.db = -1 target.tls_enable = false target.rdb.output = local_dump target.version = 2.8 #目標(biāo)redis版本 fake_time = #處理過期的鍵值,當(dāng)遷移兩端不一致,目的端需要加上這個值 key_exists = ignore #當(dāng)源目的有重復(fù)的key,支持rewrite(覆蓋目的端key)、none(進(jìn)程直接退出)、ignore(保留目的端key,忽略源端key) filter.db.whitelist = 5;9 #指定通過的db 該配置db5、db9通過其他忽略 filter.db.blacklist = #指定過濾的db filter.key.whitelist = #指定特定前綴的key通過 filter.key.blacklist = #指定特定前綴的key過濾 filter.slot = filter.lua = false big_key_threshold = 524288000 #key不大直接調(diào)用restore寫入目的端,如果key對應(yīng)的value字節(jié)超過定制那么分批次一個一個寫入,如果目的端是Codis 該參數(shù)需要置1,目的端大版本小于源端,建議置1. metric = true metric.print_log = false sender.size = 104857600 #發(fā)送緩存的字節(jié)長度,超過這個閾值將會強(qiáng)行刷緩存發(fā)送 sender.count = 4095 #發(fā)送緩存的報文個數(shù),超過這個閾值將會強(qiáng)行刷緩存發(fā)送 sender.delay_channel_size = 65535 # 用于metric統(tǒng)計時延的隊列 keep_alive = 0 # TCP keep-alive?;顓?shù),單位秒,0表示不啟用。 scan.key_number = 50 # 每次scan的個數(shù),不配置則默認(rèn)100. scan.special_cloud = #有些版本具有特殊格式目前支持騰訊云的集群版"tencent_cluster"、阿里云的集群版"aliyun_cluster",主從版本不需要,只針對集群版。 scan.key_file = #有些云版本,不支持sync/psync,也不支持scan,我們支持從文件中進(jìn)行讀取所有key列表并進(jìn)行抓取:一行一個key。 qps = 200000 #每秒傳輸key的個數(shù) resume_from_break_point = false #斷點(diǎn)續(xù)傳 replace_hash_tag = false
reids-shake 2.X 啟動
./redis-shake.linux -type=sync -conf=redis-shake.conf
字段說明:
- forwardCommands:源庫發(fā)送過來的命令數(shù)。
- filterCommands:被過濾的命令數(shù),例如在Redis-shake配置文件中設(shè)置了過濾某些庫。
- writeBytes:源庫發(fā)送過來的字節(jié)數(shù)。
到此這篇關(guān)于Redis 數(shù)據(jù)遷移的項目實(shí)踐的文章就介紹到這了,更多相關(guān)Redis 數(shù)據(jù)遷移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis實(shí)現(xiàn)延遲任務(wù)的常見方案詳解
延遲任務(wù)(Delayed?Task)是指在未來的某個時間點(diǎn),執(zhí)行相應(yīng)的任務(wù),本文為大家整理了Redis實(shí)現(xiàn)延遲任務(wù)的幾個常見方案,希望對大家有所幫助2024-04-04基于Redis實(shí)現(xiàn)每日登錄失敗次數(shù)限制
這篇文章主要介紹了通過redis實(shí)現(xiàn)每日登錄失敗次數(shù)限制的問題,通過redis記錄登錄失敗的次數(shù),以用戶的username為key,本文給出了實(shí)例代碼,需要的朋友可以參考下2019-08-08詳解redis數(shù)據(jù)結(jié)構(gòu)之壓縮列表
這篇文章主要介紹了詳解redis數(shù)據(jù)結(jié)構(gòu)之壓縮列表的相關(guān)資料,壓縮列表在redis中的結(jié)構(gòu)體名稱為ziplist,其是redis為了節(jié)約內(nèi)存而聲明的一種數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2017-05-05Redis實(shí)戰(zhàn)之百度首頁新聞熱榜的實(shí)現(xiàn)代碼
這篇文章主要介紹了Redis實(shí)戰(zhàn)之百度首頁新聞熱榜的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02redis解決高并發(fā)看門狗策略的實(shí)現(xiàn)
本文主要介紹了redis解決高并發(fā)看門狗策略的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02