← all writing

what was making mamaskin's ingredient scans slow?

all right, this time we might have a memory issue. let me explain.

i was looking at mamaskin's supabase dashboard and the committed memory kept going up. was that just the app growing? was something doing more work than it needed to? i wanted to understand it before changing things.

so i asked codex to go through the code and the production metrics without overloading the database while trying to work out why it's using more memory.

one thing that came up was ingredient scans. some requests were taking several seconds on the server.

for context, mamaskin is an app i built for checking skincare ingredients during pregnancy and breastfeeding. you can scan a label or paste an ingredient list, and the backend matches that text against the ingredient catalogue.

sooooo, what's going on here?

that part of the app used the whole ingredient catalogue. when it needed a fresh copy, it fetched the lot in pages and built a lookup in memory.

there was already a cache. requests could reuse a recent copy or share a load that was already running. but when it needed to load everything again, that work was still there.

product pages already had a lookup that fetched a smaller set of ingredients. could we do something similar for scans?

i asked codex to build and test that locally first. my condition was pretty simple. it still had to find the same ingredients and return the same scores.

the idea was to take the possible ingredient names from the text and ask the database for those records. an index helps it find them directly, then the existing matcher works through that smaller set.

of course, ingredient lists aren't always neat. there are different names for the same thing, accents, extra words and text with missing commas. the lookup had to cover the combinations the matcher already understood, including names with accents and other characters.

before, a fresh load fetched all ingredient records for the matcher. the new path sends ingredient text through an indexed lookup and passes a smaller set of records to the matcher.

okay, but what happens if that smaller lookup can't handle the text?

it goes back to the full catalogue. that happens if there are too many possible names to look up, the result comes back incomplete, or the lookup fails. it can also keep using a valid full copy that's already cached.

then came the checking. does it actually return the same results?

codex ran the old and new versions locally against the same saved catalogue. that snapshot had fewer records, and the tests covered product formulas, individual ingredient names and messy text with missing separators.

113,683 test inputs used the index and returned the same matches, in the same order, with the same scores, evidence and overall risk. another 38 hit the lookup limit and used the original path. the indexed cases fetched about 34 ingredient records each on average.

there were checks for failed lookups, incomplete results and requests sharing a full load too. five example ingredient lists were also run through both versions locally using a small set of current catalogue records. same results there too.

after that, i approved the rollout. i wanted the matches and scores checked, and a comparison of scan times and committed memory afterwards.

the database index and lookup went in first and were verified before activating the new backend code. the previous version was kept available in case we needed to go back. this could reach existing app versions without waiting for another store release.

the next morning i opened a product page and tried an ingredient scan in the app. both worked. here's what the overnight logs showed.

server-side ingredient analysis before and overnight after rollout
server-side ingredient analysis before overnight after
successful requests 26 of 26 32 of 32
median time 1.25 seconds 0.30 seconds
p95 time 6.44 seconds 2.50 seconds

p95 gives an idea of how long the slower requests took.

there are only 26 requests before and 32 after, though. the first window was a few hours on 6 november, the second ran overnight into 7 november. different traffic, different times. i'd want a longer look before calling that the usual performance.

these are timings for the server part, including authentication, the lookup and the analysis. taking the photo, reading the label on the phone, sending the request and showing the result all add their own time.

29 of those 32 scans used the index and fetched between 5 and 89 ingredient records. the other three hit the lookup limit and went back to the full catalogue. their slower times are included above.

and the memory chart that started all this?

still looking into it. committed memory stayed roughly flat overnight. product-page requests also took longer in that window, so there was more to check there too.

← back to writing