retrospring icon indicating copy to clipboard operation
retrospring copied to clipboard

Answered counts can be negatively offset

Open pixeldesu opened this issue 5 years ago • 2 comments

Apparently some user managed to offset their answered count by 1 into negative. It's questionable how this happened, and probably should be fixed.

Screenshot 2020-05-22 at 19 36 07

pixeldesu avatar May 22 '20 17:05 pixeldesu

This is a fun one -- looks like that user somehow managed to delete a single answer twice at the same time. The original question ended up twice in their inbox, too.

I see a few ways how to solve this:

  • This special case might be solvable by just creating a unique index on inboxes (user_id, question_id), however this might still occur with comments, smiles, and questions.
  • We change how the answers and whatnot are counted. Right now we have these *_count columns in the user model; we could get potentially get rid of those and calculate them on the fly -- perhaps also cache them somewhere (and, of course, invalidate them once something created or destroyed). For a user with 12.6k smiles calculating the smiled_count takes 44.470ms initially, and 14.059ms afterwards.
  • We keep the current logic in place, but have a cron job running that ensures those counters are up-to-date. That might run once a day or so.

nilsding avatar May 25 '20 14:05 nilsding

We change how the answers and whatnot are counted. Right now we have these *_count columns in the user model; we could get potentially get rid of those and calculate them on the fly -- perhaps also cache them somewhere (and, of course, invalidate them once something created or destroyed). For a user with 12.6k smiles calculating the smiled_count takes 44.470ms initially, and 14.059ms afterwards.

I opt for this solution, I think we used Rails.cache for this in another project and it worked well. We can just invalidate the cache on answering a question or creating one, and otherwise leave it fixed.

This also would drop more user fields, which we strive for, to keep the model as lean as possible.

pixeldesu avatar May 25 '20 14:05 pixeldesu