Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use Memcached for Java enterprise performance, Part 1: Architecture and setup

How is Memcached different from traditional Java caching?

  • Print
  • Feedback

Page 3 of 4

Memcached client logic

In its default configuration, the Memcached client uses very simple logic to select the server for a get or set operation. When you make a get() or set() call, the client takes the cache key and call its hashCode() method to get an integer such as 11. It then takes that number and divides it by number of available Memcached servers, say two. It then takes the value of the remainder, which is 1 in this case. The cache entry will go to Memcached server 1. This simple algorithm ensures that the Memcached client on each of your application servers always chooses the same server for a given cache key.

Installing Memcached

Memcached runs on Unix, Linux, Windows, and MacOSX. You can either download the Memcached source and compile it or you can download the binaries compiled by someone else and use them to install Memcached. Here I'll walk through the process of downloading the binaries for the platform of your choice; see Resources if you prefer to compile from source.

The following installation instructions are for a Windows XP 32-bit machine. See Resources for installation instructions for other platforms such as Linux. Also note that the sample code for this article was developed on a Windows XP 32-bit machine, though it should work on any other platform.

  1. Jellycan code has a modified version of Memcached that is easy and efficient to work with. Start here by downloading the win32 binary ZIP file
  2. Expand Memcached-<versionnumber>-win32-bin.zip on your hard disk. Note that all it contains is memcached.exe. Execute this file to start the Memcached server.
  3. Now execute memcached.exe -d install to register memcached.exe as a service. You'll be able use the Services console to start and stop the Memcached server.

CL start/stop

Try starting and stopping the Memcached server from command-line instead of from a services panel. Doing that will give you more flexibility to try out different command-line options and figure out the best possible configuration for your requirements.

When you execute the memcached.exe without any command-line options, by default the Memcached server will start up on port 11211 with 64 MB of memory. In some cases you might want to have more granular control of the configuration. For example, say port 11211 is used by some other process on your machine and you want the Memcached server to use port 12000; or if you were starting Memcached server in a QA or production environment you would want to give it more memory than the default 64 MB. In these cases you could use command-line options to customize the server's behavior. Executing the memcache.exe -help command will yield a complete list of command-line options like the ones shown in Figure 3.

Figure 3. Command-line options for Memcached server (click to enlarge)

Connect with Memcached via Telnet

After the Memcached server is started it listens on the port you've assigned it to. The Memcached client connects to the server on either the TCP or UDP port, sends commands and receives responses, and eventually closes the connection. (See Resources for details of the protocol the client uses to communicate with the server.)

  • Print
  • Feedback

Resources