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
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming












Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.