פרטים נוספים על אופן השימוש במטמון Redis?

סיכום מבוסס בינה מלאכותית

In a discussion titled “More details on how the Redis cache is utilized?” under the Dev category, griegm reports unexpected Redis cache hit rate behavior during load tests — dropping to 22% under 100% read scenarios, despite 85% under mixed read/write. They seek clarity on Redis usage, asking: how to trace cached data in the codebase, whether login state affects caching, and whether static data like post content is cached. Falco explains that aggressive caching is primarily for anonymous users, suggesting testing with anon requests; notes that Discourse.cache.fetch and DistributedCache.new are key methods to grep; and clarifies that while Redis is heavily used for Sidekiq and MessageBus, post content is not cached individually — instead, full responses for anonymous users are cached to minimize DB load. griegm confirms retesting with anonymous requests yielded dramatic performance gains — from 25 to 380 requests/sec — and a cache hit rate increase to ~66%, validating Falco’s guidance.

Hey there!

I’ve been performing some load tests on a Discourse instance and I noticed that when repeatedly fetching the same comment thread over and over the Redis cache hit rate went down, rather than up, which was a bit unexpected (for a mixture of reads/writes we saw a cache hit rate of up to 85%, for 100% reads it dipped down to as low as 22%).

I’ve done some searching around in the codebase and on the forums here, and how exactly the Redis cache is utilized is a bit unclear to me. The README states this:

We use Redis as a cache and for transient data.

I used redis-cli to dump the commands that were sent to the Redis cache during the aforementioned load test, and I primarily saw “get” commands for scheduled jobs and for keys prefixed with “__mb_backlog_id_n_” (I believe this is referring to MessageBus stuff).

I have the following questions:

  • Is there an “easy” way for me to search the codebase for which bits of data are cached in Redis? I’d love to be able to answer these questions on my own, but unfortunately I’m not super familiar with Ruby on Rails applications (or Ruby in general, really). :slight_smile:
  • Does being logged-in/logged-out impact cache hit rates? For reference, the load test mentioned above was using an admin API key.
  • Are frequently queried/fairly static data such as post contents cached in Redis? Or is Redis primarily used for job scheduling and background processing with Sidekiq and whatnot?

Thanks in advance!

This is the main thing here. The most aggressive cache happens for anonymous requests, so I’d suggest retrying the load test with some anon robots.

There are a handful of grepable methods, like Discourse.cache.fetch and DistributedCache.new.

We do cache some infrequent config blobs, but the approach to topics is mostly caching the whole response for the anons, allowing the app to craft a response while barely touching the DB.

Redis is heavily used for Sidekiq and MessageBus.

Awesome, thanks for the super helpful response!

I just re-ran the load test but with anonymous requests this time around, and we saw a huge performance improvement! Previously we were only able to hit around 25 requests per second on a single host, now we can hit 380! The Redis cache hit rate also went up from ~22% to ~66%. :slight_smile:

Just thought I’d circle back with the results in case anyone was curious.

Thanks again for the help!