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?

  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?

  3. Is there a common name for this kind of problem that I can
    google/wikipedia for more information?

On Dec 15, 6:31 pm, John M. [email protected]
wrote:

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.

  1. 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.

kevin cline wrote:

On Dec 15, 6:31 pm, John M. [email protected]
wrote:

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.

A little testing over the weekend reveled that i have at least two cases
where changing a single Producers output ended up hitting the database
302 times and performing a sum across all producers or consumers 1437
times taking 8.34s on my SQLite :memory: database. 82% of the run time
was spent in one of the adding up resources either used or consumed, 11%
in data access.

Given the above, I think the question becomes where and how to cache
data. The concept of not storing redundant data seems prudent, but it
looks like a need to do some kind of memory/processing time trade off,
even it is not stored in the database.

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

///ark

Check out the counter cache in Rails: #23 Counter Cache Column - RailsCasts

On Dec 17, 2007 11:39 AM, John M. [email protected]