Redis 很黃很暴力
===
### CAP Theory
- Multiplele statement consistency
```
Begin Tx
Tx update user
where id = A set -100
Tx update B + 100
Commit Tx
```
- 2-phase commit
3 redis, apply transaction on user A and B
|A| |x| |B|
Can 2-phase commit satisfy CAP on redis? No, redis will lock A,B and not available
### On single machine
- mongodb journaling enable
- Return success before/after write to HD
### Redis
- Data might loss even if you enable persistent mode
- Use redis for search app?
- Single thread
- Don't use `keys` on production : **O(db)**
- might not suitable according to above
### Local cache
- Use local memory on the machine
- Local cache is important and useful for some case, but you shouldn't only rely on local cache
- 3 nodes -> 4 nodes
- old nodes 10% cache miss, new node 100% cache miss
- 10% -> 2.5%*3(old) + 25%(new) traffic goes to main DB
### High traffic caching
- check the status after get lock
### Consistent hash
- Open new redis server
- ideal case
- 33%,33%,33% each transfer 8% to new server
- only some cache miss
- bad case
- Inconsistent key hash
- ~~slient~~ large cache miss
### Locking server
- Redis way: SET <><> NX EX
- Single thread as lock
- not blocking, use for-loop busy waiting
- no FIFO
```
for(1)
SET NX
if OK then
return OK
else
sleep 50ms
```
- jitter
- add random to sleep time
- 50ms -> 50~60ms
- reduce the average lock waiting time
- exponential backoff
- non-fixed waiting time
- wait from short time to long time
### MQ Server
- ensure job finished at least once
- MQ -> replica
- replica execute
- replica -> MQ (finished)
- If replica no response, MQ keep the job
- zrange sorts all data
- sacrifice precision
- random select partial data and sort
- sampling
### C100K over 100QPS capability
- traffic explosion
- start saling at some time point
- auto scaling will fail
- guess the traffic
- AccessToken
- If use random string as key and store info in redis
- Everyone needs to access redis
- JWT avoid this sitiuation
- Rate limiting
- User experience matters
- nginx simple but works find for small trafic
- token bucket
- HSet store update time and capacity
- System throttling
- How do you know existing user?
- One possible solution: save access token to redis
- introduce large amount of access
- Local bloom filter
- AddString() / IsStringExist()
- Save to redis and merge accross servers
- False positive is acceptable