Jordan has posted 1 posts at DZone. View Full User Profile

New Open Source Cache System

12.18.2008
| 5629 views |
  • submit to reddit

The SHOP.COM Cache System is now available at http://code.google.com/p/sccache/

The SHOP.COM Cache System is an object cache system that...

  • is an in-process cache and external, shared Cache
  • is horizontally scalable
  • stores cached objects to disk
  • supports associative keys
  • is non-transactional
  • can have any size key and any size data
  • does auto-GC based on TTL
  • is container and platform neutral

It was built in-house at SHOP.COM (by me) and has powered our website for years. We are open-sourcing it in the hope that it will be useful to others and to get some help in its maintenance.

This is our first open source attempt and we'd appreciate any help and comments.

Here's a FAQ:

Why another cache?
At SHOP.COM, we've tried almost every cache system there is. None of them do what we need.

What's different about SHOP.COM Cache?
* It doesn't try to be a database
* It can handle very large objects and very large keys
* Cached objects can survive a server crash
* Has both a local (in-memory) and external/shared cache
* Extremely fast - >20K operations a minute (depending on hardware)
* Highly scalable - sccache scales horizontally extremely well

What kinds of things can be cached?
Any Serializable object. In practice, it's common to cache database result sets, HTML pages, HTTP sessions, etc.

Why open source it?
Our cache was written by one engineer (me). We'd like more eyes on the code and help maintaining it.

How stable is the code?
sccache has been used at SHOP.COM in one form or another for 10 years. So, it's stable enough to use in our production environment. However, I've had to make some changes to separate it into an open source library. We are not currently running the open source version at SHOP.COM. I'll post when we make the change.

 

0
Published at DZone with permission of its author, Jordan Zimmerman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dinh Pham Cong replied on Thu, 2008/12/18 - 9:53am

It is designed for Java only. It can not be compared with Memcached which is language neutral

Jordan Zimmerman replied on Thu, 2008/12/18 - 12:00pm in response to: Dinh Pham Cong

Yes, I should've put that above. This is currently a Java only solution. However, it is fair to compare to memcached for Java users.

Dinh Pham Cong replied on Thu, 2008/12/18 - 1:19pm in response to: Jordan Zimmerman

In a wiki page you said that SSCache is horizontally scalable which means: Unlimited objects can be cached by adding additional cache servers. sccache will evenly distribute objects amongst the entire farm of cache servers. Does it implement the same built-in memory clustering mechanism as it can be seen in Terracotta?

Jordan Zimmerman replied on Thu, 2008/12/18 - 2:38pm in response to: Dinh Pham Cong

>Does it implement the same built-in memory clustering mechanism as it can be seen in Terracotta?

As I understand it, Terracotta does something under the hood of the JVM so that the Java code transparently sees objects from other VMs. sccache does not work this way. It treats the external cache servers as a type of HashMap and writes/reads objects based on the hash of the object's key. I believe memcached, Coherence and JCS work this way as well.

Peter Maas replied on Thu, 2008/12/18 - 4:52pm

The post contains both "is non-transactional" and "Extremely fast - >20K transactions a minute (depending on hardware)" ... I assume the latter to be an atomic write action to cache, or is it something different?

Jordan Zimmerman replied on Thu, 2008/12/18 - 5:13pm in response to: Peter Maas

The post contains both "is non-transactional" and "Extremely fast - >20K transactions a minute (depending on hardware)" ... I assume the latter to be an atomic write action to cache, or is it something different?

Yes, I'll correct that. I was using "transactional" with different meanings.

Jay Spring replied on Thu, 2008/12/18 - 8:18pm

interesting stats, though exactly which parts of shop.com is this cache being used for?

Jordan Zimmerman replied on Thu, 2008/12/18 - 8:46pm in response to: Jay Spring

>exactly which parts of shop.com is this cache being used for? 

We cache generated HTML pages, SQL queries, HTTP Sessions, POJO objects, etc.

Joe Farmer replied on Sat, 2008/12/20 - 5:49pm

How do you overcome file fragmentation after repeated record deletions?

Jordan Zimmerman replied on Sun, 2008/12/21 - 12:44am in response to: Joe Farmer

[quote=tech0513]How do you overcome file fragmentation after repeated record deletions? [/quote]

sccache uses a data store I wrote (called CCDB2). It's actually usable by itself if anyone's interested. It re-uses blocks if size allows it to. But, in general, fragmentation isn't an issue. Here's the alogrithm:

A maximum TTL must be chosen - at SHOP.COM we use 12 hours. Every max/2 hours, the current datafile is closed for new objects and a new datafile is created. After max + max/2 hours, you will have 3 datafiles. At max * 2 hours, the third datafile is guaranteed to have nothing but stale objects and is deleted.

Joe Farmer replied on Sun, 2008/12/21 - 9:41am in response to: Jordan Zimmerman

Looks good. Thanks

Joe Farmer replied on Sun, 2008/12/21 - 6:27pm

Created a quick test:

 

public class TestServer {

public static void main(String[] args) throws Exception {
SCServerFactory factory = ShopComCacheFactory.getServerFactory();
SCServerContext context = factory.newContext();
context.port(1111);
SCStorage db = CCDB2StorageFactory.create(new CCDB2Parameters());
db.open(new File("."));
SCServer server = factory.newServer(context, db);
server.join();
}

}

public class ClientWTest {

public static void main(String[] args) throws Exception {
SCCache myCache = getCache();
int max = 100;
for (int i = 0; i < max; i++) {
Person person = new Person(getName(i));
SCDataBlock block = new SCDataBlock(person.getName(), person);
block.canBeStoredInMemory(false);
block.canBeStoredExternally(true);
myCache.put(block);
}
}

static SCCache getCache() throws Exception {
SCClientFactory clientFactory = ShopComCacheFactory.getClientFactory();
SCClientContext context = clientFactory.newContext();
context.address(new InetSocketAddress("localhost", 1111));
SCClientManager manager = clientFactory.newClientManager(context);
return new SCCache(manager);
}

static String getName(int i) {
return "person" + i;
}
}
 
public class ClientRTest {

public static void main(String[] args) throws Exception {
SCCache myCache = ClientWTest.getCache();

for (int i = 0; i < 20; i++) {
SCDataBlock block = new SCDataBlock(ClientWTest.getName(i));
block.canBeStoredInMemory(false);
block.canBeStoredExternally(true);
Object value = myCache.get(block);
System.out.println("value [" + i + "] = " + value);
}
}
}

 

After that:

1. start the server

2. run ClientWTest

3. run ClientRTest - no results returned. Any ideas why? If it's of any help - only data file ccdb23747138201158134.db was created - no index file.

 

Thanks

 

 

Joe Farmer replied on Mon, 2008/12/22 - 1:14am

Added Thread.sleep(2000); to the ClientWTest main() and it started to work. Interesting why it did not indicate there was a problem before. I would expect some kind of at least warning on the server side - I guess the issue was the client closed connection.

Jordan Zimmerman replied on Mon, 2008/12/22 - 1:55am in response to: Joe Farmer

Yes, puts happen in a background thread. So, ClientWTest was exiting too soon. The background put thread is marked as a daemon.

Jordan Zimmerman replied on Tue, 2008/12/30 - 7:22pm

FYI - there was a bug in the storage code. It's now fixed.

Issue:
http://code.google.com/p/sccache/issues/detail?id=1&can=1

Fix:
http://sccache.googlecode.com/files/sccache_0_4.zip

Jordan Zimmerman replied on Tue, 2009/01/27 - 8:28pm

FYI - a bug fix patch has been posted

Jordan Zimmerman replied on Fri, 2009/01/30 - 8:29pm

FYI - yet another bug fix patch has been posted

Jordan Zimmerman replied on Thu, 2009/05/21 - 1:19pm

A new patch is available:

http://code.google.com/p/sccache/

hookfi john replied on Sun, 2009/05/31 - 7:43am

cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store Tiffany Earrings,Tiffany Necklaces.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.