memcached client: opening, closing and reusing connections
Asked Answered
S

1

11

I have been testing spymemcached and xmemcached clients. I have been trying to find answers in the projects documentation but it is very poor.

My questions are regarding opening, closing and reusing the connections. I found this in one document:

A client may just close the connection at any moment it no longer needs it. Note, however, that clients are encouraged to cache their connections rather than reopen them every time they need to store or retrieve data. Caching connections will eliminate the overhead associated with establishing a TCP connection".

Spymemcached doesn't provide a connection pool, so every time I create a MemcachedClient instance I am creating a new connection right? Then when should I close the connection? Should I provide the same instance to all the threads in my application or create a new one every time?

xmemcached does have a connection pool. In this case should I close connections I get from the pool?

Savona answered 30/12, 2011 at 19:20 Comment(0)
B
9

Spymemcached doesn't provide a connection pool, so every time I create a MemcachedClient instance I am creating a new connection right?

Yes, every time you create a new MemcachedClient object you create a new connection. Each connection appear asynchronous to the application so even having one connection will probably be enough for your application. Some people do however build a connection pool of MemcachedClients though.

Then when should I close the connection?

You shut down connections as soon as you no longer need to communicate with memcached. If you application is short lived you need to shutdown the connection in order to get the jvm to stop since MemcachedClient connections are daemon connections by default.

Should I provide the same instance to all the threads in my application or create a new one every time?

Use the same connection with multiple threads. Creating a new connection for each call will cause a significant performance drop because of the overhead of creating a TCP connection.

xmemcached does have a connection pool. In this case should I close connections I get from the pool?

I'm not familiar with xmemcached, but I would imagine you would only want to create a few (16 maybe) threads and share them with your application threads for the best performance.

Bombard answered 31/12, 2011 at 19:34 Comment(2)
Are there any benchmarks where I can see how many Memcache connections are needed in an application ?Athodyd
The last sentence sounds incomplete - what did you mean to say?Rodrigo

© 2022 - 2024 — McMap. All rights reserved.