akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

I am curious if anyone has experimented with using persistent_term (persistent_term — OTP 29.0.2 (erts 17.0.2)) for static assets as opposed to reading from the file system? Obviously you would want to do this in production only so you are not constantly cleansing persistent_term during development. And you would want something in place to hydrate your persistent_term prior to starting your endpoint. But mostly curious if the idea is crazy or not.

I forked the Plug.Static plug and modified it to leverage persistent_term and the initial results looked good.

Showing Posts 1 to 9

pera

pera

Nice results. I don’t serve static assets with Cowboy but I am using persistent_term to persist some computationally intensive function calls (mostly parsing and graph construction). I wrote the following helper macro for this purpose:

  defmacro defpersistent([{name, value}]) do
    atom = quote(do: {__MODULE__, __ENV__.function})
    function = Macro.var(name, nil)
    quote do
      def unquote(function) do
        case :persistent_term.get(unquote(atom), nil) do
          nil ->
            value = unquote(value)
            :persistent_term.put(unquote(atom), {value})
            value
          {value} ->
            value
        end
      end
    end
  end

I then have function declarations that look like this:

defpersistent function_name: expensive_computation()
def expensive_computation() do
  ...
end
voltone

voltone

Plug.Static, through Cowboy and Ranch, offloads the actual transmission of the data to the OS, using :file.sendfile/5. I imagine it has to do more work initially than your alternative, but depending on the file size and the achievable transmission rate it may actually be more efficient when the transfer takes longer.

It might be interesting to run some more tests with different file sizes and transmission rates (preferably on a real network interface). Maybe the optimal solution is a mix, serving small files from persistent_term while falling back to sendfile/5 for large files.

hauleth

hauleth

IIRC there will be no difference as soon as the files are in memory. The difference would be only if the files are served from the FS, as then sendfile can work fully within kernel, without going through user space.

voltone

voltone

Isn’t that what we’re comparing here: Plug.Static serving up a file, versus an alternative that serves content from memory, in this case persistent_term?

akoutmos

akoutmos OP

Author of Build a Weather Station with Elixir and Nerves

I’ll put together some more tests tonight with files larger than 1MB and see how things behave. I’ll also throw this on a separate server so it’s not on local host. Will report back with results!

hauleth

hauleth

Ahh, I missed that part. In such case Plug.Static with sendfile should be faster in almost all cases (assuming that the storage is quick).

voltone

voltone

Well, if the socket can accept the entire file contents at once, without blocking, then it might be faster to just dumpt the data into the socket from memory, rather than do the system call dance with sendfile.

But if the file is larger than what the socket can handle instantaneously, causing it to block on write, then the BEAM as to go off and do other work, and come back later when the socket is ready for more. That is overhead that you don’t get with sendfile. This is why I suggested to test with different file sizes, and why testing over the loopback interface is not realistic, since it probably has a humongous TCP window size, so the socket will never block for writing.

derek-zhou

derek-zhou

Interesting! For purely static assets, we can use a front-end server like nginx. However there could be some quasi-static pages that change infrequently but are visited frequently, maybe we can pre-render them and stash them here?

al2o3cr

al2o3cr

Another potential performance pitfall with large files: getting the data from persistent_term to the socket is going to cost at least one copy of the data (from the BEAM to the kernel) where sendfile is only transmitting a “get the data from over there” message.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
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
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews