Verify: migrate from Memcached to KiviDB
Verify. Migrate to KiviDB: Move from Redis, Valkey, ElastiCache, Redis Cloud, Aiven, Aerospike, Hazelcast and more: getting the data out, loading it, cutting
Count items on both sides:
printf "stats\r\nquit\r\n" | nc memcached.internal 11211 | grep curr_items
redis-cli -h kividb.internal -p 6380 -a "$KIVIDB_PASSWORD" DBSIZE
redis-cli -h kividb.internal -p 6380 -a "$KIVIDB_PASSWORD" INFO keyspace
INFO keyspace also shows how many keys have an expiry (expires=).
Then compare a random sample, value and TTL. This uses the functions from the copy program, so keep both files in one directory:
"""Compare N random memcached items with KiviDB: value and TTL.
python3 verify_memcached.py MEMCACHED_HOST MEMCACHED_PORT KIVIDB_URL [N]
"""
import random, sys
import redis
from copy_memcached import Meta, list_keys, read_items
host, port, url = sys.argv[1], int(sys.argv[2]), sys.argv[3]
n = int(sys.argv[4]) if len(sys.argv) > 4 else 1000
kv = redis.Redis.from_url(url)
keys = list_keys(host, port)
print(f"memcached keys: {len(keys)} KiviDB DBSIZE: {kv.dbsize()}")
sample = random.sample(keys, min(n, len(keys)))
bad = 0
for key, value, _flags, ttl in read_items(Meta(host, port), sample):
got, kttl = kv.get(key), kv.ttl(key)
if got != value:
bad += 1
print("value differs:", key)
elif (ttl == -1) != (kttl == -1) or (ttl > 0 and abs(ttl - kttl) > 5):
bad += 1
print(f"ttl differs: {key} memcached={ttl} kividb={kttl}")
print(f"checked {len(sample)}, mismatches {bad}")
sys.exit(1 if bad else 0)
Items that expire between the two reads are skipped. Run it right after the copy, before the application starts writing to KiviDB.