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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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...
3268 119930 1237
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement