3

Question:- Does java client need to worry about multiple servers ?

Meaning:- I have given two servers in memcached client, but when i set or get a key from cache, do i need to provide any server related info to it or memcache itself takes care of it??

My knowledge:- Memcache itself takes care due to consistent hashing.

but does spymemcached 2.8.0 provides consistent hashing???

Anil Kumar
  • 389
  • 2
  • 5
  • 23

2 Answers2

5

Memcached servers are pooling servers. Meaning that you define a pool (a list) of servers and when the Java client attempts a write it writes towards the pool.

It's the client's job to decide which server from the pool will receive and store the value and how it will retrieve the value from that pool.

Basically this allows you to start with one Memcached server (possibly on the same machine) and if push comes to shove you can add a few dozen more servers to the pool without touching the application code.

Since the client is responsible of distributing data across the pool of servers (the client has to choose right memcached server to store/fetch data) there are few distribution algorithms.

One of the simplest is modula. This algorithm distributes keys depending on the amount of memcached servers in pool. If the number of servers in the pool changes, the client won't be able to the find stored data, there will be cache misses. In such case it's better to use consistent hashing.

Most popular java memcached clients spymemached and xmemcached support consistent hashing.

In some use cases instead of directly using the memcached client, caching can be added to a spring application through AOP (interceptors) using simple-spring-memcached or Spring 3.1 Cache Abstraction. Spring Cache currently doesn't support memcached but simple-spring-memcached provides such integration in snapshot build and upcoming 3.0.0 release.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • 1
    that means that we just set a key and get it back, we dont need to worry about the servers on which it was stored... m i right?? – Anil Kumar Jun 20 '12 at 09:20
  • Exactly! When you create an instance of the MemcachedClient, the client object is dedicated to one pool and manages communication to all servers from the pool. You might want/ need to use multiple pools in which case you need separate instances of the MemcachedClient object. – Mihai Stancu Jun 20 '12 at 09:23
  • Since we're on the topic, Memcached has no redundancy, multiple servers means you can store as much data as the sum of all allocatable space on all the servers in the pool. There is no backup in case of failure. There are some commercial solutions based on Memcached that implement redundancy as well as alternatives such as Redis. – Mihai Stancu Jun 20 '12 at 09:25
  • @Mihai... does spymemcached uses consistent hashing or modulo hash?? – Anil Kumar Jun 20 '12 at 09:26
  • @Hellexx Sorry I'm not a Java dev. and I haven't used spymemcached so idk. – Mihai Stancu Jun 20 '12 at 09:31
  • @Hellexx both spymemcached and xmemcached clients support consistent hashing – ragnor Jul 06 '12 at 17:25
  • @ragnor this is an old post, as such we should be concerned with how future visitors will see it and how useful it is to them. Would you like to edit the post to mention your confirmation? I'll accept the edits. – Mihai Stancu Jul 06 '12 at 18:54
0

MemCached server will manage to store and retrieve key/value by itself. While storing using hash generate the key and store it. While retrieve again hash the given key and find on which server it has been stored and then fetch it, this will take some time.

Instead, there is one approach which can be used for storing and retrieving.

Create one HashMap and store the key with server address as value. Now next time if the same key needs to get then instead of searching, you will get the server address directly from the HashMap and you needs to fetch value from there only. Hence you can save the searching time of MemCahce server.

Hope you understand what i mean.

Manoj Kathiriya
  • 3,366
  • 5
  • 19
  • 19