Understanding Rails · On the job
Ticket 002: Eagle Ridge's page is slow
A ticket as it arrived in Switchback, a small Rails app. Before you fix anything, make it ready to work: ask the reporter what you need to know, write what done looks like, then compare with how a senior refined it.
Helps to know first
1. The ticket, as it arrived
From: Morgan, the product manager
Eagle Ridge Loop's page has gotten really slow. It's been like this since the newsletter featured it last month and everybody started posting there. A hiker wrote in about it, below. Are we outgrowing the server? Should we pay for a bigger one?
I love the Eagle Ridge page, but it takes forever to open now, especially on my phone before I head out. Sometimes I give up and just go. The other trails open right away.
Ravi, hikes Eagle Ridge most weekends
2. Ask the reporter
Morgan sent it in. Morgan is the product manager, not an engineer, and is passing on what hikers said. Ask what you need to know; each question shows the answer. Some answers narrow the cause, and some don't.
Which browser or device are people using?
Morgan: All kinds. Ravi is on a phone, and I'm on my laptop in Chrome. It's slow on both.
Are other trails slow too?
Morgan: No. Cedar Falls and Granite Switchbacks open right away. It's only Eagle Ridge.
Has anything changed on the server?
Morgan: Not that I know of. Nobody has touched it. That's why I wondered if we've outgrown it.
How many notes does Eagle Ridge have?
Morgan: Hundreds now. I scrolled for a long time and didn't reach the bottom. Cedar Falls has two.
Is it slow when you're signed out too?
Morgan: Yes, it's the same either way.
When did it start?
Morgan: About a month ago. The newsletter featured Eagle Ridge, and lots of new people started posting notes there. It gets a little worse every week.
Do the notes have photos or videos?
Morgan: No, just text. Hikers have asked for photos, but we don't have them yet.
Is it slow only when the site is busy?
Morgan: I thought so, but no. I opened it at six on a Sunday morning, when hardly anyone is on, and it was just as slow.
Did it start when new notes began showing up live?
Morgan: No. Live notes came in the spring, and the page was fast all summer.
Write the ticket
What must be true when this is done? And what will this ticket leave alone? It stays in your browser.
3. Compare
Compare with the refined ticket
The refined ticket
A trail's page loads each note's author on its own
Story: As a hiker checking a busy trail before I go, I want its page to open as fast as a quiet trail's, so I can read the latest notes without giving up.
How to reproduce: Run bin/setup, or bin/rails db:seed, so Eagle Ridge Loop has about 400 notes. Open it and read the log. There's a User Load for each note, each pointing at app/views/notes/_note.html.erb, and the Completed line counts about 400 queries. Cedar Falls, with two notes, makes four.
Cause: The page draws @trail.notes, and each note's partial calls note.author.name. That loads the author with a query of its own: one query for the notes, then one more for each note. This is an N+1 query.
What you wrote
Acceptance criteria
Out of scope
What the refined ticket says
Acceptance criteria
- A trail's page makes the same number of database queries with 30 notes as with 3.
- Every note still shows its author's name and its words.
- Notes are still listed newest first.
- Posting a note still works, and followers still get their email about it.
Criterion 1 counts queries, not seconds. Time depends on the machine and on what else it's doing; the number of queries doesn't. A fix can load the authors in one more query or in the same query as the notes. Either way the count stays the same as the trail grows.
Out of scope
- Showing fewer notes at a time, with pages or a "Show more" button. How many to show is the product manager's call. A good next ticket.
- Caching the page.
- A bigger server. The page is slow when the site is quiet, so a bigger server wouldn't fix it.
What a senior asked first, and why
- Are other trails slow too? Same app, same server, same code, and only one page is slow. So the cause is something about Eagle Ridge itself, not the server.
- How many notes does Eagle Ridge have? What does Eagle Ridge have that the others don't? Hundreds of notes. When a page gets slower as a list grows, look for work the page does once for each thing in the list.
- When did it start? It started when the notes started piling up, and it gets worse every week. Slowness that grows with the data points at code that does more work for each note, not at a server that's too small.
- Is it slow only when the site is busy? Slow even when nobody else is on rules out traffic. A bigger server won't fix a page that does too much work on its own.
The device, signing in, photos, the server and live notes are fair questions. Morgan answered each one honestly, but none of them narrows the cause. After the first questions, a senior opens Eagle Ridge in their own copy and reads the log.
Work it
Now fix it, in your own copy of Switchback.
The branch is 002-eagle-ridge-slow.
- Use your fork of Switchback from ticket 001, or fork it now and clone your fork. Use mise, rbenv or asdf to get the Ruby in .ruby-version.
- Bring your fork up to date: on GitHub, click Sync fork, then pull. Seed the database so Eagle Ridge has its hundreds of notes, then open Eagle Ridge and read the log.
bin/rails db:seed
- Make a branch named for the ticket. The review finds the ticket by the number the branch starts with.
git switch -c 002-eagle-ridge-slow
- Write a test that fails because of the slow page. Then fix it, and commit with a message that says why.
- Push the branch and open a pull request inside your fork, from 002-eagle-ridge-slow into your fork's master. Change the base repository to your fork, as in ticket 001, or the review can't comment.
git push -u origin 002-eagle-ridge-slow
- A GitHub Action runs the ticket's acceptance tests on your pull request and comments with what passes, what fails and what crashed, and once all four pass, the questions a reviewer would still ask. Push again and the same comment updates.
4. Watch a senior work it
Once you've tried it, see how a senior refines and fixes the same ticket, dead ends and all.
Watch episode 102