Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of High concurrency in Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces Redis how to solve the problem of high concurrency, the article is very detailed, has a certain reference value, interested friends must read it!

The details are as follows:

Why redis has the problem of high concurrency

Redis's background determines

Redis is a single-threaded nosql database based on key-value, and the data can be persisted. Because of single thread, redis itself does not have the concept of lock, and there is no competition between multiple client connections, but there will be problems when using clients such as jedis to access redis concurrently. Problems such as connection timeout, data conversion errors, blocking, and client closing of connections occur, all of which are caused by chaotic client connections.

At the same time, the nature of single-threading determines that operations on the same key with high concurrency will be queued up, which may cause subsequent requests to time out if the concurrency is large.

When accessing redis remotely, the problem of delayed return of high concurrent access is caused by network and other reasons.

Solution.

1. The connection is pooled on the client side, and the internal lock synchronized is used for the client read and write Redis operation.

two。 From the server point of view, the locking mechanism is realized by using setnx redirection. I am not clear about how this method will be used in the actual environment.

Analysis of Common errors in jedis

Exception code 1:

The copy code is as follows:

Redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

Problem analysis: redis.clients.util.Pool.getResource returns an available redis connection from the JedisPool pool. There are several important parameters for the configuration of available connections in JedisPool as follows:

1.MaxActive: the maximum number of available connection instances. There is no limit when the number is negative.

2.MaxIdle: the maximum number of idle connection instances. There is no limit when it is negative.

3.MaxWait: the timeout for waiting to get a link.

That is, when there is no active/idle connection in the connection pool, the maxWait time will be waited, and if the wait timeout does not have an available connection, a Could not get a resource from the pool exception will be thrown. So in order to avoid such mistakes,

We should set the values of these three parameters reasonably according to the actual situation of the program, and at the same time, we should also handle this exception reasonably in the program method of obtaining a connection, when there is no connection available, it may be a better choice to wait a while to get it.

Exception code 2:

The copy code is as follows:

Redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out

When you encounter this exception, you may be confused. Redis is a memory operation with a speed of milliseconds. It will make people feel confused when there is a second-level operation on redis operations. However, as mentioned at the beginning of this article, it is not surprising that redis times out in some special cases. When initializing JedisPool, jedis should reasonably set connection pool parameters through redis.clients.jedis.JedisPoolConfig according to the actual situation, and set the timeout for socket to read input InputStream through redisPool construction method.

`pool = new JedisPool (config, host, port, 100000) `

The fourth parameter is time out, in milliseconds. You can avoid the problem by setting this value reasonably. But this does not completely solve the problem of overtime. In some cases of high concurrency, the delayed return time can even reach the extreme of tens of seconds. This problem should be solved at the code level that redis single thread itself does not support locks, and concurrency operations on the same key will lead to competition.

These are all the contents of the article "how to solve the problem of High concurrency in Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report