Show HN: Stash, a graph-based cache for Node.js and Redis
The premise I started with is that one of the hardest things to manage in a cache is dependencies between entities. In order to cache items effectively, you inevitably have to duplicate "child" data inside of "parent" entries. Then, when a child is changed, you have to invalidate the child and its parents, and its parents' parents, and so on.
To try to help this, I hacked together a simple Node.js library called Stash, which models the cached values as a graph. When you invalidate an item, Stash will walk the graph and invalidate any items that depend on the item that was marked as invalid.
The code is available here:
https://github.com/nkohari/stash
I'm not suggesting that this is by any means a revolutionary idea; it just started as a mental exercise and now I'm wondering if there's any value to continuing to improve it as a library.
I'm also interested in what you find to be the most difficult part about caching, and how Stash could be improved to help.
Any feedback is appreciated, but bear in mind this is just a few hours worth of work and it has quite a few rough edges. Thanks!
8 comments
[ 2.4 ms ] story [ 31.7 ms ] threadPerhaps instead of explicitly defining dependencies, there could be some way of modeling your cached data as some kind of set of objects which would then understand their own graph. As you can see I haven't quite figured out how it would work but this is how I've been thinking about it lately. Like a simple ORM, in some way.
Something like the following terrible psuedocode:
We use MongoDB as our data store, and between the UUIDs used as primary document keys and arrays of UUIDs representing links between documents, we could (reasonably) easily determine the dependencies automatically.
Thanks
Cycles are reasonably possible, if two cached items depend upon each other. For example, if you store the text of the last comment a user made on HN inside the "user" cache entry, and the user edited their most-recent comment, you would have to invalidate not only the comment but also the user.
Stash treats dependencies as a DAG (directed acyclic graph). During traversal when a node is invalidated, it's aware of the potential of cycles and won't backtrack over paths its already examined.
Since then Mark Nottingham and myself have written this up as an internet draft:
http://tools.ietf.org/html/draft-nottingham-linked-cache-inv...