Hacker News new | comments | ask | show | jobs | submit login

SELECT `digdate`, `id` FROM `Diggs` WHERE `userid` IN (59, 9006, 15989, 16045, 29183, 30220, 62511, 75212, 79006) AND itemid = 13084479 ORDER BY `digdate` DESC, `id` DESC LIMIT 4;

Ok, how do we optimize this query?

Step 1: Keep all dugg items in memcached for the last N days, where N is determined by when you run out of memory. Then, your query becomes:

SELECT `digdate`, `id` FROM `Diggs` WHERE `userid` IN (59, 9006, 15989, 16045, 29183, 30220, 62511, 75212, 79006) AND digdate < now() - interval '5 days' AND itemid = 13084479 ORDER BY `digdate` DESC, `id` DESC LIMIT 4; /* Excuse the postgresql syntax /

If your database is properly clustered, this will mean you are only running the query against partitions holding old dugs, which is probably not as hot as the more recent stuff. Additionally, I strongly suspect that you see more recent articles more than old ones, if the article is less than 5 days old you need no SQL at all, just the memcache lookup. For example, if you are looking at the homepage, and there are 15 articles on it, you have to do a single memcached get request for all the pairs like (article_id, friend_id), so if you have 100 friends that is 100 15 keys to request. This is large, but who cares, you can add memcached servers and webservers until you puke and this will keep scaling without limit. When browsing old articles the db will get hit heavily, but only the partitions holding old data, and I would guess that this is a very very small fraction of their overall use.

Step 2: When a user is activly using the site, like they have viewed 2 pages in the last 10 minutes or something, shove all their old (article_id, friend_id) pairs into memcached as well. Once a user has reached the 'activity threshhold' and the cache is filled, no sql is necessary to find all their friend's dug articles. As a bonus, no weirdo software like 'cassandra' which may or may not continue to exist in 1 year is necessary.

For step 1 you need very little effort, just put a key into memcached every time a user digs something, and put a 5 day timeout on that key. This is 1 line of code in whatever code handles the http request representing a 'dig'. Then you have to build up the list of friends and keep it somewhere when a user logs in to the site (or returns with a cookie that has them logged in). This would take one memcache request when the user logs in/comes back to see if their friends list is in memcached, one sql statement if it is not, and a line in the are that handles adding friends to spoil the key if their friends list changes (you could try updating it, but why, just let it be regenerated on their next http request). Finally, you have to generate the keys for the (article_id, friend_id) pairs on each page view, and do a multi_get from memcached.

Step 2 would require an asynchronous process, so would be more complex.

I could implement step 1 in an hour or so if familiar with the digg codebase, and step 2 in perhaps 2 days, however if they have other async processes that occur when a user logs in that you could integrate this with it could take as little as an hour or two as well, since the logic is dead simple, it is the mechanics of running a process to do it that is time consuming.

Finally, you would have to figure out how much memory you would need to store N days of digs (users with no friends do not count in this). I believe it would not be very much.




Since I wrote that query back in 2005, and it was actually somewhat more complex (had to deal with users banned after they dug the same story as you for starters) I assume Ian (whom I've never met) is simplifying for pedagogical reasons. And we had something similar to what you describe in no. 2 in 2006.


Also I find it fascinating that the table is "Diggs" (it was originally "digs" and I suspect it still is). Gotta get the branding right, even in sql queries.


When a Memcached instance goes under, all Diggs stored on that instance in the last N days disappear. Not the end of the world for this application, but very undesirable for most things. You could potentially "fall back" to MySQL for this but a workable strategy for that probably isn't simple.

Further, step two is irrelevant. There's no way of knowing if a (article_id, friend_id) pair in memcached is from the most recent N days or whether it's been stuffed in due to a user being active. Therefore, searching the DB for older diggs is still necessary, and should take the exact same amount of load as if they weren't in memcached at all.

Memcached + MySQL makes great sense when the data set is small and simple. If all the content on the site fits within 1 GB you could probably easily push a hundred millions unique visitors a day. For an application like this, the relatively poor performance of MySQL and the inflexibility of Memcached cause problems.

It seems to me that using Cassandra, even in its current immature state, makes much more sense than the solution you're proposing.


Ahh, you make a great point, that if an instance goes down you lose those keys. Cassandra doesn't necessarily solve this, however, unless you have enough failover that it is unthinkable that you could have cassandra lose data. If you can rebuild the cache in cassandra you can rebuild it in the identical way using my scheme, but my scheme only requires N days of data to be run through.

Step 2 is fine, memcached allows setting of timeouts on keys. If you always set a timeout of (dig_time + five_days) - now() you are set. Be careful, times > 30 days imply a unix timestamp not a timeout.

Mysql kindof sucks. The clustering is easy to set up at least.

Cassandra and other big on disk hash tables are pretty cool. I think once they have datastore-like indexing capabilities they will be totally usable. My qualm with using them is that there are 80 of them right now, and they are all pretty immature. The ones that are mature, like bsddb, and complicated to use.

My point was that cassandra is just a big memcached.


> unless you have enough failover that it is unthinkable that you could have cassandra lose data

That's pretty much the idea. Cassandra makes replication + failover totally seamless, so there's no excuses in that respect. :) Cassandra also supports replicating across multiple data centers.


Sure.

but NoSQL kinda gives you memcache like awesomeness with some level of persistence. Think of cassandra as a more feature-rich, persistent memcache. :)


No. NoSQL gives you memcached on a disk, which sucks a lot. SQL is good.


NoSQL is a buzzword, originally to describe "distributed open source databases" but it's catchy enough that pretty much every nonrelational db uses it.

So saying "NoSQL gives you [or doesn't give you] X" is meaningless.

And for the record, SQL is great, but trying to keep using it once your data set has outgrown a single machine is like "trying to build a bookshelf out of mashed potatoes." (Gotta love jwz.)




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: