Optimizing string interpolations in Ruby
Tags: ruby, rails, performance
Published: 2020-06-16 13:55:49

---
title: Optimizing string interpolations in Ruby
published: true
date: 2020-06-15 04:52:27 UTC
tags: ruby,rails,performance,rubyonrails
canonical_url: https://medium.com/freshworks-engineering-blog/optimizing-string-interpolations-in-ruby-40f09b310df3
cover_image: https://cdn-images-1.medium.com/max/1024/0*k4HlRzcQAKVbND6y.jpg
---
_[Ruby on Rails is a great web application framework for startups to see their ideas evolve to products quickly. It’s for this reason that most products at Freshworks are built using Ruby on Rails. Moving from startup to scale-up means having to constantly evolve your applications so they can scale up to keep pace with your customers’ growth. In this new Freshworks Engineering series, Rails@Scale, we will talk about some of the techniques and patterns that we employ in our products to tune their performance and help them scale up.]_

Freshservice is a cloud-based IT help desk and service management solution that enables organizations to simplify IT operations. Freshservice provides ITIL-ready components that help administrators manage Assets, Incidents, Problems, Change, and Releases. The Asset Management component helps organizations exercise control over their IT assets.

Freshservice is a SaaS product powered by Ruby on Rails. It is backed by a MySQL database for persistence storage and uses Memcached and Redis extensively for caching and config storage purposes. Multi-tenancy is at the core of a SaaS product and tenant isolation is an important factor when dealing with data. To ensure that, the keys representing data in the cache stores are always suffixed with each tenant’s unique ID.

We benchmarked the idea of changing the way we build cache keys for getting and setting data on both Memcached and Redis. We noticed that for every cached object that belonged to some entity (or entities) like a tenant or a user, we were creating hash objects to build a unique key representing that entity in the cache store.

This is a fairly common practice of building a string in Ruby (the cache key in our case):

{% gist https://gist.github.com/ritikesh/c0d64410d9a903118b3c567fba30be18.js %}

The above snippet creates a new hash object unnecessarily. Since the end goal is to have an interpolated string, we can skip the hash creation part entirely. With the alternate approach, we could just interpolate the required constant with the dynamic value. This value could be anything that responds to `to_s`.

{% gist https://gist.github.com/ritikesh/8e16f59a2fe52134f4be7082ab4cac79.js %}

We introduced a `#key` method on the module, which would take a symbol and a value as params and returns an interpolated string. The resulting key that was generated was the same with both approaches, hence the existing cached objects wouldn’t be affected by this change.

We ran a benchmarking exercise to check if there was any performance impact of this activity. The benchmarking results were as follows:

{% gist https://gist.github.com/ritikesh/aae1cc94912501f63bfb7a67517ce2a2.js %}

### Benchmarks we ran

{% gist https://gist.github.com/ritikesh/aeaee99bb914051a63fcbe4b88777de8.js %}

**Note**: The above benchmarks were run on our current production versions of Ruby/Rails (`2.3.7` / `4.2.11.1`)

Currently, we’re supporting up to 2 dynamic values via 2 different methods (`MemcacheKeys#key`, `MemcacheKeys#multi_key`). Making the method accept a dynamic number of values would create an array on each invocation via the Ruby splat operation and would defeat the whole purpose.

After shipping out the key generation changes over the span of a month and a half, we have noticed significant improvements in object allocations and GC frequencies.

![](https://cdn-images-1.medium.com/max/1024/0*zgaF5lssYVIaZez1.png)

![](https://cdn-images-1.medium.com/max/1024/0*GcgNCRdg5hjUCN1F.png)

The above approach also reduces class constant footprints. We had previously included the module in numerous classes and modules with an `include MemcacheKeys` just to access a constant for building the required key. Even though constants are included via reference and not duplicated, the class still has to maintain a reference to it. You can check the `module#constants` method. After the changes, key constants no longer need to be referenced in multiple places.

![](https://cdn-images-1.medium.com/max/1024/0*ycQL2ZmD56IIE85N.png)

![](https://cdn-images-1.medium.com/max/1024/0*rZbvcRhPTU2h5Q12.png)

* * *