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

Source: Internet
Author: User
Tags delete cache website performance

Memcached was developed by Danga Interactive to enhance livejournal.com website performance. 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, we have a general overview of the traditional Java caching framework and make a comparison with memcached. Of course, you'll also install memcached on your local machine, and how to interact with memcached via Telnet. Next, create a "Hello Memcached" Java client program. You will learn how to use memcached to reduce the load on the database server and cache dynamically generated page tags. Finally, consider making some advanced optimizations for the spymemcached client.

Memcached and Java Cache Architecture Overview

Java caching frameworks such as Ehcache and Oscache are essentially hashmap objects that exist in the application code. Whenever you add a new object to the cache, it is saved in your app's memory. This strategy is not a problem when saving a small amount of data, but it is problematic to cache more than GB of data. memcached server designers use a distributed architecture that is easy to scale, so you can use memcached to do massive data caching.

The memcached schema consists of two parts. The first is a memcached server software with its own process. If you want to expand your application, you can run Memcached Server software on other machines. memcached Server Software instances are independent of each other. The second part of the memcached system is the memcached client, which knows exactly where each server exists. The client is responsible for getting the cache input corresponding to the server, as well as storing or getting the cache input-this process, I will do a detailed discussion later.

If you have ever developed a Java EE network application, you must have used Java open source caching frameworks such as Ehcache or Oscache. You may have used a commercial caching framework such as Dynacache or JBoss cache as part of the application server. Before we practice this tutorial ourselves, it is important to understand 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, it's easy to use a traditional Java caching architecture. Using an open-source framework like Ehcache or oscache, you need to download binaries and add the necessary jar files to your app classpath. Similarly, you need to create a configuration file, configure the cache, swap the size of the partition. Because the caching framework needs to be bound to software, the caching framework is typically bound to the application server, so no additional jar files need to be downloaded.

Figure 1 The traditional Java cache architecture

After you add a caching framework for your application, you get and set the cache entry (entry) in it by creating a CacheManager object. This way, the CacheManager created by your app and the cache framework will run on the same JVM. Each time the cache entry is incremented, this object is added to the cache framework to maintain a certain class of hash tables.

Once your application server software is running on multiple nodes, you may need to support distributed caching. In a distributed cache system, once an object has been 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 automatically copied to the other application servers on the system. Eventually, the entry will be available in all sites.

Using memcached

To use memcached for caching, you must download and install the memcached Server Software on your platform. Once the memcached server is successfully installed, it listens for cache calls over TCP or UDP ports.

Figure 2 memcached Architecture

Next, download a javamemcached client and add the client jar file to your app. Then create a memcached client object, and you can call its methods to get and set the cache entry. Once an object is added to the cache, the Memcached client obtains the object, serializes it, and sends a byte array to the memcached server store. At this point, the cache object may be garbage collected by the JVM to which the application runs.

When you need to cache an object, you can call the Memcached client's Get () method. The client gets the GET request, serializes it, and passes the GET request to the memcached server. The memcached server looks for this object from the cache through the request. If this object is stored, the server returns the 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 runs on more than one application server, all apps can point to the same memcached server, which gets and sets the cache entry. If you have more than one memcached server, the server will not know about each other. Therefore, you need to configure the memcached client so that it can know all the memcached servers. For example, the application creates a Java object in AppServer1, and then calls the memcached's set () method, memcached the client to find a memcached server to hold the entry. It then communicates only with this memcached server. Similarly, once the code that exists in AppServer2 or APPSERVER3 attempts to fetch a certain entry, the Memcached client first finds out which server stores the entry and then communicates only with the server.

memcached Client Logic

By default, the Memcached client uses a very simple 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, such as 11. This number is then divided by the number of memcached servers available (for example, 2), and the remainder in this example is 1. The cache entry will point to Memcached server 1. This simple calculation ensures that the memcached client on which the application server resides chooses the same server for the given cache key.

memcached Installation

The memcached can run 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. If you are more inclined to compile, 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 can be run on other platforms.

    1. Jellycan code is a memcached revision, easier to use and more efficient, we start by downloading Win32 binary compressed files.
    2. Unzip the Memcached-<versionnumber>-win32-bin.zip, note that it contains Memcached.exe, execute this file to complete the server Setup.
    3. Using memcached.exe-d install to register Memcached.exe as a system service, you can start or stop the memcached server on the service console.

When you execute the memcached.exe,memcached by default, the server consumes 64 megabytes of memory and listens on 11211 ports. In some cases, you might want to do some finer-grained control. For example, Port 11211 is occupied by other processes in this machine, you want memcached to be able to listen on port 12000, or if you want to build memcached servers in a quality assurance or production environment, you need more than 64 megabytes of default memory. You can customize server behavior with command-line parameters. Running the MEMCACHE.EXE-HELP command will get all the command-line options, as shown in 3.

Figure 3 memcached Server command-line options

Interacting with memcached via Telnet

Once the memcached server starts listening on the port you specify, the memcached client can connect to it via TCP or UDP ports, send commands or accept responses, and finally close the connection.

There are many ways to connect memcached servers, and I'll use Java client connections in the second part of this tutorial, and you'll be able to use simple APIs to store or retrieve objects from the cache. Or you can use the Telnet client to connect directly to the server. Knowing that using Telnet clients to interact with memcached servers is important for debugging Java clients, so let's start here.

Telnet command

First you need to connect the memcached server with a Telnet client. On the WindowsXP platform, if the memcached server is also running on this machine and listening on port 11211 by default, just perform telnet localhost 11211. The following commands are important for Telnet management memcached:

    • set Add a new project to the cache , using the format is Set <keyName> <flags> <expiryTime> <bytes>, you can save the typed value to the next line. If you do not 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, such as add <keyName> <flags> <expiryTime> <bytes>.
    • replace overrides the value of a key , if this key already exists, such as replace <keyName> <flags> <expiryTime> <bytes>.
    • delete Delete cache entry for a key , call Delete <keyName> Delete the value of KeyName.

Figure 4 shows a case of interaction with the memcached server via Telnet. As you can see, the memcached server responds to every command, such as stored, not_stored, and so on.

Figure 4 Telnet Client interaction with Memcached server case

The first part concludes

Here, we briefly discuss 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 Java Client sypmemcached command to establish a distributed caching scheme for a Java sample application. In the process, you will learn more about memcached and how to improve the performance of your Java EE application.

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

Related Article

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.