Skip to content

Storage north star: bounded cache size, big caches, offloading old data, SQLite alternatives #80

Closed

Description

@neoromantic

#1 (PR #79) stops the cache from only growing: retention pruning, tg db vacuum, sizes in tg status, and a contentless search index. It doesn't settle how tg should store a whole account's history for years. This issue sets the goals for that, so later changes can be measured against them.

North star

  • Predictable size. A user can tell how big their cache will get and keep it under a limit they choose. A busy account with years of history stays at a size people accept on a laptop.
  • Big caches stay fast. Reads (messages search, list, tg sql), sync writes and startup don't slow down noticeably as a cache grows to millions of messages.
  • Old data can leave hot storage. History past an age or size threshold can be compacted, moved to an archive file or dropped from the local copy (it can be fetched again), without breaking the "messages are eternal" rule for what stays.
  • Nothing is lost silently. Compacting, offloading or migrating never drops data the user didn't ask to drop, and every step is reversible or documented.
  • tg sql stays useful. Agents and people can still query the cache with SQL, and the schema stays documented.

What we know (from #79, made-up data)

  • raw_json is the biggest part: 43% of the file for text-only messages (about 540 B per message) and 61% with 1 in 10 photos (about 1.25 KB per message).
  • A photo message's raw_json is about 7.7 KB because Uint8Array fields (fileReference, the stripped thumbnail) serialize as one JSON key per byte. Base64 took one such message from 7.1 KB to 1.3 KB. Per-row gzip helped little (706 B to 448 B).
  • The contentless FTS5 index saved 11–18% of the file.

Questions to answer

  1. raw_json. Encode binary fields compactly (base64), drop fields we can always rebuild, store a normalised subset, or compress with a shared dictionary (zstd with a trained dictionary instead of per-row gzip). What does tg sql lose with each?
  2. Media metadata. File references expire anyway; do we keep them, and in what form?
  3. Offloading. Archive old messages into separate files (per account and period, SQLite or Parquet), into a cold table, or drop them and re-fetch on demand? How do search and tg sql reach archived data?
  4. Limits. A configurable size or age budget per account, what happens at the limit, and how tg status reports it.
  5. Storage engine. Keep SQLite (bun:sqlite, built in, no native deps) or move part or all of the data to something else:
    • DuckDB: columnar and compressed, good for analytics and archives, but weaker at many small writes and needs a native package;
    • Turso / libSQL, or the Rust rewrite of SQLite: SQLite-compatible, but what do they add for a single-user local cache?
    • Parquet files for archives only, next to SQLite for hot data.
      Compare them on size, write rate for the daemon, query speed, binary size, cross-compiling all five release targets, and Bun support.
  6. Big caches. WAL size, index bloat, migration time on a multi-GB file, VACUUM time and the disk space it needs.

Done when

  • A benchmark on generated made-up data (for example 1M and 10M messages with a realistic mix) reports bytes per message, write rate and query times, and can run again later.
  • A short design doc in docs/ picks the approach for each question above, with numbers.
  • Follow-up issues exist for each change the doc picks.

Activity

  1. added
    enhancement
    on Oct 22, 2026
  2. neoromantic commented on Oct 22, 2026

    @neoromantic
    OwnerAuthor

    Related: #81 (getting the text and snippets back from the search index in tg sql) and #82 (semantic search, which adds vectors to the storage budget).

  3. neoromantic commented on Oct 28, 2026

    @neoromantic
    OwnerAuthor

    Decisions, 2026-10-28.

    Done in this round (in progress as PRs):

    Answers to the questions above

    1. raw_json: keep the full TL object, never a normalised subset. Byte fields as base64 (raw_json: store byte arrays as base64 #86, existing rows via Background, resumable migrations for big caches #89); drop fileReference once attachment metadata exists (raw_json: stop storing fileReference #95); the stripped thumbnail stays, because after a deletion on Telegram it is the only trace of a picture. zstd with a trained dictionary only if the benchmark shows a large saving and tg sql keeps working (raw_json: measure zstd with a trained dictionary #97).
    2. Media metadata: a per-account attachment metadata table, owned with Send and download media #22; the search index gets file names and kinds from it (Search attachment names, kinds and text derived from attachments #94).
    3. Offloading: messages are never deleted automatically. No eviction with re-fetch: it would lose messages deleted on Telegram, which only the local copy has. No hard size cap that deletes. An archive into separate files only if the benchmark shows the main file is too big or too slow (Archive old messages into separate files #101).
    4. Limits: a soft budget that warns and points at the biggest chats (A soft cache size budget per account #98, tg status --sizes: cache size per chat #90), and an option to stop caching live messages in chats with sync off (Option to cache live messages only in synced chats #100). The default behaviour doesn't change.
    5. Storage engine: stay on SQLite (bun:sqlite: no native dependency, one binary for all five targets, FTS5 built in). DuckDB needs a native package and is weak at the daemon's many small writes; libSQL/Turso adds nothing clear for a single-user local cache; Parquet only makes sense with an archive (Archive old messages into separate files #101). Revisit only if the 10M-message benchmark hits a wall.
    6. Big caches: migrations that rewrite every row must run in the background in batches (Background, resumable migrations for big caches #89). Integrity checks and backups: tg db check and tg db backup #91. Word forms in search: Search: find other word forms without a manual prefix #92, compared on the benchmark. Names in the index after a rename: Search: re-index older messages after a person or chat is renamed #93. Edit history: Keep earlier versions of edited messages #96.

    Still open here: the repeatable 1M/10M benchmark and docs/storage.md with numbers, as in "Done when" above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions