Memcached and Play

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches.

But i bet you already knew that. I use the Play Framework to create web-applications at the server side. And amongst all the numerous good things its known for is that its stateless, allowing one to create highly scalable solutions. Think of servlet containers and you think of HttpSession that will help you tie up all the requests. Well, in Play! there is no session to fall back on. The session, while being incredibly useful, requires that each user request comes to the same instance of the servlet container to make any sense (sticky sessions). This creates a problem when you are deploying large scale applications spanning across different machines.

Play cache with Memcached

Play! allows you to use Memcached as its caching service when you need to cache data. Multiple instances of Play! can point to the same instance / cluster of Memcached servers and what you get in return is a robust and scalable solution for your caching requirements. However, its important to note that since Memcached is an in-memory implementation, the cache contract is clear. When you put data in a cache, you cannot expect that data to remain there forever. A cache is fast, but values expire and the cache generally exists only in memory.

Install Memcached on Ubuntu

Run the following to install Memcached

Install memcached

1

sudo apt-get install memcached

The configuration file for Memcached is location at /etc/memcached.conf. To edit the settings you simply do the following on the command prompt.

Edit Memcached configuration

1

vi/etc/memcached.conf

There are various settings and you are advised to go thru the man pages (man memcached ) but common settings are

Memcached

1

2

3

4

5

6

7

8

# Memory usage in Mb

-m128

# default port

-p11211

# listen locally

-l127.0.0.1

Once you are done with the settings, save and start the daemon

Start Memcached

1

sudo/etc/init.d/memcached restart

Optionally, you can also run this from the command prompt with the flags mentioned in the man pages. I would take a look at the wiki page that details this better. For example, if you want to see verbose logs you could use -v, -vv or -vvv in order of least verbose to most verbose.

Memcached - command line

1

memcached-vvv

Play Configuration

While i do intend to make further posts on my experiments with the Play Framework, here is the tiny bit of configuration that i needed to make in the application.conf file to make it work

Play - application.conf

1

2

3

4

5

6

7

# Memcached configuration

# ~~~~~

# Enable memcached if needed. Otherwise a local cache is used.

memcached=enabled

#

# Specify memcached host (default to 127.0.0.1:11211)

memcached.host=127.0.0.1:11211

If you application is configured properly, lookout for the following logs while starting your application

Categories

Tags

Help Center

Contact Info

CollectiveZen
Tel: (+91) 020 2672-2150Mobile: (+91) 7875757782

Innovations Unmlited

The convergence of commoditized infrastructure, multi-gestural interfaces, the emerging ubiquity of the mobile Internet, and location-based services has made the world an exciting place and we are extremely passionate about the potential that technology holds for all of us.