redis
redis
k-v
date type for v
https://redis.io/topics/data-types-intro
- string
- list: 队列,阻塞访问BRPOP, BLPOP
- set: set
- sorted set: for i in this: i ~= (score, v), i is sorted by score in this
- hash: dict with string limited?
- Bit arrays: 2进制位图,第i位(0,1)表示: i is False or True; 例如统计一年多少天登录过需要366bit
- HyperLogLogs:
# example
http://content.research.neustar.biz/blog/hll.html
# explain
http://antirez.com/news/75
- list=64k # 分2^^14组, 50位比特数中,记录到尾部连续(连续为正面m-1=1) # m-1个全0的为(1/2^^(m-1)),就概率有2^^m -1个不同已出现
- h = hash(string) 64bit 分割14bit(其10进制数对应list.index),50bit(最右边非0位**2对应list.value)
- 倒数平均数(set)
- Streams: append only list
安装
单机
pass
cluster
https://redis.io/topics/cluster-tutorial cluster-enabled yes 同kafaka, 采用选举模式, 除了学要使用命令开启过程 redis-cli –cluster create 127.0.0.1:7000 127.0.0.1:7001 \ 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \ –cluster-replicas 1
同步使用异步、同步写
config
maxmemory: peak memory usage
使用
- base https://redis.io/commands
- mq https://redis.io/topics/pubsub
- Transactions https://redis.io/topics/transactions
- loadbalance/hashring https://redis.io/topics/partitioning
- 分布式锁逻辑
- 单点逻辑: clinet.set(lock.name, clinet.uuid, only_not_exist) ? ( do something done; client.get(lock.name) == client.uuid ?return del(lock) : return 0 ) : return -1
- 多点逻辑: 多数派逻辑(长用于选举) 依次按1, 获取各个节点锁,过一半即可认为获取到; else del all if lock.name.v == client.uuid
- persistence https://redis.io/topics/persistence RDB: 快照 AOF: 类似mysql, 可选多久将记录同步到磁盘