mkarnebeek

mkarnebeek

Streaming http response without running out of memory

I intent to stream a large XML document over http, containing a list of objects. Each object is to be converted into a struct and put inside an elixir stream. This stream is given to client code which does arbitrary data processing. The idea behind this is to not load the entire document or all structs into memory before passing it to client code.

The syntax would look something like:

stream("http://..")
|> Stream.map(...)

But my concerns already start at the HTTP library, handling async requests by sending http chunks as messages to another process. For example:

HTTPoison.get! "http://", %{}, stream_to: self

When i execute the following:

HTTPoison.start
HTTPoison.get! "http://mirror.nforce.com/pub/speedtests/250mb.bin", %{}, stream_to: self
:observer.start

I can see in the observer the memory usage increase and the messages to my process stacking up until the entire file is loaded. This (in my head) poses a problem, as the processing is potentially way slower that retrieving the file.

Should I be concerned about this? As far as I know it is not a good idea to load the entire file into memory. On the other end, data processing has been started at the moment the first object is received, and telling the webserver to back-off has it’s own problems (more open connections, taking server-side resources longer than needed, etc.)

First 2 of 2 Posts Switch mode

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would look at using :hackney directly. HTTPoison does not surface the API that :hackney has for doing demand driven http streaming.

josevalim

josevalim

Creator of Elixir

Yes, the HTTPoison just sends messages and doesn’t have any back-pressure mechanism. The end result is exactly what you are seeing. I would open up an issue in the HTTPoison issues tracker too, possibly linking back here, the :stream_to API seems to be a very dangerous one.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
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 Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement