Using memcached to improve Java Enterprise Application performance: Architecture and Settings

Source: Internet
Author: User
Tags memcached

Memcached was developed by Danga Interactive. Used to improve the performance of livejournal.com sites.

Memcached distributed architecture supports a wide range of social networking applications, Twitter, Facebook and Wikipedia.

In the next two sections of the tutorial, Sunil Patil describes the memcached distributed hash table schema and leverages it to help you do data caching for data-driven Java enterprise applications.

This article describes how to use memcached to enhance the performance of Java enterprise applications.

First, a general overview of the traditional Java caching framework. And make a comparison with memcached. Of course. Memcached will also be installed on your local machine, how to interact with memcached via Telnet.

Next, create a "Hello Memcached" javaclient program. You will learn how to use memcached to reduce database server load and cache dynamically generated page tags. Finally, consider doing some advanced optimization configuration for spymemcachedclient.

Memcached and Java Cache Architecture Overview

The Java caching framework, like Ehcache and Oscache, is essentially a HashMap object that exists in the application code. Whenever a new object is added to the cache, it is saved in your app's memory. When you save a small amount of data. There is no problem with this strategy, but it is problematic to cache more than GB of data. Memcachedserver's designers use a distributed architecture that is easy to extend, so. can use memcached to do massive data cache.

The memcached architecture consists of two parts. The first is a memcachedserver software with its own process. If you want to expand your application, you can execute Memcachedserver software on other machines. Memcachedserver software instances are independent of each other. The second part of the memcached system is memcachedclient, which knows exactly the existence of each server. The client is responsible for getting the cache input to the appropriate server, as well as storing or getting the cache entry-this process, which I will discuss in detail later.

Assuming you've developed Java EE network applications before, you must have used Java open source caching frameworks such as Ehcache or Oscache. You may have used Dynacache or JBoss cache as part of the application server.

Before we practice this tutorial ourselves, it is important to clarify the differences between memcached and those of traditional Java caching frameworks.

Using the traditional Java cache

Whether you choose Open Source or a business solution, using a traditional Java cache architecture is very easy.

Use an open-source framework like Ehcache or Oscache. You need to download a binary file. Add the necessary jar files to your app classpath. The same, you need to create the configuration file, configure the cache, swap the size of the partition. Because the caching framework needs to be bound to software. The cache framework is typically bound to the application server, so there is no need to download any additional jar files.

Figure 1 The traditional Java cache architecture

After you have joined the caching framework for your application, get and set the cache entry (entry) in the CacheManager object by creating the.

Such The CacheManager created by your app and the cache framework will be executed on the same JVM.

Each time a cache entry is added, this object is added to a hash table maintained by the caching framework.

Once your application server software executes on multiple nodes, you may need to support distributed caching. In the distributed cache system. Once an object is added to the AppServer1, this object becomes available on both AppServer2 and AppServer3. The traditional Java cache uses replication (replication) to implement distributed caching, which means that when you add a cache entry for AppServer1, the entry is actively copied to other application servers on the system itself.

Finally, the entry will be available in all sites.

Using memcached

To use memcached for caching, you must download and install the Memcachedserver software on your platform. Once the Memcachedserver is installed successfully. It is invoked via TCP or Udpport listener cache.

Figure 2 memcached Architecture

Next, download a javamemcachedclient and add the Clientjar file to your app.

It then creates a Memcachedclient object that can invoke its method to get and set the cache entry.

Once an object is added to the cache. Memcachedclient gets the object, serializes it, and sends a byte array to the memcached server store. Then. The cache object may be garbage collected by the JVM that the application executes.

When you need to cache an object, you can call Memcachedclient's Get () method. The client gets the GET request, serializes it, and passes the GET request to memcachedserver. Memcachedserver the request to find the object from the cache. Assume that this object exists. The server will return this byte array to the client. The client receives an array of bytes, deserializes and creates an object to return to your app.

Even if your app is running on more than one application server. All applications can point to the same memcachedserver, which gets and sets the cache entry.

If you had more than one memcachedserver,server, you wouldn't know each other.

Therefore, you need to configure Memcachedclient. So that it can know all the memcachedserver. For example, the application creates a Java object in AppServer1, and then calls the memcached's set () method, Memcachedclient finds a memcachedserver to hold the entry. Then it only communicates with the memcachedserver.

Same, once the code that exists in AppServer2 or APPSERVER3 attempts to fetch a certain entry. Memcachedclient first finds out which server stores this entry and then communicates only with this server.

Memcachedclient logic

In the default state. Memcachedclient uses easy logic to select the server for Get or set operations.

Once get () or set () is called, the client gets the cache key (key) call Hashcode () method to get the integer value, for example 11.

This number is then divided by the number of memcachedserver available (for example, 2), and the remainder in this example is 1. The cache entry will point to Memcachedserver1. This simple calculation ensures that the memcachedclient that the application server resides in chooses the same server for the given cache key.

memcached Installation

Memcached can be executed on UNIX, Linux, Windows, and MacOSX. You can download memcached source code compilation, or download the compiled binary files directly to install memcached.

Here I will show you the installation process for downloading binaries for a specific platform. Suppose you are more inclined to compile. Please see here.

The following installation instructions are for Windows XP 32-bit machines. If the platform is Linux and other platforms, see here. Note The case code for this article was developed on a Windows XP 32-bit machine, but could be executed on other platforms.

    1. Jellycan code is a memcached revision number that is easier to use and more efficient. Let's start by downloading the Win32 binary compressed file.
    2. Unzip the Memcached-<versionnumber>-win32-bin.zip, and note that it includes Memcached.exe. Run this file to finish server Setup.
    3. Using the Memcached.exe-d install Memcached.exe as a system service, you can turn memcachedserver on or off in the service console.

When you execute Memcached.exe in the default state. The Memcachedserver consumes 64 megabytes of memory by default and listens for 11211port. In some cases, you might want to do some finer-grained control. For example, port11211 is occupied by other processes in this machine, you want memcached to be able to monitor port12000, or if you want to build memcachedserver in a quality assurance or production environment, you need more than 64 megabytes of default memory.

You can customize the server behavior with command-line parameters. The Execute memcache.exe-help command gets all the command-line options, such as those seen in 3.

Figure 3 memcachedserver command-line Options

Interacting with memcached via Telnet

Once memcachedserver starts listening to the port,memcachedclient you specify, you can connect to it via TCP or Udpport, send commands or accept responses, and finally close the connection.

There are many ways to connect memcachedserver, and I will use the Javaclient connection in the second part of this tutorial, and you will be able to use simple APIs to store or retrieve objects from the cache. Or you can use Telnetclient to connect directly to the server. It is important to understand how to use telnetclient to interact with Memcachedserver to debug javaclient. So let's start here.

Telnet command

First you need to connect Memcachedserver with Telnetclient. On the WindowsXP platform, assume that Memcachedserver also executes on this machine and listens to port11211 by default, only to perform telnet localhost 11211. The following commands are important for Telnet management memcached:

    • set Add a new project to the cache , the format is Set <keyName> <flags> <expiryTime> <bytes>, and you can save the typed value to the next line. If you don't want the cache entry to expire, you can enter 0.
    • get returns the value of the cache key , call get <keyName> get the value of KeyName.
    • add Add a new key , provided that the key does not exist before. For example, add <keyName> <flags> <expiryTime> <bytes>.
    • replace Overrides a key Value, provided that the key already exists. For example, replace <keyName> <flags> <expiryTime> <bytes>.

    • delete Delete the cache entry for a key, call delete <keyName> Delete the value of KeyName.

Figure 4 shows a case of interaction with Memcachedserver via Telnet. As you have shown, Memcachedserver will respond to every command, such as stored, not_stored, etc.

Figure 4 Telnetclient and memcachedserver interaction case

The first part concludes

To this. We briefly discussed the memcached distributed framework and many traditional Java caching systems.

Memcached is installed in your development environment, and memcached is connected via Telnet.

In the next tutorial, we will invoke the javaclientsypmemcached command to establish a distributed caching scheme for a Java Demo sample application. In this process. You'll learn a lot about other information about memcached and how to improve your Java EE application performance.

Using memcached to improve Java Enterprise Application performance: Architecture and Settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.