views:

237

answers:

2

Has anyone joined SQLAlchemy and Memcached together so that results returned by SQLAlchemy where cached in Memcached? At the moment I have no idea how to implement it myself, I am completely lost and looking for help.

Thanks, Boda Cydo.

+2  A: 

Memcache and relational databases are two separate things, that don't work in the same way at all. Memcache is a simple lookup bucket, useful for dumping blobs like partial web pages in that are shared amongst many requests and take you a long time to produce (for when you can't reasonably optimise/denormalise your database schema enough to make them quick to produce).

It's not a good idea to use memcache like a full backend data store, and it wouldn't be useful to present an SQL interface that went to memcache or the database: you'd get the worst of both worlds, without either memcache's simplicity/speed or a relational database's ACID-compliance.

Your database server already has a cache. If you want it to cache more, turn the size of that cache up as high as you can without having to go to silly hardware. Use memcache for making the front-end faster — but only when you really can't get the performance you want out of the database, because it requires you to make compromises on data consistency. Don't resort to memcache until you really need to.

bobince
couldn't disagree more. loading the rows over the network, converting into internal structures, rendering a page. All expensive processes that can't be improved by a faster database. Most non-trivial web applications that use any kind of remote system to load data should assume the usage of a cache like Memcached by default. It's not just about caching result sets, that is only one aspect of caching.
zzzeek
And even result set caching is extremely relevant. You might have a few queries that are necessarily slow (say, aggregating out the standard deviation of some values across an entire large table) and are only needed a few times a day. You can try scaling your database onto massive hardware for just this one query, or you can throw it into a cache for free.
zzzeek
+4  A: 

See the beaker caching example in SQLAlchemy source to see how you might integrate caching into a SQLAlchemy application. Beaker can use Memcached as the cache store.

Ants Aasma