One of the newest features of the spymemcached client library is called vbucket support. The vBucket algorithm allows Membase to automatically connect clients to the data they need even if servers are being rearranged, taken offline, and so on. It is an indirection layer on top of buckets in Membase that are transparent to clients. According to an article written by Dustin Sallings the basic requirements that led to the development of vbuckets are as follows:
Never service a request on the wrong server.
Allow scaling up and down at will.
Servers refuse commands that they should not service, but
Servers still do not know about each other.
We can hand data sets from one server another atomically, but
There are no temporal constraints.
Consistency is guaranteed.
Absolutely no network overhead is introduced in the normal case.
If you want to know more about vbuckets, be sure to read Dustin's article.
You have been using a pre-release version of the spymemcached client library to complete the rest of this tutorial, and inside it a new NodeLocator implementation exists that uses vbucket hashing. It is designed to automatically adjust to the changing topology of your Membase cluster at runtime.
All you need to start using this functionality is to use a new
MemcachedClient constructor that allows you to
pass in a list of base URIs, and the bucket name and password.
Replace the first three lines of the
method you wrote earlier with the following lines:
URI base = new URI(String.format("http://%s:8091/pools", serverAddress); List<URI> baseURIs = new List<URI>(); baseURIs.add(base); client = new MemcachedClient(baseURIs, "private", "private", "private");
Compile and run the application:
$ mvn assembly:assembly $ java -jar target\membasetutorial-exe.jar serverhost
Replace serverhost with the name or IP address of your server, you won't need the port this time. You will see something like the following output:
INFO: Connection state changed for sun.nio.ch.SelectionKeyImpl@16930e2 Connection established with acme-ubuntu-11/10.0.0.57:11210 Reconnected count: 1 19-May-2011 10:09:25 PM net.spy.memcached.auth.AuthThread$1 receivedStatus INFO: Authenticated to acme-win7/10.0.0.33:11210 You are user 7. 19-May-2011 10:09:25 PM net.spy.memcached.auth.AuthThread$1 receivedStatus INFO: Authenticated to acme-ubuntu-11/10.0.0.57:11210 Registration succeeded. There are currently 4 connected. Enter text, or /who to see user list, or /quit to exit.
You can see that it connects to the server and automatically loads the list of all Membase servers, connects to them and authenticates. It uses the vbucket algorithm automatically, and no code changes to the application will be required.