Faster first page load: one-lock hydrate from the connect packet, diffed against compiled defaults

Merged
FA
2mo ago
+1743-183

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Performance improvement (non-breaking for apps; the frontend/backend boot protocol changes, which the existing version-subprotocol check already requires to match)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

What changes

First load of a Reflex app used to be: HTML, JS, websocket upgrade, wait for the socket.io connect ack, then three events (hydrate, update_vars_internal, on_load_internal), each its own state lock cycle, with hydrate serializing every var of every substate even though the bundle already carries the same defaults.

Now:

  • One boot event, inside the connect packet. The frontend sends a single hydrate_and_load event as socket.io auth, so the backend starts loading state as soon as the namespace connects, one round trip earlier. The event resets and applies client storage and snapshots the state under one lock. For a page with on_load handlers it then chains on_load_internal, which locks only its leaf substate and still supersedes, so a navigation can cancel stale page-load work but never the snapshot. Enqueuing the boot event also cancels the previous page's unfinished on_load chain (as the connect's on_load_internal did before), without the boot event itself being cancellable.
  • Diff against the compiled defaults. On the first hydrate of a page the frontend sends a digest of its compiled state names plus short per-state hashes of its compiled initialState; for every state whose compiled defaults match the backend's, only vars that differ are sent. Reconnects and storage resets still get the full state. States defined after the snapshot was taken (dynamic component states) invalidate the cached snapshot; unknown states and malformed hash lists are sent in full.
  • Fewer redis operations per load. A state-tree save is now one server-side script that checks the lock and writes every touched substate atomically (one round trip instead of a lock GET, a PTTL and one SET per substate, and safe under the client's command retries), is_hydrated is flipped from the OnLoadInternalState leaf instead of the root (which made the redis manager fetch and persist every substate), and connecting no longer opens the whole state tree just to stamp the session id.

State.hydrate is unchanged and still used by the shared-state rehydrate and by the compatibility path for events arriving on an unknown token.

Measurements

Production build of a 20-substate app with an on_load handler and a cookie var, redis state manager, behind a TCP proxy adding RTT and a 4 Mbps cap. Websocket compression disabled in the rig because granian (the production server) does not negotiate permessage-deflate. Medians of 7 to 9 fresh-session loads.

MetricBeforeAfter
Time to hydrated, 40 ms RTT, 4 Mbps716 ms613 ms
Time to hydrated, zero latency236 ms195 ms
Hydrate payload on the wire36.5 KB2.1 KB
Websocket frames after connect3 out, 5 in0 out, 3 in
Redis commands per first load29155
Backend CPU per first load99 msabout 70 ms

Two further ideas were implemented, measured, and dropped: hydrating only the states a page renders (flat on both a light and a heavy app, and risky with rx.memo bodies), and enabling the opportunistic lock by default (55 to 34 redis commands, about 15 ms, but a locking-behavior change better left opt-in).

Tests

  • New unit tests: hydrate_and_load single-lock flow and cookie reset/apply, diffing against compiled defaults and the full-snapshot fallbacks, the one-command lock-checked tree save and its discard when the lock changed hands (run against both the mock and a real redis), the boot event carried in on_connect auth, a navigation queued behind a lock-waiting hydrate still receiving the snapshot, and a reconnect cancelling the previous page's unfinished on_load chain.
  • Full unit suite green locally (tests/units/reflex_cli excluded: the hosting CLI rejects the dev version string, identical on the base commit).
  • Browser integration tests run locally on the branch: client storage, connection banner (reconnect/rehydrate), dynamic routes, linked and shared state, navigation, state inheritance, event chains, login flow, router query, frontend path, mount target, stateless app, lifespan (240 passed).

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_01JHj3TDXkYX4QNoUm28RLWG
https://claude.ai/code/session_01QLryBt5fVQStw7MiURtwbC