Skip to content

Instrument ActiveSupport Caches - #74294

Open
Hamms wants to merge 3 commits into
stagingfrom
elijah/instrument-rails-cache
Open

Instrument ActiveSupport Caches#74294
Hamms wants to merge 3 commits into
stagingfrom
elijah/instrument-rails-cache

Conversation

@Hamms

@Hamms Hamms commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Part of our ongoing work to address runaway memory usage on frontend web application servers by reexamining our caching strategy.

I want to track hit rate to get better insight into how per-process versus per-server caches improve our efficiency, as well as just generally to get a look at how well we're using our cache. I'd like to track total bytes read out of the cache so we can evaluate whether it would be feasible to fetch it over the network; from Redis, for example.

Links

https://guides.rubyonrails.org/active_support_instrumentation.html#active-support-caching

Testing story

Tested on an adhoc; repeatedly fetched /courses/allthethingscourse/units/1 with curl, and confirmed that results are as expected:

image

Follow-up work

We probably want to remove the byte size metric after we've gathered a representative amount of data, since it's both gross and is adding a little bit of extra work to a very high-throughput method. I don't anticipate it being a problem in the short term, though.

Comment thread lib/cdo/aws/metrics.rb
Comment on lines 65 to 75
# Convenience method to put a single metric to CloudWatch.
# Accepts a single '[namespace]/[metric_name]' name parameter
# and a standard Ruby hash to specify dimension key/values.
# Accepts a namespace, a metric_name, and a standard Ruby
# hash to specify dimension key/values.
#
# @param [String] name in the form of 'namespace'/'metric_name'
# @param [String] namespace
# @param [String] metric_name
# @param [Number] value
# @param [Hash{Symbol => String}] dimensions
# @param [Hash] options Additional keyword arguments to be merged
# into the {Aws::CloudWatch::Types::MetricDatum} object.
def self.put(namespace, metric_name, value, dimensions, **options)

@Hamms Hamms Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the jerk who updated this method signature failed to update the comment to match, so I'm fixing it up while I'm in the area 🙃

@Hamms Hamms changed the title add some temporary instrumentation for the rails cache Instrument Rails Cache Jul 31, 2026
@Hamms
Hamms marked this pull request as ready for review July 31, 2026 19:58
@Hamms Hamms changed the title Instrument Rails Cache Instrument ActiveSupport Caches Jul 31, 2026
@Hamms
Hamms requested a review from a team July 31, 2026 20:00

@cat5inthecradle cat5inthecradle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one CloudWatch namespacing suggestion.

Comment on lines +3 to +12
Cdo::Metrics.put(
'Infrastructure',
'ActiveSupportCacheRead',
1,
{
Environment: CDO.rack_env,
Hit: event.payload[:hit].to_s,
Store: event.payload[:store]
}
)

@cat5inthecradle cat5inthecradle Aug 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing two metrics: ActiveSupportCacheHit and ActiveSupportCacheMiss, keeping Environment and Store as dimensions and toggling which metric based on event.payload[:hit]? I think that aligns more with what I'm used to seeing in CloudWatch.

Environment Store Hit Name
production mystore true ActiveSupportCacheRead
production mystore false ActiveSupportCacheRead

vs

Environment Store Name
production mystore ActiveSupportCacheHit
production mystore ActiveSupportCacheMiss
production mystore ActiveSupportCacheBytesRead

Functionally the same, but it'll play nicer in the CloudWatch UI, and puts it right next to the other metric in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants