Tuesday, September 29, 2009
Spymemcached Optimizations Followup
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
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!
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
If you have questions or problems please visit the hibernate-memcached google group.
Friday, July 11, 2008
Dogpile Prevention for Hibernate-memcached
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.
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
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
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
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 :)