How to connection pool memcached in Java (spymemcached)
Asked Answered
I

3

11

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?

Inarch answered 21/5, 2012 at 19:40 Comment(0)
L
17

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.

Lottie answered 22/5, 2012 at 23:25 Comment(1)
Everything I needed to know. I was hoping the reconnection was encapsulated. Thanks muchh.Inarch
R
0

You can create an object pool initially.Then while performing set,delete or get operation you can borrow objects from the pool. MemcachedClient will automatically connect to the server once you invoke its constructor.

Rectifier answered 8/7, 2014 at 10:9 Comment(0)
X
0

Some people use DBCP for pooling the connections. You can look at 3levelmemcache project at github it has pooling.

Xiomaraxiong answered 18/8, 2014 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.