skip to content

In an offline language-learning app, should the local database store each server response whole or normalize it into records, and why?

level: middleimportance: should knowfreq 48%

answer

  1. one entity, many responses
  2. copies drift apart after an offline edit
  3. one row per card, keyed by stable id
  4. responses are input, rows are truth
  5. whole is fine for read-only snapshots

basics

~20 s

Normalize data that screens query or the device edits into records keyed by stable ids, so one write updates every screen observing it; store whole only what is read whole, replaced whole and never edited on the device.

solid answer

~40 s

If the app stores each response under its URL, the same flashcard sits inside the lesson response, the due-cards response and the deck response. Grading it offline means finding and rewriting every copy, and any copy missed shows a stale due date. Normalizing gives each card one row keyed by its server id, with lessons and decks referring to it; a grade updates one row and every screen's observable query re-emits. Responses become input: the data layer parses them and upserts rows. It also lets the app answer queries the server never offered, like due cards across all lessons, offline. Storing whole still fits data that is read and replaced as a unit and never edited locally, such as a leaderboard snapshot.

code

json · 15 lines
json
{
  "card": {
    "id": "c_8f2c41",
    "deck_id": "d_spanish_a1",
    "front": "la manzana",
    "back": "the apple",
    "due_at": 1791331200,
    "interval_days": 4,
    "graded_locally_at": 1791298800
  },
  "lesson_card": [
    { "lesson_id": "l_food_1", "card_id": "c_8f2c41", "position": 3 },
    { "lesson_id": "l_review_week_2", "card_id": "c_8f2c41", "position": 11 }
  ]
}

go deeper

for a junior

Recall the two shapes, whole responses keyed by request and records keyed by stable id, and that duplicate copies can disagree.

for a middle

Explain how normalized rows plus observable queries let one write update every screen, and why the local schema follows the device's queries.

for a senior

Judge per data type: normalize what is edited or queried, keep read-only snapshots whole, and keep device-only columns from being overwritten by upserts.

for a principal

Weigh a shared normalized client schema across teams against per-feature snapshots, considering ownership of the schema and the cost of changing it in every release.

Once an app decides to keep server data on the device, the next design choice is its shape. There are two broad options, and interviewers ask which one you chose and why, because the wrong one quietly produces screens that disagree with each other. ## Two ways to keep server data - **Whole responses**: each response body is saved as one value, keyed by its request (for example the URL plus its parameters). Reading a screen means loading that blob and showing it. - **Normalized records**: each response is parsed into entities, and each entity is stored once, in its own row, keyed by a **stable id**. Relationships are stored as references, for example a join table between lessons and cards. ## What goes wrong with whole responses In the language-learning app the same card appears in several responses: the lesson that introduced it, the due-cards list and the deck overview. With whole-response storage: 1. **Copies multiply.** The card is stored three times, possibly at three different moments, so the three copies can already disagree. 2. **Local edits must find every copy.** Grading the card offline changes its due date. The app must open each blob that might contain it, patch it and save it again, or some screen keeps the old date. 3. **Queries are limited to what the server shaped.** "Due cards across all lessons, sorted" cannot be answered offline unless the server happened to send exactly that list. 4. **The store cannot notify precisely.** A blob is opaque to the database, so it cannot tell a screen that one card changed. ## Normalized records Normalizing turns responses into input rather than storage: - Each card is one row keyed by the id the server assigns, so a fresh response **upserts** it instead of creating another copy. - Lessons and decks refer to cards by id, so a card belongs to several lessons without being duplicated. - Device-only columns, such as when a grade was given locally, live beside the server's columns. How a pending local change is reconciled with a newer server value is the conflict policy's subject; the store's job is to hold both without one silently overwriting the other. - Indexes can be added for the device's own queries, which are often different from the server's endpoints. - A fresh response replaces the server's columns in place, so every screen observing that row sees the new value without the app tracking which screens showed it. The local schema follows the **device's queries**, not the server's database. It may hold fewer columns than the server and add some the server never sees. ## The screen observes a query With normalized rows, a screen subscribes to a query instead of loading a blob. When any write commits a change to the rows that query reads, the query emits again and the screen redraws. ```sql -- the due-cards screen observes this query SELECT card.id, card.front, card.back, card.due_at FROM card WHERE card.deck_id = ? AND card.due_at < ? ORDER BY card.due_at LIMIT 50; ``` A grade updates one `card` row. The due-cards screen, the lesson screen and the deck badge all observe queries over that table, so all three update from one write, with no code that hunts for copies. ## When storing whole is right | Data | Edited on device? | Queried inside? | Better shape | |---|---|---|---| | Flashcards and their schedule | Yes | Yes | Normalized rows | | Lesson progress | Yes | Yes | Normalized rows | | Lesson text and exercises | No | No, read whole | One document per lesson and version | | Leaderboard snapshot | No | No | Whole response, replaced on refresh | Normalization has a price: mapping code, a schema to maintain and schema changes across releases. It pays when the device edits the data, when an entity appears in several responses, or when the device needs queries the server never offered. For a read-only screen fed by exactly one response, keeping that response whole is cheaper and perfectly fine. ## Mistakes interviewers listen for - Mirroring the server's tables one for one instead of designing for the screens' queries. - Keying rows by list position, which changes with every response. - Normalizing everything, including blobs nobody will ever query. - Storing whole responses and then writing code that patches cards inside them, which is normalization done by hand, without an index and without notifications.

  • Isn't normalizing extra work when the server already shapes each response for its screen?
    Yes: mapping code, a schema and changes to it across releases. It pays when the device edits the data, when one entity appears in several responses, or when screens need queries the server never offered offline. For a read-only screen fed by one response, keeping that response whole is cheaper and fine.
  • Where do device-only fields go when a fresh server response upserts a normalized card row?
    Into their own columns, or a side table, that the upsert never writes. A refresh then replaces the server's fields and leaves the learner's local grade and its timestamp in place; deciding later which value wins is the conflict policy's job, not the store's.

An address book versus copying a friend's address into every letter you keep: when the friend moves, the address book needs one change, while the letters each hold an old copy you would have to find and correct by hand.

  • Saving each response whole under its URL is fine even when the device edits that data offline.
  • Normalizing means mirroring the server's database tables one for one on the phone.
  • Updating one copy of a card that appears in several stored responses is enough.
  • List position from the last response is a good key for a stored record.