views:

25

answers:

2

I have a Rails app that allows users to build up a network structure and then ask questions about how to navigate around it.

When adding nodes and connections these are just saved to the database. At the point you make a query of the network I calculate the shortest path from any node to any other node.

Constructing this in memory takes a while (something I need to fix), but once it is there, you can instantly get the answer to any of these path questions.

The question is... How do I share this network between calls to the website, so each request doesn't regenerate the paths network each time?

Note: I am hosting this on apache server using passenger (mod ruby)

Thoughts?

+1  A: 

One way that 37 signals does it is have the mysql/Db buffer size at XGB and have at much amount of ram/flash mem available on the server this will cache all your selects.

Ryan bates has a screen cast where he explains these concepts including database sharding master slave replication quite well which should help you in the long run ;)

Jet Abe
Are you suggesting storing the calculated paths in the database, so they are query-able. (I understand you are also saying make the database cache everything in memory for performance). That could work. I would need to drop the contents of the new paths table each time I was recalculating them.
Nigel Thorne
No, only that store them in cache it can be MemCache or DB cache or a combination of bothThis way your most visited paths are going to be available without hitting the database
Jet Abe
+1  A: 

Could you use memcached for this data? (see ActiveSupport::Cache::Store for the Rails cache API).

Greg Campbell
I found an answer from Jet's answer. Rails.cache.fetch("network"){calculate_network} caches the result.. then setting the cache method in the config file lets me share it across threads. :) Thanks guys
Nigel Thorne