Basic
Redis supports lots of data types:
- string
- list
- set
- hash (k-v map)
etc.
Examples:1
2
3
4redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> GET tutorialspoint
"redis"
Single Machine Redis
- Redis keeps a key-value db structure internally. Normally, it is stored in memory. But it also has persistence strategy:
- RDB: store the compress the k-v db into disk
- AOF: store change log of cmd
- a single machine redis can operate multiple dbs. (in a distributed mode, a redis can only operate one db)
- server logic:
- handle IO event (client request) and time event. It is event driven.
- Reactor pattern
- Single thread
Distributed Redis
- Support Master-Slave mode. (Master will sync up its data with slave)
- Use Sentinel will monitor Master health
- Each node in the cluster will maitain the current cluster state: including some health tag, shard-to-slot mapping
- Node(shard): responsible for a number of slots. Each node has only one db
- key send to node A
- node A check if the key is in its slots
- if no, return the target node
- if yes, execute the cmd (find the key’s value or insert the k-v into its db)
Other features
- Lua
- reduce network cost. We can do multiple cmds in the lua scripts so one request is only needed to do the same
- atomic in a lua. No other cmds will be executed in the middle of lua
- we can reuse the lua script in the client any time.
- Pub-Sub
- support simple regex pattern match to topics
- If subscribers are not online, the message will lost
Use cases
- find top K hot XXXX in a website
- find recent visited records
- sanity check of a request (i.e., if the user id is marched with some fields)
- statistic: i.e., count how many users are registered, how many visited, etc.