Table of contents
1. Concept and advantages and disadvantages:
I. Introduction
Recently, I encountered some Redis-related problems on some of the systems I was in charge of. I just talked about Cluster and Sharding in the circle of friends, and found that some places were rather vague. Considering that I also sorted out the Sentinel cluster mode before, I took advantage of A little effort to sort out some relevant information about Sharding.
There will be time to add it later in the Cluster mode.
2. Redis sharding cluster
1. Concept and advantages and disadvantages:
Single point of failure : The general approach is to realize the automatic failover of the fragment through the Sentinel mode of one master and N slaves. For the specific solution principle and construction, please refer to my other article: Redis Sentinel (Sentinel) modeExpansion problem : Generally, the capacity can only be expanded by restarting, but this method increases the difficulty of the operation and maintenance side in terms of key-value pair data migration, and the application layer needs to make configuration changes to support new instances. There is another way. The author of Redis recommends using a PreSharding method. I won’t introduce it here, and I’ll add it later.
2. Data skew problem
-
Balance: the hash values of different objects after hashing in the hash ring (that is, the hash family) can ensure that the distribution is as uniform as possible to ensure that the balance falls to different nodes;
-
Monotonicity: For some key-value pairs that have been assigned to specific nodes, even if a new node joins, it can be guaranteed that this part of the key-value pair is either mapped to the original node or mapped to a new node (that is, it will not be mapped to other old nodes);
-
Dispersion: I don’t really understand it yet, and I can’t translate it
-
Loadability: I don’t really understand it yet, and I can’t translate it
Other:1. Friends who are interested in papers related to the application of this algorithm on the cache system can refer to the information on the website of Columbia University: http://www.cs.columbia.edu/~asherman/papers/cachePaper.pdf2. Those who are interested in the original papers on the algorithm can refer to the information of Princeton University: https://www.cs.princeton.edu/courses/archive/fall09/cos518/papers/chash.pdf
3. Data loss problem
-
First of all, if consistent hashing is adopted, because there are many virtualized nodes mentioned above, even if the above situation occurs, the affected data range is relatively small, at least not the data of the entire physical node has to be checked. ;
-
Secondly, if we do the corresponding bottom-up processing at the application layer (that is, penetrate to the database to obtain and synchronize to the Redis node), because the corresponding data range is small, the traffic pressure to the server is not so great.
4. Application
<!--引入Jedis客户端-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
application.properties
#redis sharding instance config
redis_client_timeout=500
redis_one_host=192.168.32.101
redis_one_port=6379
redis_one_password=123
redis_two_host=192.168.32.102
redis_two_port=6380
redis_two_password=123
RedisConfiguration.java
@Configuration
public class RedisConfiguration {
//redis one host
@Value("${redis_one_host}")
private String redisOneHost;
//redis one port
@Value("${redis_one_port}")
private int redisOnePort;
//redis one password
@Value("${redis_one_password}")
private String redisOnePassword;
//redis two host
@Value("${redis_one_host}")
private String redisTwoHost;
//redis two port
@Value("${redis_one_port}")
private int redisTwoPort;
//redis two password
@Value("${redis_two_password}")
private String redisTwoPassword;
//redis client timeout
@Value("${redis_client_timeout}")
private int redisClientTimeout;
@Bean(name="redisPool")
public ShardedJedisPool createRedisPool() throws Exception {
//设置连接池的相关配置
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(5);
poolConfig.setMaxIdle(2);
poolConfig.setMaxWaitMillis(5000);
poolConfig.setTestOnBorrow(false);
poolConfig.setTestOnReturn(false);
//设置Redis信息
JedisShardInfo shardInfo1 = new JedisShardInfo(redisOneHost,redisOnePort, redisClientTimeout);
shardInfo1.setPassword(redisOnePassword);
JedisShardInfo shardInfo2 = new JedisShardInfo(redisTwoHost, redisTwoPort, redisClientTimeout);
shardInfo2.setPassword(redisTwoPassword);
//初始化ShardedJedisPool
List<JedisShardInfo> infoList = Arrays.asList(shardInfo1, shardInfo2);
ShardedJedisPool jedisPool = new ShardedJedisPool(poolConfig, infoList);
return jedisPool;
}
public static void main(String[] args){
ShardedJedis shardedJedis = null;
try{
RedisConfiguration redisConfiguration = new RedisConfiguration();
ShardedJedisPool shardedJedisPool = redisConfiguration.createRedisPool();
shardedJedis = shardedJedisPool.getResource();
shardedJedis.set("CSDN", "56");
shardedJedis.set("InfoQ","44");
shardedJedis.set("CNBlog","13");
shardedJedis.set("SegmentFault","22");
Client client1 = shardedJedis.getShard("CSDN").getClient();
Client client2 = shardedJedis.getShard("InfoQ").getClient();
Client client3 = shardedJedis.getShard("CNBlog").getClient();
Client client4 = shardedJedis.getShard("SegmentFault").getClient();
System.out.println("CSDN 位于实例:" + client1.getHost() + "|" + client1.getPort());
System.out.println("InfoQ 位于实例:" + client1.getHost() + "|" + client1.getPort());
System.out.println("CNBlog 位于实例:" + client1.getHost() + "|" + client1.getPort());
System.out.println("SegmentFault 位于实例:" + client1.getHost() + "|" + client1.getPort());
}catch(Exception e){
e.printStackTrace();
}finally {
shardedJedis.close();
}
}
}
According to the log printed out after running, these values are stored in different Redis instances , but how to allocate them to different shards when the client uses them is determined by the consistent hash algorithm implemented by Jedis; Of course, the default algorithm it supports is the 64-bit MURMUR_HASH algorithm, and also supports the MD5 hash algorithm.
"C:\Program Files\Java\jdk1.8.0_102\bin\java"...
CSDN is located in instance: 192.168.32.101|6379
InfoQ is located at instance: 192.168.32.102|6380
CNBlog is located at instance: 192.168.32.101|6379
SegmentFault at instance: 192.168.32.102|6380
Three, afterword
4. Reference