For a music-streaming web app's search box, what should a tab's prefix cache hold, how long, and when can shorter prefixes answer longer ones?
answer
- backspace is the big win
- key by everything that changes the answer
- memory only, minutes, capped
- truncated top results cannot be narrowed
- provisional paint, then still ask
basics
~20 sKeep normalised queries, results and a completeness flag in memory for a few minutes, keyed also by account, market and content setting. A shorter prefix may answer a longer one only when its list was complete and matching is plain prefix.
solid answer
~50 sThe cache lives in the tab's memory and maps a normalised query to the grouped results, when they were stored, and whether the server returned every match or only its top few. Its key also carries what changes the answer: the account, the market and locale, and the explicit-content setting. Entries expire after a few minutes, the cache is capped with least-recently-used eviction, and it is cleared on sign-out; I would not write it to disk, because queries are search history. Exact hits, common after a backspace, repaint with no delay. Narrowing is the trap: filtering the top ten for 'rad' to answer 'radi' drops anything ranked eleventh for 'rad' but first for 'radi'. So a shorter prefix answers a longer one only when its list was complete and the match is a plain prefix the client can reproduce; otherwise the filtered rows are a provisional paint while the real request goes out.
code
json · 9 lines{
"key": "acct:7f3c|market:JP|locale:ja|explicit:off|q:radi",
"storedAt": 1791331200000,
"complete": false,
"groups": {
"artists": [{ "id": "artist:118", "title": "Radio Lanterns", "match": [[0, 4]] }],
"tracks": [{ "id": "track:5521", "title": "Radiant Hours", "match": [[0, 4]] }]
}
}go deeper
Remember that the tab can keep answers it already received, so backspacing to earlier text repaints instantly without asking the server again.
Explain the entry, the key and the lifetime, and the completeness rule that decides whether a shorter prefix's results may answer a longer one.
Show where narrowing silently breaks: truncated top results, fuzzy or kana-to-kanji matching, personal ranking. Say what you would ask the API for so the rule is checkable.
Weigh in-memory caching against the HTTP cache and server caching as one system: which layer answers which repeat, what each costs in privacy on shared machines.
## What the cache is for This is a cache **inside the browser tab**, held in memory by the page. It is not the suggestion service's own prefix cache, which is the server's business. In a **music-streaming web app** it pays off in a few everyday moments: - **Backspace and retype.** A user types "radio l", deletes back to "radio", and types again; every one of those states has already been answered. - **Reopening the dropdown** after a click away, with the same text in the box. - **Repeat searches** within the same session. - **Interim paints** while a request is in flight, if the rules below allow it. ## What an entry holds - the **normalised query** it answers; - the **grouped results** as the list renders them: ids, titles, types, artwork URLs, match ranges; - whether the server said the list is **complete** (every match) or **truncated** (its top few); - **when it was stored**, so it can expire. ## The key: everything that changes the answer | Part of the key | Why it belongs there | |---|---| | Normalised text | "Rad", "rad " and full-width forms are one query; the normalisation must match the server's | | Account | library matches and personal ranking differ per user | | Market and locale | what is available, and in which language, differs | | Content setting | an explicit-content filter changes what may be shown | A simpler equivalent is one cache per account, market and setting, emptied whenever any of them changes. ## How long it lives - **Memory only, for the life of the tab.** Queries and their suggestions are search history; on a shared computer a copy on disk outlives the session. Whether anything search-related is persisted, and how it is wiped on sign-out, is the offline and caching design's call. - **Expire after a few minutes.** The catalogue changes slowly, but personal parts drift, and a new release should appear without a reload. - **Cap the size**, say a couple of hundred entries, evicting the least recently used. - **Clear on sign-out**, on an account switch, and when the content setting changes. The browser's **HTTP cache** is an alternative for exact repeats: a GET per query with `Cache-Control: private, max-age=60` lets the browser answer repeats itself. It survives reloads, but it may write to disk, it is keyed by URL, and it cannot narrow a prefix. Some teams use both. ## When a shorter prefix may answer a longer one Answering "radi" by filtering the cached results for "rad" is correct only when **both** of these hold: 1. **The shorter answer was complete.** If the server returned its top ten out of thousands, filtering drops everything ranked eleventh or lower for "rad", including the artist who would be first for "radi". For a catalogue of millions, short prefixes are almost always truncated. 2. **The client can reproduce the match exactly.** A plain prefix match on the same normalised field can be redone in the tab. Typo tolerance, matches at word starts inside titles, the server's own accent folding, or matching kana typed against a kanji title usually cannot be reproduced exactly. Ranking is a softer third concern: the filtered rows keep the order the server gave for "rad", which is usually acceptable but may differ from the order it would give for "radi". When they hold, typically for long, specific prefixes where the server returned fewer than its limit, and always for the local sources held entirely in the tab, the box answers without a request. Otherwise the filtered rows are a **provisional paint**: shown at once, then replaced when the real answer for "radi" arrives. Asking the API to mark completeness explicitly, a field set when fewer than the limit matched, makes the rule checkable instead of guessed. ## Pitfalls - Caching **errors** as if they were empty results, so a blip shows "No results" for minutes. - Keying by **raw input**, so "Rad" and "rad" miss each other. - Narrowing **truncated** lists and quietly losing the best match. - Forgetting **in-flight** requests: retyping the same text before the first answer lands sends it twice. Sharing one pending request per key is the request layer's job; the cache just needs to see it. ## Summary Keep a small, short-lived, memory-only map from fully keyed, normalised queries to results and a completeness flag; answer exact hits instantly; narrow from a shorter prefix only when its list was complete and the match is one the tab can reproduce.
- Should failed or empty responses be cached in a music web app's suggestion cache?Empty answers yes, briefly, since they are real answers for that text. Failures no: a timeout or server error says nothing about the catalogue, and caching it would show 'No results' for minutes after a network blip. Leave the entry absent so the next keystroke or a retype asks again.
- When is the browser's HTTP cache a better fit than an in-memory map for suggestions?When exact repeats across reloads matter and the responses are not personal, such as catalogue-only suggestions for signed-out visitors. Then a short max-age lets the browser reuse responses with no client code. It still cannot narrow a prefix, and personalised responses need private caching or none on shared machines.
- Any longer prefix can be answered by filtering the shorter prefix's cached results.
- Key the suggestion cache by the raw input text alone.
- Persist suggestion responses to disk indefinitely; catalogue data never goes stale.
- A cache hit should still wait for the typing delay before painting.
- Cache failed requests as empty results so the box stops retrying.