Skip to content

messages sync slows from ~75 to ~7 messages/s as the store grows; time goes to local processing #917

Done

Description

@lebedev-nikita

Summary

During a long messages sync of one large supergroup, throughput falls steadily as the store grows, from ~75 messages/s to ~7 messages/s. Telegram is not the bottleneck: one page of 100 messages arrives in ~270 ms. The time goes to local processing; on 0.134.0 the sync process sits at 100% CPU. At this rate, the remaining ~570k messages of the chat take about a day.

Environment

  • tg-cli 0.99.0, then 0.134.0; node 26.13.1; macOS arm64 (Apple silicon, SSD)
  • Store: schema 71, 186 chats, ~490k messages, wirecat.db ~540 MB, WAL ~15 MB
  • One chat: supergroup, ~1.1M visible message ids
  • No other process had the store open (lsof)

Command

tg personal messages sync <chat> --limit 2000000 --no-catch-up --background
# 0.134.0 additionally: --full-history

Measurements

Version Messages of this chat already stored Throughput
0.99.0 ~600 (start) ~75 msg/s
0.99.0 0 → 192k (first 2 h) 27 msg/s average
0.99.0 192k → 476k (next 7.5 h) ~10 msg/s average
0.99.0 ~477k ~6.6 msg/s (~15 s per 100-message page)
0.134.0 ~488k ~7.5 msg/s; process at 100% CPU

Foreground run on 0.134.0, 500 messages: 67 s wall, 41 s user, 21 s sys.

Same chat, same time, --trace on a single messages list --limit 100:

{"event":"response","operation":"messages.list","duration_ms":272,"outcome":"ok","counts":{"messages":100}}
not saved to the local store: database is locked
real 6.75

So a page costs ~0.3 s at Telegram, 1 s of default --pause, and ~13 s locally. While the sync ran, store status took ~6 s and other commands could not write (database is locked). On 0.99.0 the sync process showed ~0% CPU when sampled, so it may have been waiting on I/O or locks there; on 0.134.0 it is CPU-bound.

The per-page cost grows with the number of stored messages, so something in the write path appears to scale with the store or the chat's stored history rather than with the page. Each run also prints 500+ mentions found since the last review; mention extraction or the review count may be involved, but I have not profiled it.

Expected

The per-page write cost stays roughly constant, so a full backfill of a large chat is limited by Telegram's rate rather than by local processing.

Activity

  1. leemour commented on Nov 24, 2026

    @leemour
    Member

    The measurements helped a lot. Every page goes through one local save in our store, and the per-page cost grows with the chat's size, so something in that save scales with what's already stored. The "database is locked" in other commands fits this too. While one long save runs, the store is busy for everyone else.

    We're reproducing it now on a synthetic store of about the same size, to see exactly which step grows. I'll post what we find and the fix here. If you can, run one page with --trace on 0.134.0 again and attach the timings. That would help us compare.

  2. lebedev-nikita commented on Nov 25, 2026

    @lebedev-nikita
    Author

    0.134.0, same chat, no other tg process running. The local store already held ~543k messages of this chat (wirecat.db ~650 MB):

    time tg personal messages list <chat> --limit 100 --trace
    {"event":"request","operation":"messages.list","level":"operation"}
    {"event":"request","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1}
    {"event":"response","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1,"duration_ms":624,"outcome":"ok"}
    {"event":"request","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1}
    {"event":"response","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1,"duration_ms":189,"outcome":"ok"}
    {"event":"request","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1}
    {"event":"response","operation":"messages.getDialogs","level":"wire","transport":"mtproto","attempt":1,"duration_ms":88,"outcome":"ok"}
    {"event":"request","operation":"messages.getHistory","level":"wire","transport":"mtproto","attempt":1}
    {"event":"response","operation":"messages.getHistory","level":"wire","transport":"mtproto","attempt":1,"duration_ms":160,"outcome":"ok"}
    {"event":"response","operation":"messages.list","level":"operation","duration_ms":1128,"outcome":"ok","ids":{"chat":"-1001389068340"},"counts":{"messages":100}}
    {"event":"store","operation":"store.write","kind":"store","table":"accounts","op":"insert","n":1}
    {"event":"store","operation":"store.write","kind":"store","table":"messages","op":"update","n":114}
    {"event":"store","operation":"store.write","kind":"store","table":"message_counter_observations","op":"insert","n":22}
    {"event":"store","operation":"store.write","kind":"store","table":"attachments","op":"insert","n":16}
    {"event":"store","operation":"store.write","kind":"store","table":"messages","op":"insert","n":8}
    {"event":"store","operation":"store.write","kind":"store","table":"message_stems","op":"insert","n":6}
    {"event":"store","operation":"store.write","kind":"store","table":"message_stems_pending","op":"delete","n":6}
    {"event":"store","operation":"store.write","kind":"store","table":"mention_pending","op":"delete","n":6}
    {"event":"store","operation":"store.write","kind":"store","table":"mentions","op":"insert","n":1}
    {"event":"store","operation":"store.write","kind":"store","table":"involvements","op":"insert","n":8}
    {"event":"store","operation":"store.write","kind":"store","table":"involvement_pending","op":"delete","n":8}
    
    real 17.91
    user 9.72
    sys  5.26
    
  3. added
    bug
    area:search
    on Nov 25, 2026
  4. leemour commented on Nov 25, 2026

    @leemour
    Member

    Found it. Every saved message ran one UPDATE that read the whole chat, to link replies saved before their thread's root. No index covered that column, so the cost grew with the chat: a page of 100 took 78 ms at 10k stored messages and 919 ms at 90k. Now it runs once per page, 32 to 42 ms up to 60k (benchmark). Shorter saves should also mean fewer "database is locked" errors in other commands while a sync runs.

    The fix is merged (WireCatLabs/cli-core#3333, #924) and ships with the next tg release. An index in the next store version keeps it flat after that. I'll post the version here when it's out.

  5. leemour commented on Nov 25, 2026

    @leemour
    Member

    Released in 0.136.0. Update with npm install -g @wirecat/tg-cli@latest. The first sync after the update builds a new index once, after that pages should stay fast as the store grows. If it still slows down for you, reopen this issue with the message count and the rate you see.

  6. leemour commented on Nov 25, 2026

    @leemour
    Member

    One thing I missed above, it applies to #916 too. 0.136.0 also moves the store to version 73, and after that only tools on cli-core 1.0 open it. If you use other WireCat CLIs, update them before you run tg 0.136.0. gcal, drive, yandex-disk, bitrix24 and wordpress are not on cli-core 1.0 yet, so if you need one of them today, stay on tg 0.135.0 for now. The migration writes a rollback copy before it changes anything.

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

    area:searchbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions