Tony the Pony
Tony the Pony

Reputation: 41457

GAE Datastore: “total” property vs. adding up line items?

This is another Google App Engine Datastore question.

Let’s say my app tracks a bunch of rats and how much cheese they’ve eaten. Each Rat entity has a series of Meal entities -- each tracks when the rat ate and how many slices of cheese (slices property). Each Rat also has a total_slices property that is updated whenever a new meal is logged.

Should I abandon the total_slices property in favor of simply adding up the slices of each Meal whenever I need to know the total? The Datastore documentation says that queries are cheap, while updating an entity at a high frequency is problematic (Google defines “high frequency” as updating an entity more than once a second at a sustained rate). So if I have some really voracious rats that feast more than once a second, I’m risking a timeout when I update the Rat’s total_slices.

Any suggestions how to best design this scenario?

Upvotes: 2

Views: 113

Answers (1)

cope360
cope360

Reputation: 6344

There are two separate issues wrapped up in your question:

1. Should you de-normalize total_slices to the Rat entity?

To answer this you must analyze the data access patterns of your application. For example:

  • Is getting total_slices for a Rat a common task?
  • Will you ever need to list or sort Rats by total_slices?
  • Will adding up each Meal still be efficient after several years of data have accumulated?

2. What do you do in GAE if you have an entity that needs to be updated more than 1/sec?

Use a Sharded Counter.

Upvotes: 4

Related Questions