Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-2287] Avoid fetching same state from redis multiple times #4055

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Oct 4, 2024

Fix #2950

When fetching a state from redis which has computed vars, it might get fetched twice when populating substates. To avoid this, pass a list of already-fetched substates to get_state and apply these before performing populate_substates. Then populate_substates skips fetching substates which are already in the tree.

Consolidate logic between the "deserialize instance" and "create instance" paths -- previously we needed the other path, because we had to instantiate a new state class with a valid parent_state, but with the merge of 319ff17, the parent_state pointer can be added later, so the two paths were equivalent.

Copy link

linear bot commented Oct 4, 2024

@masenf masenf changed the base branch from masenf/garden-variety-pickle to main October 4, 2024 02:19
masenf added 2 commits October 3, 2024 19:24
In the presence of computed vars, substates may be cached more than once.
@masenf masenf force-pushed the masenf/optimize-redis-get-state branch from cc00e56 to 42f8fcf Compare October 4, 2024 02:24
adhami3310
adhami3310 previously approved these changes Oct 4, 2024
reflex/state.py Outdated
@@ -2974,39 +2985,29 @@ async def get_state(
if redis_state is not None:
# Deserialize the substate.
state = BaseState._deserialize(data=redis_state)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to catch schema mismatch error

If the serialized state's schema does not match the current corresponding state
schema, then we have to create a new instance.
@masenf masenf merged commit 7529bb0 into main Oct 7, 2024
39 checks passed
@masenf masenf deleted the masenf/optimize-redis-get-state branch October 7, 2024 21:58
Kastier1 pushed a commit that referenced this pull request Oct 23, 2024
* Avoid fetching substates multiple times

In the presence of computed vars, substates may be cached more than once.

* Consolidate logic in StateManagerRedis.get_state

* Suppress StateSchemaMismatchError and create a new state instance.

If the serialized state's schema does not match the current corresponding state
schema, then we have to create a new instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REF-2287] Redis fetches the same state multiple times
3 participants