Two-week programme · Day 12 of 14
Nearly all reads, on a mid-range phone
Part 1Basics
A feed is almost all reading. Here the challenge isn't losing data. It's speed: opening instantly, and scrolling smoothly on an ordinary phone.
The same idea helps again. Show what you already have on disk straight away, and fetch fresh content behind it. The rest is about doing as little work as possible while the user scrolls.
Words you need today
- Frame
- One redraw of the screen. At 60 per second, each has about 16 ms.
- Main thread
- The thread that draws the screen. Anything slow here makes scrolling stutter.
- LRU cache
- A store of limited size that removes whatever was used least recently.
- CDN
- Servers around the world holding copies of images, close to users.
- Keyset cursor
- Paging by the last item seen, so deep pages cost the same as the first.
- Cold start
- Opening the app when it isn't running at all, the slowest kind of launch.
The story
A shopper opens a social shopping app on a mid-range Android phone. The first page draws from a local cache, exactly as Priya's job drew from SQLite. A fresh page arrives behind it and slides in without moving what she's reading.
She scrolls, and the next page loads by cursor, just like Day 4's pull. Images arrive sized for the screen from a CDN. Two rows ahead start to load, one video plays, and everything else stays paused. Nothing she wrote needs syncing except likes, which go through a small outbox.
Last year this app took 1.8 seconds to start. Now it takes 4.1, and nobody can say why. That is your other problem today.
Part 2Today's question
What changes when the hard part is speed, not correctness?
By the end of this lesson, you should be able to:
- Keep a list smooth on a mid-range phone
- Build an image cache with cancellation
- Set and defend a startup budget
Part 3In detail
Step by step
- Open from cache. Draw the last saved first page immediately.
- Fetch fresh content. Ask for the newest posts, and don't move what the user is reading.
- Page by cursor. Near the end of the list, ask for the next 20 using the last cursor.
- Load the right image. Request the size the screen needs from the CDN, decode it off the main thread, and cache it in memory and on disk.
- Do less off-screen. Cancel loads for rows that scrolled away. Let only one video play.
Frames are a budget
At 60 frames per second you have 16 ms per frame. Decoding one large image on the main thread can spend several frames. So decode off the main thread, downsample to the view's size, and cache at each layer.
Caches at every level
Memory cache for this screen, disk cache for this week, CDN for everyone. An LRU evicts what you used least recently, and cancelling work for rows that scrolled away matters as much as caching.
Startup is a budget too
Most of cold start is work you chose to do early. Move SDKs to first use, show local data first, and fail CI when the budget is exceeded. Without that gate, the next feature undoes your fix.
One cursor shape, again
1GET /v1/feed?cursor=eyJ0IjoxNzk0...&limit=202→ { "items": [ … 20 posts … ],3 "next_cursor": "eyJ0IjoxNzk0...",4 "has_more": true }Opaque to the client, and keyset-based underneath, so page 500 costs what page 1 costs. Day 2 gave the local version of this rule.
The code, explained
This is the real code, and our tests run it. Read each block, then the notes under it.
Decoding images at the right size
Memory is the first thing a feed runs out of. Decoding at screen size is the biggest single saving.
1/** Decode a 4000 px photo for a 400 px row at 1/8 size: 64 times less memory, same look. */2fun inSampleSize(width: Int, height: Int, targetW: Int, targetH: Int): Int {3 var sample = 14 while (width / (sample * 2) >= targetW && height / (sample * 2) >= targetH) sample *= 25 return sample6}7 8fun bitmapBytes(width: Int, height: Int, sample: Int): Long =9 (width / sample).toLong() * (height / sample) * 4 // 4 bytes per pixelsample *= 2- Android's decoder works in powers of two.
bitmapBytes- Width times height times 4 bytes. A 12-megapixel photo costs 48 MB decoded, whatever its file size.
Compiled and tested by npm run test:mobile.
Merging pages without jumps
New posts shift the feed while someone scrolls, so the same post can land on two pages.
1data class Post(val id: String, val createdAtMs: Long)2data class Feed(val posts: List<Post> = emptyList(), val cursor: String? = null)3 4/** A post can slide onto two pages as new ones arrive, so duplicates are normal. Drop them by ID. */5fun Feed.append(page: List<Post>, next: String?): Feed {6 val seen = posts.mapTo(HashSet()) { it.id }7 return Feed(posts + page.filter { seen.add(it.id) }, next)8}9 10/** Fresh posts wait behind a pill unless the reader is at the top, so the list never jumps. */11fun Feed.onFresh(fresh: List<Post>, atTop: Boolean): Pair<Feed, Int> {12 val seen = posts.mapTo(HashSet()) { it.id }13 val new = fresh.filter { it.id !in seen }14 return if (atTop) copy(posts = new + posts) to 0 else this to new.size15}page.filter { seen.add(it.id) }- add returns false for a repeat, so duplicates simply drop out.
fun Feed.onFresh- Fresh posts wait behind a pill while reading, so the list never moves under the reader's thumb.
Compiled and tested by npm run test:mobile.
A startup budget that fails the build
Every team adds a little to startup. Only an automatic check stops it creeping up.
1data class Phase(val name: String, val ms: Long)2 3/** Fails CI, and names the phases to look at first. */4class StartupBudget(private val totalMs: Long) {5 fun check(phases: List<Phase>): List<String> {6 if (phases.sumOf { it.ms } <= totalMs) return emptyList()7 return phases.sortedByDescending { it.ms }.take(2).map { "${it.name} took ${it.ms} ms" }8 }9}phases.sumOf { it.ms } <= totalMs- Within budget: nothing to report.
sortedByDescending { it.ms }.take(2)- When over budget, it names the two slowest phases, so the failure tells you where to look.
Compiled and tested by npm run test:mobile.
What can go wrong
- Scrolling stutters
- Something heavy is on the main thread. Move decoding off it, and flatten row layouts.
- Memory fills up and the app is killed
- Downsample images to view size, and limit the memory cache.
- Rows repeat or vanish between pages
- Page by cursor rather than by offset, and drop duplicates by ID.
- Startup gets slower each release
- Delay SDK setup until first use, and fail CI when a startup budget is exceeded.
Decisions to defend
- What does the app show on open? Cached page first, fresh page behind it, not spinner until the API answers. The same choice as Day 1, for a different reason: speed, not signal.
Part 4Questions and answers
Answer out loud first, then open each one.
Fresh content arrives while she's reading. Do you insert it above her?
Why measure startup at p90 on mid-range phones?
Part 5Going further
The three questions, for today
- Where does it live?
- In the local cache, and then in the CDN for everyone else.
- Who runs the next step?
- The scroll itself. Nothing needs to run with the app closed.
- What if it runs twice?
- Duplicate image requests coalesce, and a cursor stops repeated rows.
Practise
- Mock45 minInfinite feed with images and video
Spend most of your time on the client: cache layers, scroll and video.
- Build35 minImage loader with coalescing and two cache tiers
Coalesce duplicate requests, and cancel what scrolled off.
Optional reference pages
You don’t need these for the lesson. They hold more detail if you want it later.
You have designed four products. Tomorrow you change altitude: 150 engineers ship into one app, and a release breaks Priya's sync.