akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

Persistent_term for static assets using Plug?

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.

Most Liked

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.

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.

Last Post!

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.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement