Model Coordination, A design problem

Greating All,

I have had to solve the following problem enough in the last month that I'm sure it has a name and a body of research behind it but I'm having trouble finding it.

The general problem is this: I have a Site Model. Each Site has many Consumers and Producers. Producers make some amount of a "resource" available at the Site, while Consumers use these "resources." The program's goal is to manage changes in Consumers and Producers to keep them in balance.

Questions: 1) When is it better to try to maintain a "resources" column in Site, and when is it better to reproduce the data every time. In other words is there a good way of "cashing" the resource calculation?

As a general rule, do not store redundant information in a database, or anywhere else, until you have a known performance problem.

2) When a controller makes a change to a Consumer or Producer should the model or the controller be responsible for asking the changed items Site to rebalance its resources?

This one is easy. The model should be responsible. Keep the controllers as thin as possible. Ideally a controller action should fetch a model, call one method on the model, and then call a view to render that one model.

What is involved in rebalancing resources? Maybe the algorithm could be improved.


Check out the counter cache in Rails: