Overview of Redis
- The word Redis means Remote Dictionary Server
- Initial release in 2009
- It is an advanced key-value store or a data structure
- Run entirely in memory
- All data is kept in memory
- Quick data access since it is maintained in memory
- Data can be backed up to disk periodically
- Single threaded server
- Extensible via Lua scripts
- Able to replicate data between servers
- Clustering also available
About Redis
Redis is ana open source, BSD licensed, advanced key-value cache and store. It is ofen referred to as a data structure server since keys can contain strings, hashesm lists, sets, sorted sets, bitmaps and hyperloglogs.
Redis:
- key-vaule cache and store
- in mermory
- 持久化
- 主从(借助sentinel实现一定意义上的高可用)
Clustering(分布式)
Redis is an in-memory but persistent on disk database
- 1 Million small Key->String value pairs use ~ 100MB of memory
- Single threaded - but CPU should not be the bottleneck
- Average Linux system can deliver even 500k requests per second
- Limit is likely the available memory in your system
- max 232 keys
Persistence
- Snapshotting
- Data is asynchronously transferred from memory to disk
- AOF(Append Only File)
- Each modifying operation is written to a file
- Can recreate data store by replaying operations
- Without interrupting service, will rebuild AOF as the shortest sequence of commands needed to rebuild the current dataset in memory
Replication
- Redis supports master-slave replication
- Master-slave replication can be chained
- Be careful:
- Slaves are writeable
- Potential for data inconsistency
- Fully compatible with Pub/Sub features
Diffences to Memcached
- Memcached is a “distried memory object caching system”
- Redis persists data to disk eventually
- Memcached is an LRU cache
- Redis has different data types and more features
- Memcached is multithreaded
- Similar speed
Redis优势
- 丰富的操作
- 内建relication和cluster
- 就地跟新(in-place update)操作
- 支持持久化
- 避免雪崩效应
Memcached优势
- 多线程
- 多核cpu
- 更少的阻塞操作
- 更少的内存开销
- 更少的内存分配压力
- 可能有更少的内存碎片
存储系统有三类
- RDBMS
- Key-value
- Memcached, Redis
- Column family NoSQL
- Cassandra, HBase
- Documentation NoSQL
- MongoDB
- Graph NoSQL
- Neo4j
- NoSQL
- NewSQL
Redis组件
- redis-server
- redis-cli
- Command line interface
- redis-benchmark
- Benchmarking utility
- redis-check-dump & redis-check-aof
- Corrupted RDB/AOF files utilitues
安装配置
/etc/redis.conf
守护进程 端口: 6379/tcp
等待队列,缓存新的请求: tcp-backlog 511
unixsocket /tmp/redis.sock
sock 在内存中直接交换数据,而不再经过Tcp/Ip栈进行封装
快照频率的
save 900 1 #900s一个键发生变化
save 300 10 #300s10个键发生变化
save 60 10000
主从服务器配置: saveof
Keys
- Arbitrary ASCII strings
- Define some format convention and adhere to it
- Key length matters
- Multiple name spaces are available
- Separate DBs indexed by an integer value
- SELECT command
- Multiples DBs vs Single DB + key prefixes
- Keys can expire automatically
commands
help @STRING
help @list
SET count 0
GET count
# 设置过期时间
setex count 100 60
# list
lpush list1 'hello1'
rpush list1 'hello2'
lpop list1
# set
sadd set1 1 2 3 4
sadd set2 1 2 3 4
sinter set1 set2
sunion set1 set2
# sorted_set
help @sorted_set