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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement