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

State pickle compression #4430

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

benedikt-bartscher
Copy link
Contributor

@benedikt-bartscher benedikt-bartscher commented Nov 24, 2024

Turns out, state compression can make a big difference. Of course depending on the circumstances (Redis Bandwidth, Latency, State Size, ...). You can run REDIS_URL=redis_host:redis_port pytest benchmarks/benchmark_pickle.py to test it on your machine.

Will probably evaluate other compression algorithms (especially those integrated into python) in a followup and introduce a ReflexCompressor Interface.

@benedikt-bartscher benedikt-bartscher changed the title WIP: State compression State compression Nov 24, 2024
@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review November 24, 2024 23:16
pyproject.toml Outdated Show resolved Hide resolved
reflex/state.py Outdated Show resolved Hide resolved
@benedikt-bartscher
Copy link
Contributor Author

benedikt-bartscher commented Nov 25, 2024

@masenf we could add a compression threshold for small payloads. WDYT?

Edit: I just implemented it

reflex/state.py Outdated Show resolved Hide resolved
@benedikt-bartscher benedikt-bartscher marked this pull request as draft November 25, 2024 20:04
@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review November 25, 2024 20:50
@benedikt-bartscher
Copy link
Contributor Author

While this can help a lot with big states, I think frontend payload compression could have an even bigger impact. Especially with slow connections, f.e. on mobile. Pickle payloads already benefit from referential serialization, json/ws does nothing similar. In fact, currently, the ws payloads are not compressed at all. Socketio only compresses in long-polling mode, which is only used for the initial connection and incompatible with gunicorn (I disabled it in #4441)

@masenf
Copy link
Collaborator

masenf commented Dec 12, 2024

In fact, currently, the ws payloads are not compressed at all.

how did you determine this? i'm looking at a packet capture of the websocket traffic and almost every message seems compressed

Frame

image

Decompressed payload

image

@benedikt-bartscher
Copy link
Contributor Author

@masenf i was just brainstorming in that comment and only read some docs. I did not test it. Just confirmed your wireshark capture, seems to be compressed by default. Interesting, i wonder where this happens, maybe i'll need to scroll trough some code to understand it.

@benedikt-bartscher
Copy link
Contributor Author

It is just not documented very well. python-socketio uses python-engineio which uses simple-websocket, which has compression enabled with hardcoded default settings since miguelgrinberg/simple-websocket@9277e67

@masenf
Copy link
Collaborator

masenf commented Dec 12, 2024

In the JS version of socket.io, perMessageDeflate is now disabled by default: https://socket.io/docs/v4/server-options/#permessagedeflate

This extension is known to add a significant overhead in terms of performance and memory consumption, so we suggest to only enable it if it is really needed.

@benedikt-bartscher
Copy link
Contributor Author

benedikt-bartscher commented Dec 12, 2024

Very intersting, thanks for sharing. Maybe some (optimized) reflex apps could even benefit from disabling that compression. For big states/apps it's probably good that it's enabled. However with python-socketio reflex has no way to control this. Maybe we can adress that in the future. However we are getting a bit off-topic in this PR which is about pickle compression. Maybe i'll create an Issue to further discuss our performance impact and ideas for websocket compression.

@benedikt-bartscher benedikt-bartscher changed the title State compression State pickle compression Jan 12, 2025
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.

2 participants