Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

python - Slow requests whenever Psycopg2 reconnects to DB

I'm currently facing a very strange issue. I did some optimizations in my queries, which improved quite a lot the overall performance for my Django application's GET requests. However, I'm still facing a few very slow ones (1000ms or more). Checking on Elastic APM, I noticed that for all those cases, there was a DB reconnection. While it's obvious those requests would take more time, it's still takes WAY more time than the amount required for the reconnection itself.

I have set the DB_CONN_MAX_AGE to None, therefore the connections themselves shouldn't be closed at all. Which makes me think the reason for the disconnection itself could also indicate the cause for this.

The blue bars represent the amount of time a given transaction took. The total amount of time for this particular request was 1599ms. The DB reconnection took 100ms, the the queries about ~20ms. Adding those up, gives a total time of 120ms.

enter image description here

I'm a bit clueless how to find out where the rest of the 1479ms. I did some load tests locally, but couldn't reproduce this particular issue. Of course is serializations, middlewares, authentication and other things that might add up some time to requests, but not nearly the 1479ms shown here. It certainly related to the DB connection itself, or better yet, something that happens before that. But I'm not sure how to diagnose it. Specially being unable to reproduce this locally.

I'm open to any ideas that could lead to more information on how to solve this problem. Or maybe someone had a similar experience and could share it with me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...