当前位置:网站首页>Redis getting started complete tutorial: common exceptions on the client
Redis getting started complete tutorial: common exceptions on the client
2022-07-07 02:48:00 【Gu Ge academic】
In the process of using the client , Whether the client is used improperly or Redis The server appears
problem , The client will react with some exceptions . This section will analyze Jedis Common during use
Abnormal conditions of .
1. Unable to get connection from connection pool
JedisPool Medium Jedis The number of objects is limited , The default is 8 individual . It is assumed that the default
Recognition configuration , If there is 8 individual Jedis Object is occupied , And didn't return it , At this point, the caller also needs to start from
JedisPool Borrowing from China Jedis, We need to wait ( For example, the maxWaitMillis>0), Such as
Fruit in maxWaitMillis You still can't get Jedis Object will throw the following exception :
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource
from the pool
…
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.
java:449)
There's another situation , It's set up blockWhenExhausted=false, Then the caller finds
When there are no resources in the pool , An exception will be thrown immediately without waiting , The following exception is
blockWhenExhausted=false The effect of the :
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource
from the pool
…
Caused by: java.util.NoSuchElementException: Pool exhausted
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.
java:464)
For this question , What we need to focus on is why the connection pool has no resources , Cause no
There are many reasons for resources , May be the following :
· client : The setting of high parallel outgoing connection pool is too small , There is a shortage of Supply , So it will appear above
Error of , But normally, as long as the number of connections is greater than the default maximum (8 individual ) Just a little more , because
Under normal circumstances JedisPool as well as Jedis The processing efficiency is high enough .
· client : Connection pool is not used correctly , For example, no release , For example, the following code
in .
Definition JedisPool, Use the default connection pool configuration :
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379);
image JedisPool To borrow 8 Secondary connection , But no return operation was performed :
for (int i = 0; i < 8; i++) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.ping();
} catch (Exception e) {
e.printStackTrace();
}
}
When the caller borrows from the connection pool Jedis when ( Do the following ), Will throw an exception :
jedisPool.getResource().ping();
· client : There are slow query operations , These slow queries hold Jedis Objects will return faster than
slower , Cause the pool to be full .
· Server side : The client is normal , however Redis For some reasons, the server has caused customers
Blocking of end command execution , It will also cause the client to throw such an exception .
We can see that there are many reasons for this exception , Don't be fascinated by abnormal appearances
Puzzled , And there is no master key to solve all problems , Development and operation and maintenance can only be continuously strengthened for
Redis The understanding of the , Gradually find the problem .
2. Client read / write timeout
Jedis Calling Redis when , If there is a read-write timeout , The following exception will appear :
redis.clients.jedis.exceptions.JedisConnectionException:
java.net.SocketTimeoutException: Read timed out
There are also several reasons for this exception :
· The read-write timeout is set too short .
· The command itself is slow .
· The client and server networks are abnormal .
·Redis Self blocking .
3. Client connection timeout
Jedis Calling Redis when , If there is a connection timeout , The following exception will appear :
redis.clients.jedis.exceptions.JedisConnectionException:
java.net.SocketTimeoutException: connect timed out
There are also several reasons for this exception :
1) Connection timeout set too short , You can set it through the following code :
// millisecond
jedis.getClient().setConnectionTimeout(time);
2)Redis happening , cause tcp-backlog Is full , Cause new connection failure .
3) The client and server networks are abnormal .
4. Client buffer exception
Jedis Calling Redis when , If a client data flow exception occurs , The following differences will appear
often :
redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
There may be several reasons for this exception :
1) Output buffer full . For example, set the output buffer of the normal client to 1M1M60:
config set client-output-buffer-limit "normal 1048576 1048576 60 slave 268435456
67108864 60 pubsub 33554432 8388608 60"
If you use get Command to get a bigkey( for example 3M), This anomaly will appear .
2) A long idle connection is actively disconnected by the server , This question has been analyzed in detail in the last section
topic .
3) Abnormal concurrent reading and writing :Jedis Objects are operated concurrently by multiple threads at the same time , There may be
Above anomaly .
5.Lua The script is executing
If Redis Currently executing Lua Script , And more than lua-time-limit, here Jedis
call Redis when , You will receive the following exception . How to deal with such problems , In the 3 Chapter Lua Of
Section has been introduced , No more details here .
redis.clients.jedis.exceptions.JedisDataException: BUSY Redis is busy running a
script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.
6.Redis Loading persistent file
Jedis call Redis when , If Redis Loading persistent file , Then you will receive the following
abnormal :
redis.clients.jedis.exceptions.JedisDataException: LOADING Redis is loading the
dataset in memory
7.Redis Use more memory than maxmemory To configure
Jedis When writing , If Redis Memory used is greater than maxmemory Set up , Meeting
Received the following exception , At this point, you should adjust maxmemory And find the cause of memory growth :
redis.clients.jedis.exceptions.JedisDataException: OOM command not allowed when
used memory > 'maxmemory'.
8. The number of client connections is too large
If the number of client connections exceeds maxclients, The newly applied connection will appear the following differences
often :
redis.clients.jedis.exceptions.JedisDataException: ERR max number of clients reached
At this point, the new client connection executes any commands , The returned results are as follows :
127.0.0.1:6379> get hello
(error) ERR max number of clients reached
This problem may be tricky , Because it cannot be executed at this time Redis Order problem repair
complex , Generally speaking, it can be solved from two aspects :
· client : If maxclients If the parameter is not very small , The client connection number base of the application
This will not exceed maxclients, Generally speaking, it is because the application side is interested in Redis Improper use of the client
Yes . At this time, if the application side is a distributed structure , You can apply nodes through the offline part ( example
For example, nodes that occupy more connections ), bring Redis The number of connections dropped first . So that most
The node can operate normally , At this point, through the search program bug Or adjust maxclients Conduct problem
Repair .
· Server side : If the client cannot process at this time , And the current Redis High availability mode ( for example
Redis Sentinel and Redis Cluster), You can consider the current Redis Do failover .
There is no definite solution to this problem , But no matter from which aspect , Faulty
Rapid recovery is extremely important , Of course, it is more important to find the problem , Otherwise, after a period of time
The number of client connections will still exceed maxclients.
边栏推荐
猜你喜欢
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
用全连接+softmax对图片的feature进行分类
Redis入门完整教程:客户端管理
Niuke programming problem -- double pointer of 101 must be brushed
Hash table and full comments
NuScenes数据集关于Radar数据的统计
Linear list --- circular linked list
Draco - gltf model compression tool
如何设计好接口测试用例?教你几个小技巧,轻松稿定
Planning and design of double click hot standby layer 2 network based on ENSP firewall
随机推荐
电气工程及其自动化
MySQL --- 常用函数 - 字符串函数
widerperson数据集转化为YOLO格式
Remember the problem analysis of oom caused by a Jap query
Read fast RCNN in one article
基于ensp防火墙双击热备二层网络规划与设计
Leetcode:minimum_depth_of_binary_tree解决问题的方法
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
LeetCode 77:组合
Gee upgrade can realize one piece of run tasks
QPushButton-》函数精解
C语言练习题_1
导数、偏导数、方向导数
MMDetection3D加载毫米波雷达数据
Derivative, partial derivative, directional derivative
一文读懂Faster RCNN
C language exercises_ one
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
Niuke programming problem -- double pointer of 101 must be brushed
惯导标定国内外研究现状小结(删减版)