Showing posts with label Memcached. Show all posts
Showing posts with label Memcached. Show all posts

Tuesday, September 29, 2009

Spymemcached Optimizations Followup

A couple days ago, Dustin Sallings, the other of the spymemcached Java client for memcached, posted an article on some optimizations done to the library. If you are currently using hibernate-memcached you may want to try out the latest spymemcached RC release.

If you combine the latest spymemcached RC release, with memcached 1.4 and the new binary protocol you should see significant performance increases. As Dustin didn't change anything about the spymemcached API it should be a drop in upgrade for you.

spymemcached Optimizations - Dustin Sallings

hibernate-memcached

Enjoy!

Friday, July 17, 2009

Hibernate-memcached 1.2 Released

Earlier this week I released hibernate-memcached 1.2. Hibernate-memcached is a simple library that enables the use of Memcached as a second-level cache in Hibernate.

The 1.2 release updates the Maven dependencies to spymemcached 2.3.1, which includes some bug fixes and reduces the dependencies by one jar (spy.jar is gone). The main purpose of the hibernate-memcached release is to add support for the new binary protocol released with memcached 1.4. Previous versions of memcached only supported the ascii protocol. This has always performed well in it's own right; but the binary protocol reduces CPU compute time and can decrease response time as well.

The hibernate-memcached release also includes a minor tweak to use getMulti in Memcached when DogpilePrevention is enabled. Not a big deal, but someone pointed out an opportunity for it; so I added it.

You can download hibernate-memcached here. Or you can setup your maven project by following these instructions. As always, the source is available via github.

Enjoy!
-Ray Krueger

Wednesday, August 20, 2008

Hibernate-memcached 1.0 released into the wild!

This evening I decided it was time to release hibernate-memcached 1.0. It has all the features I intended for it to have and I haven't seen any problems come up with it at all, so it must be time. Please, spend some time checking it out, even if you don't think you're going to use it right now. I'd really love to get some feedback and see if there is anything folks would like to see it do in a future release.

If you have any questions or comments please drop a line here or even use the hibernate-memcached Google group.

So, what next?

Tuesday, August 19, 2008

Hibernate 3.3 and Hibernate-memcached

I spent some time looking over the new Cache SPI released in Hibernate 3.3.0.GA. Definitely a much more in-depth API compared to the old CacheProvider/Cache interfaces. Following the new interfaces (RegionFactory, Region) you can be much more aware as to your specific responsibilities in the caching system.

I have tested that hibernate-memcached remains compatible with hibernate-3.3.0 and everything looks fine. I am going to hold off on implementing a RegionFactory/Region based implementation for now and do that as a post 1.0 release. It will require some refactoring to the current code base to get it to play nice with both APIs (I'd like to stay compatible with 3.2 for a while).

So, look forward to a hibernate-memcached 1.0 release tonight, or maybe tomorrow.

P.S. Hello China! According to Google analytics, a link from javaeye.com is generating 250+ hits a day to the hibernate-memcached project site right now.

Saturday, July 26, 2008

Hibernate-memcached 1.0-RC1 Released

This morning I've released hibernate-memcached 1.0-RC1. This release contains the new Dogpile prevention feature as well as some minor refactoring. If you're using hibernate-memcached, or are even slightly interested in testing out using memcached in combination with hibernate as a second-level cache, you should check this version out.

If you have questions or problems please visit the hibernate-memcached google group.

Friday, July 11, 2008

Dogpile Prevention for Hibernate-memcached

I posted on the hibernate-memcached project that I was going to release a 1.0-RC1 soon. I will, but first I want to implement one more feature that I think people might like; Dogpile Prevention.

If you're only running 2 or 3 instances of your application this really isn't a problem. If you're running 42 instances there is the risk of a "dogpile". If a frequently used item drops from the cache after it's expiration time comes up you run the risk of many instances of your application working concurrently to restore the cached data. Each instance overwriting the other instances attempt to update the cache until everyone sees the data is no longer missing. Other caching systems attempt to remedy this with locking. That's a really bad thing most of the time for scaling as you'd never want to have all the instances of your application synchronized on what one of them is doing.

To remedy this in hibernate-memcached I will follow the patterns mentioned in the article above. This new feature will go in as a configurable option that is, by default, turned off. I plan to make it configurable per cache-region so enabling it would look like hibernate.memcached..dogpile.prevention=true. There is a slight bit of overhead to this is why it won't be on by default. I may make it configurable at the global level as well with something like hibernate.memcached.dogpile.prevention=true.

Once I get this in and tested I plan to release a 1.0-RC1 version of hibernate-memcached.

Tuesday, June 10, 2008

Hibernate-memcached 0.6 released

I've released version 0.6 of the hibernate-memcached integration. Mainly this was to fix the "nextTimestamp" stuff that hibernate uses for query caching. Apparently that value needs to be "course grained" when using a distributed cache.
hibernate-memcached at Googlecode

Saturday, May 31, 2008

Introducing hibernate-memcached

Some of you may, or may not (care), know I am a huge fan of memcached. Its combination of simplicity, elegance and performance make it fantastic choice for any real caching solution.

I've recently begun work on implementing a bridge for using memcached as a second-level cache for hibernate. The project is hosted at googlecode right now, and the code is currently available in the googlecode hosted subversion repositories. In the future I'll probably move it over to github, but subversion is much more accessible to the public at large still.

Adding hibernate-memcached to your hibernate based application is as simple as adding hibernate-memcached.jar, memcached.jar, and spy.jar to your classpath. This can also be done using Maven.

Enabling the hibernate-memcached cache is simple as adding two properties to your hibernate config.

hibernate.cache.provider_class=com.googlecode.hibernate.memcached.MemcachedCacheProvider
hibernate.memcached.servers=server1:11211 server2:11211

Property #1 tells Hibrnate to use the MemcachedCacheProvider. Property #2 tells the MemcachedClient in use what servers to use for the cache. It really is as simple as that.

There are many other configuration properties available to configure things like the default and per-region cache times, operation timeout, hash algorithm to use, and what key strategy to use.

The hibernate-memcached cache is working great for both individual entity caching and query caching. I currently have a lot of work to do around getting documentation up on the googlecode site. It is ready to play with and easy to setup, if anyone out there has the time to check out; please do! If you run into any questions or problems you can post them on the hibernate-memached google group, or open an issue at googlecode.

Thursday, March 13, 2008

Hibernate and Memcached

As I mentioned at the end of my last post, Cache Everything, I am working on building a library to use memcached from Hibernate as a second level cache. That project is hibernate-memcached and its in it's infancy over at googlecode.

The Hibernate second level cache is used to cache items between session invocations. Hibernate always uses it's session as a first level cache, but if you want to avoid database hits between sessions you need a second level cache. If you want these caches to stay in synch between several instances of your application, you need a distributed second level cache.

Memcached is a fantastically fast distributed cache, so why not use that? Once I build a jar of the hibernate-memcached project you'll be able to include it in your application and tell Hibernate to use it. Telling Hibernate to use it means adding the cache_provider property to your hibernate.cfg.xml, or your Spring LocalSessionFactoryBean if you're using that. The hiberante.cfg.xml looks like this...

<hibernate-configuration>
<session-factory>
<property name="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</property>
<mapping class="com.yadda.yadda.Something"/>
</session-factory>
</hibernate-configuration>


Then, in your mappings you tell Hibernate that you want instances of your persistent objects to be cached, or collections from relation ships to be cached. Once Hibernate knows what you want cached you can watch it pump stuff into memcached, which is good clean geeky fun. Fire up memcahed (on windows or linux) using the -vv command line option to make it run "very verbose". You can see it storing and responding for each operation taken.

You can play with the code straight out of subversion at google code. It will actually work and cache stuff, but it is currently hardcoded to look only at localhost:11211 for a memcached instance. I'll get around to making it configurable. To build the code you'll need Maven2. I highly recommend using Don Brown's no-suck port of maven2 actually.

More to come...

Cache Everything

So I'm working on this Web Service project and its time to consider scalability. Which means its time to setup memcached. If you haven't looked at using memcached, you should.

Memcached is a totally stupid-simple distributed memory object cache. It's this super tiny application that slices off a chunk of RAM and listens on a port. Yeah, that's about it :)

It gets cool to talk about when you start talking about making it "distributed". When you start talking about "Distributed Caches" people start thinking about Coherence and other hugely complicated and hugely expensive enterprise applications. It really dosen't have to be so hard.

Here's what you do. Get a couple cheap boxes with one or two gigs of ram in them. Install the Linux flavor of your choise. Install memcached and run it with "memcached -d -m 1024". This will start memcached on it's default port (11211) and allocate 1gb of ram to the cache. If you do this on three boxes, you now have a 3gb distributed cache.

There are client libraries available for most popular languages. If you're using Java I'd highly recommend using spymemcached from Dustin Sallings.

So you give the clients a list of servers to connect to and they will basically treat that big distributed cache you created as a huge hashtable. Each get/set operation has a string key that is hashed and used to decide what "partition" of the cache to read/write to. If an instance goes down the client just begins distributing reads/writes to the other instances until the missing member comes back.

Using any of the client libraries, in just about any language, your usage looks like this:

  • Check the cache for your object.

  • If your object is not null, return the object.

  • Otherwise, read the object from the database or what have you.

  • Put the object in the cache.

  • Return the object.



Memcached uses a very light and fast binary protocol which makes it usable by just about any language. There are pre-built packages in apt if you're using Ubuntu, you can easily build it from source for just about any other *nux flavor. There's even a windows port. I've only used that for dev at this point though.

I'm working on writing a second level cache for hibernate that uses memcached. It is super simple, but I just started work on it last night (seriously). Check it out if you want. I plan to work on it over the next few nights and get it ready for prime time :)