stefanchrobot
I need to make a POST request with JSON of considerable size (~10MB). Is it possible to make such a request without spiking the memory usage?
Thanks to Jason.encode_to_iodata!/2, the payload is an iodata. I’m using Finch to send my request:
payload = Jason.encode_to_iadata!(data)
headers = [{"content-type", "application/json"}]
request = Finch.build(:post, url, headers, body)
Finch.request(request, MyFinch)
When the request is being processed, the Observer is showing a spike in memory usage:
Is there a way to avoid the second spike (with Finch or some other HTTP client)? I tried sending the payload as {:stream, list}, but I’m getting {:error, %Mint.TransportError{reason: :closed}}. Not sure if this is related to a specific server.
One of the values in the JSON is a pretty big blob, but I wrote a function that chunks it into smaller pieces, so I’d rule that factor out.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 10 of 11 Posts
mpope
Since there is a big blob maybe try iolist_to_iovec on the encoded json + see if you can open the TCP connection with the
socketbackend. inet — OTP 29.0.2 (kernel 11.0.2). I don’t see an option in Finch to forward socket opts, and Mint doesn’t seem use thesocketbackend by default so it might be tricky. A bit of a shot in the dark. Suggesting this because I did a bit of testing back last year and noticed that outgoing traffic consumed less memory (can’t 100% remember if it was binary), however incoming memory seemed to increase slightly. This was moving 1-4GBs between a few cloud VMs.mpope
I guess there is
inet_default_connect_optionswhich might be able to default the inet backend to socket? From the docsc4710n
How did you make this request in detail?
stefanchrobot
I’ve spent some more time on this and dropped down to Mint to have a better understanding of what’s happening. The
TransportErrorseems to be related to the specific server that I was testing with which I now replaced with a locally running Phoenix app.To recap: I’m sending out emails via REST, so I need to send quite a lot of JSON requests that share a big binary blob (bese-64 encoded attachment). I’m caching the blob, but I’m still facing memory usage spikes which in the end causes out of memory issues. Here’s the test script that I’m using to reproduce this:
I’m testing the following approaches:
It looks like only the last approach avoids memory spikes. Since it seems the problem only shows up with HTTPS and not HTTP, I’m guessing this has something to do with how the data is encrypted by the
:sslmodule. My working theory is that the last approach fits nicely with the encryption algorithm. But I’d prefer for the chunking to happen somewhere in the library code, not mine. Otherwise it feels like the solution is a bit magical and quite fragile.@ericmj @whatyouhide @voltone I’d appreciate if you could chime in on this.
D4no0
I highly doubt this is in any way related to ssl, as symmetric encryption used on the line creates very little overhead, it is more likely the overhead introduced by the json format and the base64 encoding, or maybe both. If you have possibility of changing the format of data, it would be much better to use a mechanism like multipart requests and ditch base64 entirely, because while your solution might work, I’m not sure on how easy it integrates with tools that collect telemetry, or how debugable should something go wrong.
stefanchrobot
Doesn’t seem like this has anything to do with JSON or base64 encoding. I’ve changed the script to POST plain text and the results are the same (the original script already worked with iodata and the content was pretty much irrelevant at that point).
mpope
Could the TLS support be flattening the iolist into a single binary causing a large allocation?
stefanchrobot
I traced what Mint is doing and dropped down one level to
:ssland I consistently get the same results:If you uncomment the line with the call to
chunk_binarythe memory spike disappears.mpope
Interesting. One thing in the ssl application that caught my eye was the
tls_record:encode_datafunction. Consider the following:That seems to create a 2.5 MB spike even though the function doesn’t complete successfully. Now when applying the chunking, the delta doesn’t occur when calling that function (although the total memory seems generally higher, ~50MB instead of ~44MB in my shell). I also called the
:tls_record.encode_datafunction without wrapping the chunked binary in a list. Not scientific but could be a good place to dig in further. Changingbeast_mitigationtodisableddidn’t seem to help, either.stefanchrobot
Thanks, I’ll dig into that! FYI I cross-posted this to the Erlang forum too.