Redis architecture
Implemented in C, the performance mainly lies in how OS schedules each process and the cost spent in network communication and sockets reading/writing system call.
Normal command
1.bgsave:save db in background,The OK code is immediately returned. Redis forks, the parent continues to serve the clients, the child saves the DB on disk then exits.
2.client kill [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [ADDR ip:port] [SKIPME yes/no].
3.client list:list all client connection
4.client pause:stop processing commands from client for some time.
5.client getname:get name of current client connection/client setname:set name of current connection
6.cluster nodes:集群模式nodes配置、
7.config get params:get configuration of certain configuration.
8.get key:get value of key/append key value:键值插入value
9.debug object [key]:get debug information about a key
10.exists [key] determine if it exists
11.exec:execute all commands queued in a transaction and restores the connection state to normal.
12.MULTI:Marks the start of a transaction block. Subsequent commands will be queued for atomic execution using EXEC.
13.save synchnrously save all keys to index.
14.TTL [key]:get the time for a key to live.
Piplining
Redis is a TCP server using the client-server model and what is called a Request/Response protocol.
This means that usually a request is accomplished with the following steps:
The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.
The server processes the command and sends the response back to the client.
the server & client may not live in the same mahcine.(many hop)
Whatever the network latency is, there is a time for the packets to travel from the client to the server, and back from the server to the client to carry the reply.
How to reduce RTT in the network, thus improve the throughput in DB?
This way it is possible to send multiple commands to the server without waiting for the replies at all, and finally read the replies in a single step.server is forced to queue the replies using memory.
pipling save time in:1.socket I/O,read/write syscall with context switch2.RTT time is reduced with multiple commands batched together.
具体方法是用特定的ClientAPI实现pipling 请求batch.
Pub/sub mechanism
SUBSCRIBE, UNSUBSCRIBE and PUBLISH implement the Publish/Subscribe messaging paradigm where (citing Wikipedia) senders (publishers) are not programmed to send their messages to specific receivers (subscribers). Rather, published messages are characterized into channels, without knowledge of what (if any) subscribers there may be. Subscribers express interest in one or more channels, and only receive messages that are of interest, without knowledge of what (if any) publishers there are. This decoupling of publishers and subscribers can allow for greater scalability and a more dynamic network topology.
消息分类,不发送给特定人群。subscribe foo bar
listen to certain channel.publish channel message
publish message to certain channel
Expires mechanism
it’s the core of which it can be used as cache, set a key with some time such that it will get expired.volatile using persist [key] to clear the timeout.
Using Redis as an LRU cache
When Redis is used as a cache, often it is handy to let it automatically evict old data as you add new one. This behavior is very well known in the community of developers, since it is the default behavior of the popular memcached system.
in redis.conf:configure with maxmemory 100mb
.maxmemory-policy
:volatile-lru
: evict keys by trying to remove the less recently used (LRU) keys first, but only among keys that have an expire set, in order to make space for the new data added.
Transaction
multi
with multi operation,exec
will start execution of transaction. discard
will flush the transaction queue and exit. Atomicity, executed/or not.
No roll back for redis, when failed may still get some operation executed.Redis is internally simplified and faster because it does not need the ability to roll back.主要目的就是快!!
Implement self modules
in C, and load it later can extend the command supported. Module load ….so. //in conf file:loadmodule /path/file.so.
Disgtributed locks with
already have Java implementation
Regular command
quit:关闭连接(connection)
auth:简单密码认证
2)持久化
save:将数据同步保存到磁盘
bgsave:将数据异步保存到磁盘
lastsave:返回上次成功将数据保存到磁盘的Unix时戳
shundown:将数据同步保存到磁盘,然后关闭服务
3)Key value Operation
exists(key):确认一个key是否存在
del(key):删除一个key
type(key):返回值的类型
keys(pattern):返回满足给定pattern的所有key
randomkey:随机返回key空间的一个
keyrename(oldname, newname):重命名key
dbsize:返回当前数据库中key的数目
expire:设定一个key的活动时间(s)
ttl:获得一个key的活动时间
select(index):按索引查询
move(key, dbindex):移动当前数据库中的key到dbindex数据库
flushdb:删除当前选择数据库中的所有key
flushall:删除所有数据库中的所有key
Introduction
Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到硬盘上进行保存。因为是纯内存操作,Redis的性能非常出色,Redis最大的魅力是支持保存List链表和Set集合的数据结构,而且还支持对List进行各种操作,例如从List两端push和pop数据,取 List区间,排序等等,对Set支持各种集合的并集交集操作,此外单个value的最大限制是1GB,不像memcached只能保存1MB的数据,Redis可以用来实现很多有用的功能,比方说用他的List来做FIFO双向链表,实现一个轻量级的高性能消息队列服务,用他的Set可以做高性能的tag系统等等。另外Redis也可以对存入的Key-Value设置expire时间,因此也可以被当作一个功能加强版的memcached来用。
Example
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class testJedis{
private Jedis jedis;
jedis = new Jedis(“127.0.0.1”, 6379);
jedis.set(“name”, “whw”);
jedis.append(“name”, “ is my name;”);
jedis.del(“name”);
//所有字段的key-value类型全部要用字符串的类型保存
//Map
jedis.hmset(“user”, map);
jedis.hdel(“user”, “age”);//删除HashMap中的某个键值对
// 通过keys获取key的值
Iterator
//可用连接实例的最大数目,默认为8;
JedisPoolConfig config = new JedisPoolConfig();
jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH);
Jedis jedis = jedisPool.getResource();
//通常对JedisPool使用单例模式,final class JedisPool{
JedisPool jedispool = null;
public static Jedis getJedisPool() {
return jedispool.getReosource();
}
}
}