Skip to content

We cache rendered commit messages in Redis twice

When we render a commit message using our Markdown pipeline, we cache the results twice:

  1. Through Commit's attr_mentionable :safe_message, which goes through Mentionable and generates a key like cache:gitlab:banzai/commit:$sha/safe_message/single_line.
  2. Through Commit's cache_markdown_field calls, which go through CacheMarkdownField and generate a key like markdown_cache:commit:$sha.

The first is a single string for the whole message; the second is a hash with keys for the title, full title, and description.

We can see that these are basically identical and generated for the same commit:

[ gprd ] production> Rails.cache.fetch('banzai/commit:573bc5f69928fe32b7f92c176843dbaa1a989264/safe_message/single_line').bytesize
=> 551
[ gprd ] production> Gitlab::Redis::Cache.with { |r| r.hmget('markdown_cache:commit:573bc5f69928fe32b7f92c176843dbaa1a989264', 'title_html', 'description_html') }.sum(&:bytesize)
=> 550

From gitlab-com/gl-infra/scalability#499 (comment 406421969) we also see that these keyspaces use approximately the same amount of space in production (900 MB). This represents 1-2% of our total cache space, so it's not the biggest offender, but it is a very obvious area for improvement, and we have seen a long term drop in our cache hit ratio: https://dashboards.gitlab.net/explore?orgId=1&left=%5B%22now-6M%22,%22now%22,%22Global%22,%7B%22expr%22:%22sum(redis:keyspace_hits:irate1m%7Benvironment%3D%5C%22gprd%5C%22,%20type%3D%5C%22redis-cache%5C%22%7D%20and%20on%20(instance)%20redis_instance_info%7Brole%3D%5C%22master%5C%22%7D)%5Cn%2F%5Cn(%5Cnsum(redis:keyspace_hits:irate1m%7Benvironment%3D%5C%22gprd%5C%22,%20type%3D%5C%22redis-cache%5C%22%7D%20and%20on%20(instance)%20redis_instance_info%7Brole%3D%5C%22master%5C%22%7D)%5Cn%2B%5Cnsum(redis:keyspace_misses:irate1m%7Benvironment%3D%5C%22gprd%5C%22,%20type%3D%5C%22redis-cache%5C%22%7D%20and%20on%20(instance)%20redis_instance_info%7Brole%3D%5C%22master%5C%22%7D)%5Cn)%5Cn%22,%22format%22:%22time_series%22,%22interval%22:%221m%22,%22intervalFactor%22:2,%22legendFormat%22:%22%7B%7B%20fqdn%20%7D%7D%22,%22datasource%22:%22Global%22%7D,%7B%22mode%22:%22Metrics%22%7D,%7B%22ui%22:%5Btrue,true,true,%22none%22%5D%7D%5D

Edited by Sean McGivern