Yesterday I shipped my biggest ever React App performance improvement. You'll never guess what the tiny change was 😛
After working on a React app for 2.5 years, it was starting to feel clunky. Nothing obvious, just a general sense of clunky slowness.
But hey, at almost 40,000 lines of code and hundreds of unique components, maybe that's expected? We are running a lot of code and throwing around many re-renders.
Users were starting to notice. My girlfriend uses our app and she always tells me "Your thing is slow as shit". But user metrics looked good so this wasn't prioritized.
Until last week our platform team raised the alarm.
To the metrics!
"Emergency meeting! Wtf are you people doing with our server?"
Our server was struggling. Had been for months. The platform team fixed all they could, now they needed help from us product teams. What the hell are our clients doing to this thing?
Here's a good article, if you wanna read more about the details of Node's event-loop model and how it impacts performance.
The team constructed an API impact chart for us. Number of requests per week * p95 latency.
We are fetching a patient's appointments 1,000,000+ times per week and 95% of those requests "take less than 1.1 seconds". We have a few thousand users 🤨
Impact: over one billion.
An old sin
At this point I got a tingle in the back of my brain. "Aren't we always joking that our backend-for-the-frontend loads appointments on every request?"
Oral tradition is important. Those little jokes, japes, and anecdotes are your lore. They survive when teams evolve and they hold a nugget of truth. Like a myth based on reality.
We dug in and yep, deep in our session middleware, a line of code that pre-fetches appointments from the data server. On every request.
A 4 year old performance optimization nobody needs. A backlog ticket from 2021 when someone found this, but couldn't remove.
The one line fix
We looked and the code was dead. Anything that used session data had been moved to React by now and would load its own data when needed.
The fix was simple:
Sometimes the best fix is a smöl change after a long investigation.
My coworker left a pull request comment that I will frame and put on the wall in my office ❤️
<3 honestly, this is probably the most impactful removal of a single line of code in Tia's history. And it was entirely enabled by the transition to React. Great job :)
Did performance improve?
Preliminary estimates show we went from calling that endpoint 150,000x/day to about 1,000x/day. p95 latency dropped by half. Server's less busy 😅
You can even see when the fix went live on a chart of database CPU usage.
And yes, the app feels subjectively snappier. Or I'm imagining it. It's hard to tell. But I can tell you this: every API request the app makes is faster. Up to 1 second faster. Because it doesn't pre-load appointments.
PS: for another fun story about following metrics to the unlikeliest of places, check out Immutability isn't free
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️