Skip to content

Commit

Permalink
Add memory fix post
Browse files Browse the repository at this point in the history
  • Loading branch information
jordinl committed Jan 18, 2024
1 parent 328ee0d commit 5202a45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: post
title: I once reduced response time by 80% with one line change
date: 2024-01-17
image: /assets/img/memory-fix.png
---

Shortly after I started a job I noticed we were storing, for each of our customers, Stripe customer data as JSON in
the DB. I actually said in a meeting that I thought it would be better to just store only the attributes we needed
as columns instead, but I was told that _I should not worry about it and that it worked beautifully_. It was
actually worse than that because Stripe allows to expand responses when querying their API so we were storing
customer data with their subscriptions; after that when we wanted to read this information we would hydrate Stripe
objects from the [Stripe ruby gem](https://github.com/stripe/stripe-ruby).

So a few months go by and I'm asked to investigate some memory issues on production. I start chasing down the issue
on my development machine, run some memory profiling and notice the Stripe gem is doing a lot of memory allocations.
Thought that couldn't be the issue so I start looking somewhere else. The actual pages with the biggest memory
usage allowed embedding custom images and CSS so I thought the issue would be there. I spent
some time looking into that, but I couldn't find what was the cause. So I started commenting out one piece of code at
a time until I noticed it was those Stripe JSON objects that were causing these huge memory spikes.

Everytime we wanted to check what type of Stripe subscription the customer had or if it was active we would hydrate
a Stripe object with the subscription data from the DB. The temporary fix I did was to memoize the hydrated Stripe
subscription so this inefficient process was done only once per request and not many times, I think I counted 6
times in a single request.

On my development machine I noticed an 84% reduction in memory allocation, so I knew it would improve things. When
we pushed the code to production I couldn't believe what I was seeing. I was looking at our APM tool to check
performance and had to refresh the page a few times because I thought it wasn't working right, mean response time
had dropped 80%, it also looked flat now instead of having lots of peaks and valleys.

<p class='center'>
<img src='/assets/img/memory-fix.png' alt='Memory Fix' />
The pink solid line represents new response time and the pink dotted line old response time
</p>
Binary file added assets/img/memory-fix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5202a45

Please sign in to comment.