Click here to Skip to main content
Click here to Skip to main content

Implementing Distributed Caching using Memcached

By , 26 Jul 2010
Rate this:
Please Sign up or sign in to vote.

Tools Used in this Article - Need to be Downloaded

  1. Memcached Manager - This application enables us to manage MemCacheD in our Windows server environment including remote service management.
  2. Memcached .NET client Library - C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers.
  3. Environment. Visual Studio 2010, Ubuntu 9.10

Contents

  1. Introduction
  2. Installing and configuring Memcached on Linux
  3. Installing and configuring Memcached on Windows using Manager
  4. Configuring Memcached server with Command line
  5. Using Memcached in web app and in win form

1. Introduction

A distributed cache as a concept and as a best practice is gaining more popularity. Only a few years ago, very few people in the .NET space knew about it, although the Java community has been ahead of .NET in this area. With the explosive growth in application transactions, databases are stressed beyond their limits, and distributed caching is now accepted as a vital part of any scalable application architecture.

There are a couple of In-Memory distributed cache engines such as Velocity, NCache, ScaleOut and Memcached. Each one of these has its advantages and disadvantages in term of scalability, security and licensing the details of each can be explored on the internet. In this article, we will discuss Memcached.

Mecached is open source easily deployable distributed in-memory, cross application and cross Platform Engine meaning the same server can be accessed from a variety of languages - PHP, Java and .NET and from Windows, Linux, etc.

In this article, we will be Installing Memcached server on Linux and Windows and after that, we will be accessing both servers from .NET applications (Web and desktop) using the Memcached client library.

2. Installing and Configuring Memcached on Linux

In this article, we will be installing Memcached on Ubuntu 9.10, but you can install on any Linux platform.

Memcached uses Libevent, so you have to compile and install Libevent first. Login to Ubuntu and open terminal and run the following commands and run the following commands on Ubuntu terminal:

wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar xzf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure && make && sudo make install

System might prompt for the password, so enter the password to continue. Now run the following commands to compile the Memcached server:

wget http://memcached.googlecode.com/files/memcached-1.4.1.tar.gz
tar xzf memcached-1.4.1.tar.gz
cd memcached-1.4.1
./configure && make && sudo make install

After installation completes, two folders “libevent-1.4.14b-stable” and “memcached-1.4.1” will be created as shown below:

Linux_Installed_memcached.jpg

Now you can run Memcached either by double clicking from its location or through terminal as shown below. While running from terminal, you can configure different options listening address and port:

Memcached –L 192.168.1.106 –P 56789

Linux-_Runing_memcached_terminal.jpg

To make sure Memcached is running, type the following command to check the status:

sudo netstat –plntu

Linus-_ports.jpg

Checking with system monitor that Memcached is working:

Linux-_Memcached.jpg

Memcached is running and listening on port 56789.

Now we will be checking the IP address Ubuntu is running on so that we can use that in our client.
Click system -> Administration -> Networking Tools.

Linus-_Neetwork_address.jpg

3. Installing and Configuring Memcached on Windows using Manager

In this step, we will be installing and configuring the Memcached server on Windows. To do this, download the Memcached manager from the link given at the beginning of article. Run that tool as administrator.

Add server name “localhost” and click add server. Refer to the below screen:

manager1.jpg

And click add instance button, configure the settings. Refer to the below screen:

manager2.jpg

Make sure that memcached is started and running, We can use resource monitor to see the port its running on:

resource_monitor.jpg

Memcached is running and listening on port 12345.

4. Configuring Memcached Server through Command Line

Configuring Memcached with command line is the same as we did for Ubuntu, e.g.,

Memcached –L 192.168.1.101 –P 12345 

But here is the list of some more commands that can be useful:

  • -u <user> : run as user if started as root
  • -m <num> : maximum <num> MB memory to use for items
    • If more than available RAM - will use swap
    • Don’t forget 4G limit on 32 bit machines
  • -d : Run as a daemon
  • -l <ip_addr> : Listen on <ip_addr>; default to INDRR_ANY
  • -p <num> : port
  • -s <file> : unix socket (disables network support)
  • -c <num> : max simultaneous connections
  • -v : verbose
  • -vv : (2 v’s) very verbose
  • -P <file> : PID file (used with -d)
  • -t <threads> : number of threads to use to process incoming requests. Only valid if compiled with thread support enabled. Only useful up to number of CPUs

5. Using Memcached in Web Application and in Desktop Application

In this step, we will be accessing Memcached from both Linux and Windows. To do so, first we need to download the Memcached .NET client Library from the link given at the beginning of article.

Create a solution and add web and desktop projects as shown below:

solution.jpg

Add reference to the Memcached.ClientLibrary.dll in both projects. This assembly can be located at the following path:

memcacheddotnet_clientlib-1.1.5\
memcacheddotnet\trunk\clientlib\src\clientlib\bin\2.0\Release

To test the functionality of Memcached, we will insert some value through web application and will be retrieving that from desktop application.

Web application contains text boxes for server, key, value and a button insert into Memcached as shown below:

browser.jpg

Add the following code snippet in Page_Load():

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] serverlist = { txtServer.Text };

        SockIOPool pool = SockIOPool.GetInstance();
        pool.SetServers(serverlist);

        pool.InitConnections = 3;
        pool.MinConnections = 3;
        pool.MaxConnections = 5;

        pool.SocketConnectTimeout = 1000;
        pool.SocketTimeout = 3000;

        pool.MaintenanceSleep = 30;
        pool.Failover = true;

        pool.Nagle = false;
        pool.Initialize();
    }
} 

Add the following code into Button1_Click() handler:

protected void Button1_Click(object sender, EventArgs e)
{
    MemcachedClient mc = new MemcachedClient();
    mc.EnableCompression = false;

    mc.Set(txtkey.Text, txtValue.Text);

    //SockIOPool.GetInstance().Shutdown();
}

Add the following code into btnStats_Click() handler:

protected void btnStats_Click(object sender, EventArgs e)
{
    // Get the Statistics of memcahed server
    MemcachedClient mc = new MemcachedClient();
    IDictionary stats = mc.Stats();
    foreach (string key1 in stats.Keys)
    {
        lblStats.Text += key1;
        Hashtable values = (Hashtable)stats[key1];
        foreach (string key2 in values.Keys)
        {
            lblStats.Text += key2 + ":" + values[key2] + "\r\n";
        }
        lblStats.Text += "\r\n";
    }
}

Creating desktop application contains text boxes for server and Key and button “Get Value“:

desktop.jpg

Write the following code snippet into btnGetValue_Click() event handler:

private void btnGetValue_Click(object sender, EventArgs e)
{
    string[] serverlist = { txtServer.Text };

    SockIOPool pool = SockIOPool.GetInstance();
    pool.SetServers(serverlist);

    pool.InitConnections = 3;
    pool.MinConnections = 3;
    pool.MaxConnections = 5;

    pool.SocketConnectTimeout = 1000;
    pool.SocketTimeout = 3000;

    pool.MaintenanceSleep = 30;
    pool.Failover = true;

    pool.Nagle = false;
    pool.Initialize();

    MemcachedClient mc = new MemcachedClient();
    mc.EnableCompression = false;

    string value = (string)mc.Get(txtKey.Text);
    MessageBox.Show(value);
}

Now test the Memcached by changing servers and inserting different values.

Now change the server to Windows Memcached which is running on 192.168.1.102:12345 as shown below

browser2.jpg

And we can see in Memcached statistics in Manger, Item Count is 1.

manager3.jpg

Conclusion

We have successfully Installed memcached server on Windows and Linux environment and used those in our ASP.NET and C# projects.
I hope this project will be useful for developers who want to implement scalable solutions with distributed caching.
I tried my best to describe each step with a screen shot. I hope you've enjoyed this article. If you like this article, please let me know Smile | :) . If you have any questions, please feel free to contact me at fayaziiui@gmail.com.

History

  • 26th July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

About the Author

Fayaz Soomro
Software Developer Macmillan Publishing
United States United States
No Biography provided

Comments and Discussions

 
QuestionMore on Distributed Caching case. PinmemberPingPong KingKong12-Feb-14 5:25 
QuestionAccessing from multiple instances Pinmemberpsnivas20-Nov-13 6:28 
AnswerVery good article PinmemberNurhakKaya14-Aug-12 5:23 
This is a very informative article. Thank you so much!Thumbs Up | :thumbsup:
 
http://www.nurhak-kaya.blogspot.com/[^]
GeneralDistributed Cache Pinmemberwesnur14-Sep-10 21:35 
GeneralMy vote of 2 PinmemberPedroMC3-Aug-10 3:45 
GeneralIn GNU/Linux, use the package manager to install software PinmemberPedroMC3-Aug-10 3:41 
GeneralDistributed Cache Pinmembergocebe3-Aug-10 3:36 
GeneralMy vote of 2 PinmemberSecrets26-Jul-10 2:45 
GeneralRe: My vote of 2 PinmemberFayaz Soomro26-Jul-10 6:10 
GeneralFeedback PinmemberGil Fink25-Jul-10 19:06 
GeneralRe: Feedback PinmemberFayaz Soomro26-Jul-10 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 26 Jul 2010
Article Copyright 2010 by Fayaz Soomro
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid