patric-vinicios

patric-vinicios

Memory Consumption in Requests

I have a question about memory consumption when receiving a frontend request, for example.

In this first example, let’s suppose that the user created an order

"order": {
  "order_code": "123",
  "items": [
    {
      name: "item name 1",
      quantity: 1,
      price: 100.0
    },
    {
      name: "item name 2",
      quantity: 2,
      price: 100.0
    },
    {
      name: "item name 3",
      quantity: 3,
      price: 100.0
    }
  ]
}

And each item should have the same “order_code” too, for some logic business.
In the backend, we should map each item and copy the order_code inside it.

In this second example, the frontend is responsible for sending the order_code for each item and the backend should validate if the order_code in each item is equal to the order’s order_code.
(The customer don’t need to see this field as long as the frontend send as a “hidden” input, for example)

"order": {
  "order_code": "123",
  "items": [
    {
      name: "item name 1",
      quantity: 1,
      price: 100.0,
      order_code: "123"
    },
    {
      name: "item name 2",
      quantity: 2,
      price: 100.0,
      order_code: "123"
    },
    {
      name: "item name 3",
      quantity: 3,
      price: 100.0,
      order_code: "123"
    }
  ]
}

Creating some test using Grafana and Prometheus, I had some results where the request duration in milliseconds was higher in the first example than the second.

But, using erlang.memory(:total) the second example was consuming more memory than the first example for some reason.

I thought that in the first example the memory consumption would be greater, as another data is created in memory for the new items with order_code.

How does memory consumption work in this case?
Is it advisable to do this type of mapping in Backend or should we just validate the data?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The difference here is so small that testing methodology is going to matter a lot unless you’re taking about a very large number of records per request. Total memory in particular is going to be a very noisy way of measuring the impact of your JSON serialization.

Can you say more about your use case and benchmarking approach? Have you tried micro benchmarking with benchee for a more focused comparison?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It really shouldn’t. At the “dozens scale these operations should take microseconds not milliseconds and be nearly impossible to measure. Can you show a reproducible example of what you’re talking about?

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement