-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Add request body compression with optional brotli #927
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
Open
mixalturek
wants to merge
7
commits into
master
Choose a base branch
from
brotli-compression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f26780a
feat: Compress requests using brotli algo
mixalturek 8e112bb
feat: Apply review comments
mixalturek 0dd99a9
feat: Refactoring of tests
mixalturek 3fffa62
feat: Use brotli quality=6; lower but faster
mixalturek 37f5a47
rm brotli from dev deps
vdusek 7617ed0
feat: Documentation for the compression
mixalturek 28f34e2
feat: Formatting
mixalturek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| id: compression | ||
| title: Request body compression | ||
| description: The client compresses every request body automatically, using brotli when available or gzip as a fallback. | ||
| --- | ||
|
|
||
| The Apify client compresses every request body before sending it to the API. This reduces the amount of data transferred over the network, resulting in faster requests and lower bandwidth usage — especially for large payloads such as Actor inputs, dataset uploads, or key-value store records. | ||
|
|
||
| ## How it works | ||
|
|
||
| The client selects the compression algorithm automatically at startup, with no configuration required: | ||
|
|
||
| 1. If the [`brotli`](https://pypi.org/project/brotli/) package is installed, the client uses brotli compression at quality level 6 (out of a maximum of 11) and sends the `Content-Encoding: br` header. Quality 6 is a deliberate trade-off: it compresses better than gzip while keeping CPU overhead low compared to maximum-quality brotli. | ||
| 2. Otherwise, it falls back to gzip compression and sends the `Content-Encoding: gzip` header. | ||
|
|
||
| The server supports both algorithms and decompresses the request body transparently. | ||
|
|
||
| ## Enabling brotli | ||
|
|
||
| Brotli is available as an optional extra. Install it alongside the client: | ||
|
|
||
| ```bash | ||
| pip install "apify-client[brotli]" | ||
| # or | ||
| uv add "apify-client[brotli]" | ||
| ``` | ||
|
|
||
| Once installed, the client detects it at startup and switches to brotli automatically — no code changes needed. To revert to gzip, uninstall the extra. | ||
|
|
||
| ## Comparison | ||
|
|
||
| | | Brotli | Gzip | | ||
| |-------------------------------|----------------------------------------|---------------------------------| | ||
| | **Compression ratio** | Better than gzip at quality 6 | Good | | ||
| | **CPU cost** | Moderate at quality 6 | Low | | ||
| | **Availability** | Requires the `brotli` extra | Built-in — no extra needed | | ||
| | **`Content-Encoding` header** | `br` | `gzip` | | ||
| | **Best for** | Large payloads where bandwidth matters | Minimal-dependency environments | | ||
|
|
||
| :::tip | ||
| For most workloads, the bandwidth savings from brotli outweigh the small CPU overhead. Install the `brotli` extra unless you are running in an environment where installing additional packages is not feasible. | ||
| ::: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.