Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connection pool memcached in Java (spymemcached)

The API I'm using, spymemcached, basically gives

MemcachedClient memc = new MemCachedClient(new InetSocketAddress("host", port));

It doesn't really give me a connect() or isConnected() function. The API offers a DefaultConnectionFactory but at first look at the code it doesn't look like it manages a connection pool. Does anyone know how to do this in spymemcached or in another Java memcached library?

More generally - what's the most "moral" way to make my application tolerant against loss of connection?

like image 301
djechlin Avatar asked May 21 '12 19:05

djechlin


1 Answers

When you call the MemcachedClient constructor it automatically connects to your memcached server. There is no connect() or isConnected() method. If you lose the connection with Spymemcached it will try to reconnect for you. Also, the DefaultConnectionFactory is meant to be used to specify special connection attributes (e.g. hashing method and failure mode). If you want to use a connection factory then you need to use the MemcachedClient constructor that takes a ConnectionFactory and a List<InetSocketAddress>.

Spymemcached uses a single IO thread, but acts like a multithreaded client. With one thread for example, you can do up to 50k ops per second. If you want to create a thread pool then you will have to do it in your user application.

More generally - what's the most "moral" way to make my application tolerant against loss of connection?

Like I mentioned above, Spymemcached will attempt to reconnect if it loses connection to the server. This process will usually take around 17ms. Most people that use the client do however create a thread pool in their application code.

like image 147
mikewied Avatar answered Sep 17 '22 23:09

mikewied