Capstone - An Offline Expense Tracker

Article · 5:00

Migrations across shipped versions, FTS5 search, and a backup and restore you have actually performed, assessed on evidence.

The system. An offline-first mobile expense tracker. It records expenses against categories and tags, searches notes with full-text search, survives schema upgrades from any previously shipped version, and can back itself up and restore.

Three invariants must hold at all times:

  1. Every expense references a category that exists, with foreign keys enforced.
  2. Any cached aggregate - a monthly total, a budget's spent column - agrees with the sum of the underlying rows.
  3. integrity_check and foreign_key_check both pass after every migration and every restore.

What is assessed. The curriculum's weighting is 20% knowledge checks, 35% laboratory portfolio, 15% troubleshooting practical and 30% capstone. The capstone is marked on three things.

Correctness, including under interruption. Not that the application works, but that it is still correct after the process is killed mid-write, mid-migration and mid-restore. Your test suite must do all three.

Measured performance against a declared baseline. Declare it first: dataset size, device or machine class, and the p50, p95 and p99 for your five core operations. Then measure. An FTS5 search over 100,000 notes and a monthly aggregate over three years of data are the two to watch.

A defensible account of failure modes and limits. What happens when the device is full? When the user restores an old backup over a newer schema? When two devices have both been edited offline? That last one has no clean answer in SQLite, and saying so clearly - with your chosen compromise and its consequences - is worth more than pretending otherwise.

The deliverables: architecture diagram; data model with the reasoning for the model you chose over the one you rejected; source code; a reproducible test dataset; a benchmark report; a security review covering the threat model, file permissions and your encryption decision; a recovery runbook with measured RPO and RTO; and a short design defence.

The design defence is where marks are most often lost. Name the trade-off you are least comfortable with. For this capstone it is usually synchronisation - SQLite gives you no answer, and whatever you chose has a cost you should be able to state.

-- ================= The invariants, as runnable assertions ==========
-- Put these in CI, and run them after every migration and restore.

-- 1. No orphaned references.
PRAGMA foreign_key_check;              -- expected: no rows

-- 2. Cached aggregates agree with the rows beneath them.
SELECT b.category_id, b.month, b.spent_minor AS cached,
       coalesce(sum(e.amount_minor), 0) AS derived
FROM budget b
LEFT JOIN expense e
       ON e.category_id = b.category_id
      AND strftime('%Y-%m', e.spent_at) = b.month
GROUP BY b.category_id, b.month, b.spent_minor
HAVING b.spent_minor <> coalesce(sum(e.amount_minor), 0);
-- expected: no rows

-- 3. The file itself is sound.
PRAGMA integrity_check;                -- expected: ok

-- And the FTS index actually matches the table, which is the failure
-- that is otherwise completely silent:
SELECT (SELECT count(*) FROM expense WHERE description IS NOT NULL)
     - (SELECT count(*) FROM expense_fts) AS fts_drift;
-- expected: 0

-- ================= The interruption tests ==========================
--
-- Three kills, and all three must leave a consistent database:
--
--   1. SIGKILL mid-write
--      kill -9 during a transaction; reopen; integrity_check = ok and
--      the partial transaction is absent.
--
--   2. SIGKILL mid-migration
--      kill -9 between two migration statements; reopen; either the
--      migration completed or it did not happen at all. If your
--      migration is split across several transactions, this is where
--      you find out.
--
--   3. SIGKILL mid-restore
--      kill -9 while restoring a backup; the ORIGINAL must still be
--      intact, which means restoring to a temporary file and renaming
--      atomically, not writing over the live database.

-- ================= Declare the baseline BEFORE measuring ===========
--
--   Dataset:   100,000 expenses, 3 years, 40 categories, 200 tags
--   Device:    mid-range Android, or a 2 vCPU container
--   Claim:     insert expense        p99 < 10 ms
--              monthly report        p99 < 50 ms
--              FTS search, 100k rows p99 < 30 ms
--              full backup           < 2 s
--              migration v1 -> v6    < 5 s
--
-- Then measure, and report the one that missed. There is always one.

-- ================= The failure modes ===============================
--
-- Each is a row in your report: what happens, what the user sees,
-- what the application does about it.
--
--   device storage full
--     writes fail with SQLITE_FULL; a VACUUM needs 2x and will also
--     fail -> check free space before offering a backup
--
--   old backup restored over a newer schema
--     user_version is LOWER than the code expects -> migrations run
--     forward, which is fine. The reverse - a backup NEWER than the
--     code, after a downgrade - has no safe answer. Detect and refuse.
--
--   two devices edited offline
--     SQLite offers nothing here. Last-write-wins loses data silently;
--     a change log with per-row timestamps and a merge rule is the
--     usual compromise; CRDTs are correct and expensive. Whatever you
--     chose, state what it loses.
--
--   WAL file grows unbounded
--     a read transaction was left open -> checkpoint on backgrounding
--     and audit for unclosed cursors
--
-- Verify after every recovery with the four assertions above, not by
-- checking that the application launched.

Where to go from here

Longer reading about this subject, written alongside the course.

Course guide How to work through this course What each module is for, the four things to build alongside it, and the habits that separate using SQLite from engineering with it. 2 min read

Discussion

No questions on this topic yet. Sign in to ask the first one.