yogk
yogk

Reputation: 381

What does persistent connection in Elasticsearch Python API mean?

I am using the Python Elasticsearch API to interact with ES in my application. Currently, as soon as the app gets a user request, it does esclient = Elasticsearch("127.0.0.1") and then uses this esclient to search for required data. But I recently read that ES has persistent connections. So,

  1. Should I save the esclient somewhere and reuse it? If yes, how do I do that?
  2. Would there be some resource leak if I forget about esclient after the request and open a new one next time? If yes, how do I cure this/ close the open connection?

Also, same questions for the memcached/ Redis python APIs. I do client = Client("127.0.0.1") multiple times during each user request.

Sorry, might seem like a stupid question but I am a bit confused.

Thanks-in-advance!

Upvotes: 6

Views: 1274

Answers (1)

Akhil Mathew
Akhil Mathew

Reputation: 1659

For elasticsearch python client you should just have one instance and keep reusing it.

If you create new ones there will be some connections left hanging, but also both your app and elasticsearch itself will be paying the overhead of creating and maintaining additional connections, which is not good.

If it's a Django or Flask application, you can create an elasticsearch specific resource file and create a connection as follows and reuse it everywhere.

ESCLIENT = Elasticsearch()

Import ESCLIENT everywhere and reuse.

Upvotes: 2

Related Questions