Consider Random Topic backfill as background task?

AI-generated summary

In a discussion titled ‘Consider Random Topic backfill as background task?’ within the Dev category, DeanMarkTaylor raised concerns that the random topic list backfill process significantly impacts page load times, citing a specific example where a single query added over 300ms to the response time. The user provided detailed stack traces and SQL queries to illustrate the performance bottleneck, suggesting that for rarely seen content, the query should be deferred and results updated via the message bus rather than executed immediately during page rendering.

In response, sam clarified that the heavy query is typically only triggered in extreme cases. Under normal circumstances, the system hits a low watermark threshold that automatically defers the operation, as implemented in the random_topic_selector.rb service.

Whenever the random topic list backfill is executed page load time is dramatically hit.

In this example here an additional 301.9 ms for the single query.

If the “random topic” plays any part of a new users experience to a site…
…perhaps it would be best to not make the query immediately and only return / update the results via the message bus once available?

Executing action: show
T+493.1 ms
Reader
301.9 ms
lib/freedom_patches/fast_pluck.rb:41:in `select_raw'
lib/freedom_patches/fast_pluck.rb:79:in `pluck'
lib/freedom_patches/fast_pluck.rb:71:in `pluck'
app/services/random_topic_selector.rb:27:in `backfill'
app/services/random_topic_selector.rb:57:in `next'
lib/topic_query.rb:657:in `random_suggested'
lib/topic_query.rb:111:in `list_suggested_for'
lib/topic_view.rb:298:in `suggested_topics'
app/serializers/topic_view_serializer.rb:90:in `details'
app/controllers/application_controller.rb:262:in `render_json_dump'
app/controllers/topics_controller.rb:580:in `block (2 levels) in perform_show_response'
app/controllers/topics_controller.rb:572:in `perform_show_response'
app/controllers/topics_controller.rb:89:in `show'
lib/middleware/anonymous_cache.rb:129:in `call'
config/initializers/100-quiet_logger.rb:10:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:26:in `call'
lib/middleware/request_tracker.rb:73:in `call'
lib/scheduler/defer.rb:85:in `process_client'
lib/middleware/unicorn_oobgc.rb:95:in `process_client'
SELECT  "topics"."id" FROM "topics" LEFT OUTER JOIN "categories" ON "categories"."id" = "topics"."category_id" LEFT OUTER JOIN topic_users AS tu ON (topics.id = tu.topic_id AND tu.user_id = -1) WHERE (categories.id = 114 OR (categories.parent_category_id = 114 AND categories.topic_id <> topics.id)) AND (topics.archetype <> 'private_message') AND (COALESCE(categories.topic_id, 0) <> topics.id) AND "topics"."visible" = 't' AND ("topics"."id" != 67459) AND (topics.deleted_at IS NULL) AND (COALESCE(tu.notification_level,1) > 0) AND (
          NOT EXISTS (
            SELECT 1
              FROM category_users cu
             WHERE cu.user_id = -1
               AND cu.category_id = topics.category_id
               AND cu.notification_level = 0
               AND cu.category_id <> 114
               AND (tu.notification_level IS NULL OR tu.notification_level < 2)
          )) AND "topics"."closed" = 'f' AND "topics"."archived" = 'f'  ORDER BY RANDOM() LIMIT 3000   

For information that is perhaps rarely seen on initial render (bottom of page), just may be this is a good optimisation?

You only hit:

In very extreme cases.

In general the low watermark will hit and it will be deferred per: