<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chamseddine Benhamed on Medium]]></title>
        <description><![CDATA[Stories by Chamseddine Benhamed on Medium]]></description>
        <link>https://medium.com/@chamseddine-benhamed?source=rss-8bd1d90bf9e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*L3u9luv9hcvrFJQ8quBvuQ.jpeg</url>
            <title>Stories by Chamseddine Benhamed on Medium</title>
            <link>https://medium.com/@chamseddine-benhamed?source=rss-8bd1d90bf9e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 09:26:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chamseddine-benhamed/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding the Reasons Behind the Increase in ‘Max Used Memory’ Values Between Consecutive…]]></title>
            <link>https://chamseddine-benhamed.medium.com/understanding-the-reasons-behind-the-increase-in-max-used-memory-values-between-consecutive-6a160a6bbb7f?source=rss-8bd1d90bf9e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a160a6bbb7f</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-cloud]]></category>
            <dc:creator><![CDATA[Chamseddine Benhamed]]></dc:creator>
            <pubDate>Wed, 31 May 2023 09:06:16 GMT</pubDate>
            <atom:updated>2023-06-01T07:41:28.992Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding the Reasons Behind the Increase in ‘Max Used Memory’ Values Between Consecutive Invocations of AWS Lambda Functions</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mCmAWsOlY9pStXE3xu3Mug.jpeg" /></figure><p>The official AWS <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/log-structure.html">documentation</a> defines “<strong>Max memory Used</strong>” as the actual memory used during invocation. However, I have observed that in most cases, the value of “Max memory Used” either remains the same or increases between consecutive invocations of the lambda function.</p><p>In this article, I aim to provide answers to the following questions:</p><ul><li>Why does the “Max memory Used” value tend to increase between consecutive invocations? Could this indicate a memory leak?</li><li>Shouldn’t we expect the “Max memory Used” value to be consistent across invocations? How is this value calculated?</li></ul><p>To address these questions, I have created a lambda function with the following code and conducted various tests. Let’s explore the different scenarios below:</p><pre>import json<br><br>def lambda_handler(event, context):<br>    if event[&quot;condition&quot;] == &quot;A&quot;:<br>      data = [f&quot;{x}&quot; for x in range(10**7)] # condition A<br>      print(&quot;Condition A&quot;)<br>    if event[&quot;condition&quot;] == &quot;B&quot;:<br>      data = [f&quot;{x}&quot; for x in range(10**6)] # condition B<br>      print(&quot;Condition B&quot;)<br>    if event[&quot;condition&quot;] == &quot;C&quot;:<br>      data = [f&quot;{x}&quot; for x in range(10**5)] # condition C<br>      print(&quot;Condition C&quot;)<br>    else:<br>      data = [f&quot;{x}&quot; for x in range(10)] # condition D<br>      print(&quot;Condition D&quot;)<br>    return {}</pre><p>We can deduce from the code that the size of the data array is dependent on the conditions fulfilled upon invoking the lambda function. Condition D triggers a creation of a data array with 10 strings, condition C results in a data array with 100,000 strings, condition B generates a data array with 1,000,000 strings, while condition A creates a data array with 10,000,000 strings. It is evident that invoking the lambda function with an event that meets condition A requires significantly more memory compared to invoking it with an event that satisfies condition D.</p><p><strong>Scenario 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*F574uGXhQ7GRdkdnD75a4g.png" /></figure><p><strong>Scenario 2:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*QGrAX_917-bvq46sBTHF9w.png" /></figure><p><strong>Conclusions:</strong></p><p>After examining the aforementioned results, we can draw the following conclusions:</p><ol><li>“Max Memory Used” refers to the <strong>maximum</strong> amount of memory that was <strong>used by a lambda container</strong> during its usage.</li><li>An increase in the “Max Used Memory” value can occur due to changes in the memory requirements of the Lambda function between invocations. The function might require additional memory to store data in variables, causing a rise in the “Max Used Memory” value.</li><li>An increase in the “Max Used Memory” value does not necessarily mean that there is a problem with the Lambda function. However, if the value consistently increases over time, it may indicate a need for optimization or a potential memory leak that should be investigated.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a160a6bbb7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[@OneToOne relationship with spring data JPA, how to avoid N+1 problem when fetching a list of…]]></title>
            <link>https://chamseddine-benhamed.medium.com/onetoone-relationship-with-spring-data-jpa-how-to-avoid-n-1-problem-when-fetching-a-list-of-73239dcd4cfa?source=rss-8bd1d90bf9e------2</link>
            <guid isPermaLink="false">https://medium.com/p/73239dcd4cfa</guid>
            <category><![CDATA[jpa]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[spring-data-jpa]]></category>
            <dc:creator><![CDATA[Chamseddine Benhamed]]></dc:creator>
            <pubDate>Tue, 16 Mar 2021 17:13:16 GMT</pubDate>
            <atom:updated>2021-03-17T09:00:17.488Z</atom:updated>
            <content:encoded><![CDATA[<h3>@OneToOne relationship with spring data JPA, how to avoid N+1 problem when fetching a list of items?</h3><h3>The problem:</h3><p>Imagine You have the following entities:</p><p>The Person entity:</p><pre>package com.chmits.demo.entities;<br><br>import lombok.*;<br><br>import javax.persistence.*;<br><br>import java.util.UUID;<br><br>import static javax.persistence.GenerationType.<em>AUTO</em>;<br><br><em>/**<br> * </em><strong><em>@author </em></strong><em>Chamseddine Benhamed &lt;chamseddine.bhd at gmail.com&gt;<br> */<br><br></em>@Entity(name = &quot;person&quot;)<br>@Table(name = &quot;person&quot;)<br>@Getter<br>@Setter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>public class Person {<br>    @Id<br>    @GeneratedValue(strategy = <em>AUTO</em>)<br>    @Column(name = &quot;id&quot;)<br>    private UUID id;<br><br>    @Column(name = &quot;something&quot;)<br>    private String something;<br><br>    @OneToOne(cascade = CascadeType.<em>ALL</em>)<br>    @JoinColumn(name = &quot;passport&quot;)<br>    private Passport passport;<br>}</pre><p>The Passport entity:</p><pre>package com.chmits.demo.entities;<br><br>import lombok.AllArgsConstructor;<br>import lombok.Getter;<br>import lombok.NoArgsConstructor;<br>import lombok.Setter;<br><br>import javax.persistence.*;<br><br>import java.util.UUID;<br><br>import static javax.persistence.GenerationType.<em>AUTO</em>;<br><br><em>/**<br> * </em><strong><em>@author </em></strong><em>Chamseddine Benhamed &lt;chamseddine.bhd at gmail.com&gt;<br> */<br><br></em>@Entity(name = &quot;passport&quot;)<br>@Table(name = &quot;passport&quot;)<br>@Getter<br>@Setter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>public class Passport {<br>    @Id<br>    @GeneratedValue(strategy = <em>AUTO</em>)<br>    @Column(name = &quot;id&quot;)<br>    private UUID id;<br><br>    @Column(name = &quot;something&quot;)<br>    private String something;<br>}And a JpaRepository:</pre><pre>package com.chmits.demo.repositories;<br><br>import com.chmits.demo.entities.Student;<br>import org.springframework.data.jpa.repository.JpaRepository;<br><br>import java.util.List;<br><br>public interface StudentRepository extends JpaRepository&lt;Student, Long&gt;{<br>}</pre><p>So there’s one to one relation between Person and Passport</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*W9jeuimTCjs4bJ0os0JrRA.jpeg" /><figcaption>One to One relation</figcaption></figure><p>The Person JpaRepository :</p><pre>package com.chmits.demo.repositories;<br><br>import com.chmits.demo.entities.Person;<br>import org.springframework.data.jpa.repository.JpaRepository;<br><br><em>/**<br> * </em><strong><em>@author </em></strong><em>Chamseddine Benhamed &lt;chamseddine.bhd at gmail.com&gt;<br> */<br><br></em>public interface PersonRepository extends JpaRepository&lt;Person, Long&gt;{<br><br>}</pre><p>The test:</p><pre>@Test<br>public void OneToOneFindAll() {<br>    Person person1 = new Person();<br>    person1.setSomething(&quot;person1&quot;);<br><br>    Person person2 = new Person();<br>    person2.setSomething(&quot;person2&quot;);<br><br>    Person person3 = new Person();<br>    person3.setSomething(&quot;person3&quot;);<br><br>    Passport passport1 = new Passport();<br>    passport1.setSomething(&quot;passport1&quot;);<br><br>    Passport passport2 = new Passport();<br>    passport2.setSomething(&quot;passport2&quot;);<br><br>    person1.setPassport(passport1);<br>    person2.setPassport(passport2);<br><br>    System.<em>out</em>.println(&quot;SAVE PERSON 1&quot;);<br>    personRepository.save(person1);<br><br>    System.<em>out</em>.println(&quot;SAVE PERSON 2&quot;);<br>    personRepository.save(person2);<br><br>    System.<em>out</em>.println(&quot;SAVE PERSON 3&quot;);<br>    personRepository.save(person3);<br><br>    System.<em>out</em>.println(&quot;FIND ALL PERSONS&quot;);<br>    List&lt;Person&gt; personList = personRepository.findAll();<br>}</pre><p>Logs after test execution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-3VQMSTOKoCNykOPjIFVGw.png" /></figure><p>The PersonRepository findAll() method executed 3 queries, one to get all persons (3 in our test) and two queries to get the two passports referenced by person1 and person2 !</p><p>Imagine that we have a REST endpoint [GET /v1/persons] in our app to get all persons in a list. That endpoint uses PersonRepository’s findAll() method to return the result.</p><p>findAll() in that case will execute N + 1 queries (N is the length of the persons list), one query to get all persons and one query per person to get its passport, so to get a list with 10 000 entities, we should execute 10 001 queries ! That’s huge right ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*Rz1jOgo8c63k4Dh2gMupIA.png" /></figure><h3>The solution:</h3><p>To make findAll() method executes only one query instead of N + 1 :</p><p>Update the Passport definition :</p><pre>@Entity(name = &quot;person&quot;)<br>@Table(name = &quot;person&quot;)<br>@Getter<br>@Setter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>@NamedEntityGraph(name = &quot;Person.withPassport&quot;, attributeNodes = @NamedAttributeNode(&quot;passport&quot;))<br>public class Person {<br>...<br>}</pre><p>Then override the findAll() method in PersonRepository or create a new JpaRepository for the Person entity (so you can use two versions of findAll()):</p><pre>package com.chmits.demo.repositories;<br><br>import com.chmits.demo.entities.Person;<br>import org.springframework.data.jpa.repository.EntityGraph;<br>import org.springframework.data.jpa.repository.JpaRepository;<br><br>import java.util.List;<br><br><em>/**<br> * </em><strong><em>@author </em></strong><em>Chamseddine Benhamed &lt;chamseddine.bhd at gmail.com&gt;<br> */<br><br></em>public interface PersonRepository extends JpaRepository&lt;Person, Long&gt;{<br><br>    @Override<br>    @EntityGraph(&quot;Person.withPassport&quot;)<br>    List&lt;Person&gt; findAll();<br>}</pre><p>Now the findAll() method will execute one query instead of N +1 queries as show the new test log :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*uSUhmuWL6t3RuoPpRttd8g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W1QR0cFA2eqTE8iBQNlzlA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73239dcd4cfa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker compose : Wait until a container gets“ready”]]></title>
            <link>https://chamseddine-benhamed.medium.com/docker-compose-wait-until-a-container-gets-ready-1499c442dfdc?source=rss-8bd1d90bf9e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1499c442dfdc</guid>
            <category><![CDATA[logstash]]></category>
            <category><![CDATA[elk]]></category>
            <category><![CDATA[logspout]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[socat]]></category>
            <dc:creator><![CDATA[Chamseddine Benhamed]]></dc:creator>
            <pubDate>Mon, 14 Dec 2020 14:57:13 GMT</pubDate>
            <atom:updated>2020-12-14T16:02:16.043Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to wait for a container to get ready?</h3><p>You can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on, links…”. <strong>However</strong>, for startup Compose <strong>does not wait</strong> until a container is “ready” (whatever that means for your particular application) — only until it’s running !</p><p>In this article I’ll propose a solution to resolve that type of problems: Suppose that we have a docker compose with a lot of docker images (hello_world1 and hello_world2 in our study case) each of them generate some logs that should be collected and centralized, for this purpose we use the famous elk stack to centralize logs</p><ul><li>elasticsearch 6.8.6</li><li>logstash 6.8.6</li><li>kibana 6.8.6</li></ul><p>docker-compose.yml :</p><pre>version: &#39;3&#39;<br><br>services:<br>  elasticsearch:<br>    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6<br>    ports:<br>      - 9200:9200<br>      - 9300:9300<br>    environment:<br>      - discovery.type=single-node<br><br>  kibana:<br>    image: docker.elastic.co/kibana/kibana:6.8.6<br>    ports:<br>      - 5601:5601<br>    depends_on:<br>      - elasticsearch<br><br>  logstash:<br>    image: docker.elastic.co/logstash/logstash:6.8.6<br>    volumes:<br>      - pipelines.yml:/config/pipelines.yml:Z<br>      - logstash.conf:/usr/share/logstash/pipeline/logstash.conf:Z<br>    depends_on:<br>      - elasticsearch<br><br><br>  hello-world1:<br>    image: hello-world<br><br>  hello-world2:<br>    image: hello-world</pre><p>pipeline.yml :</p><pre><em># This file is where you define your pipelines. You can define multiple.<br># For more information on multiple pipelines, see the documentation:<br>#   https://www.elastic.co/guide/en/logstash/current/multiple-pipelines.html<br><br></em>- pipeline.id: main<br>  path.config: &quot;/usr/share/logstash/pipeline/logstash.conf&quot;</pre><p>logstash.conf :</p><pre>input {<br>    udp { <em>port </em>=&gt; 5000 }<br>}<br><br>filter{<br>    json{<br>        <em>source </em>=&gt; &quot;message&quot;<br>    }<br>}<br><br>output {<br>    elasticsearch {<br>        <em>hosts </em>=&gt; [&quot;elasticsearch:9200&quot;]<br>    }<br>}</pre><p>Now our elk stack is set up, we should add logspout (https://github.com/gliderlabs/logspout) to collect all logs generated from our services (the hello_world services) and push them to logstash using udp protocol.</p><p>In a nutshell, <strong>logspout</strong> is a log router for Docker containers that runs inside Docker. It attaches to all containers on a host, then routes their logs wherever you want. It also has an extensible module system.</p><p>Note: Adding LOGSPOUT=ignore as a service environment variable means that we’ll ignore the service’s logs so they will not be collected.</p><pre>version: &#39;3&#39;<br><br>services:<br>  elasticsearch:<br>    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6<br>    ports:<br>      - 9200:9200<br>      - 9300:9300<br>    environment:<br>      - discovery.type=single-node<br>      - LOGSPOUT=ignore<br><br>  kibana:<br>    image: docker.elastic.co/kibana/kibana:6.8.6<br>    ports:<br>      - 5601:5601<br>    environment:<br>      - LOGSPOUT=ignore<br>    depends_on:<br>      - elasticsearch<br><br>  logstash:<br>    image: docker.elastic.co/logstash/logstash:6.8.6<br>    volumes:<br>      - pipelines.yml:/config/pipelines.yml:Z<br>      - logstash.conf:/usr/share/logstash/pipeline/logstash.conf:Z<br>    environment:<br>      - LOGSPOUT=ignore<br>    depends_on:<br>      - elasticsearch<br><br>  logspout:<br>    image: gliderlabs/logspout:v3.2.12<br>    command: &#39;udp://logstash:5000?filter.name=hello*&#39;<br>    environment:<br>      - LOGSPOUT=ignore<br>      - &quot;RAW_FORMAT={ \&quot;container\&quot;: {{ toJSON .Container.Name }},                    \&quot;message\&quot;: {{ toJSON .Data }} }\n&quot;<br>    volumes:<br>      - /var/run/docker.sock:/tmp/docker.sock:ro<br>    depends_on:<br>      - logstash<br><br>  hello-world1:<br>    image: hello-world<br>    depends_on:<br>      - logspout<br>      <br>  hello-world2:<br>    image: hello-world<br>    depends_on:<br>      - logspout</pre><p>Now if we start our compose file using docker-compose up and we open kibana dashboard (localhost: 5601) then we can figure out that some logs are lost!</p><p>Why that? How can I get the starting logs!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JfCKD6_x8EXG2qhoL0U3JQ.jpeg" /></figure><p>The reason behind that is when we launch docker-compose, logstash take a moment to start and logspout starts to collect log data and can’t send it to logstash which is not ready yet! Docker-compose up launches the containers in order, but doesn’t guarantee the readiness of containers in the same order.</p><p>There’s some solutions in the internet that propose adding a command to test the readiness of a container before launching the executable (you can use wait-for-it. sh or wait-for to wrap your service’s command), Those solutions work, but they are not the best solution, especially in case you have a lot of services since you should add the script to all services and that’s a boilerplate code!</p><p>Proposed solution :</p><p>The solution that i propose is to use <strong>socat</strong> (<a href="https://www.redhat.com/sysadmin/getting-started-socat">https://www.redhat.com/sysadmin/getting-started-socat</a>), the <a href="https://linux.die.net/man/1/socat">socat</a> utility is a relay for bidirectional data transfers between two independent data channels.</p><pre>version: &#39;3&#39;<br><br>services:<br>  elasticsearch:<br>    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6<br>    ports:<br>      - 9200:9200<br>      - 9300:9300<br>    environment:<br>      - discovery.type=single-node<br>      - LOGSPOUT=ignore<br><br>  kibana:<br>    image: docker.elastic.co/kibana/kibana:6.8.6<br>    ports:<br>      - 5601:5601<br>    environment:<br>      - LOGSPOUT=ignore<br>    depends_on:<br>      - elasticsearch<br><br>  logstash:<br>    image: docker.elastic.co/logstash/logstash:6.8.6<br>    volumes:<br>      - pipelines.yml:/config/pipelines.yml:Z<br>      - logstash.conf:/usr/share/logstash/pipeline/logstash.conf:Z<br>    environment:<br>      - LOGSPOUT=ignore<br>    depends_on:<br>      - elasticsearch<br><br>  socat:<br>    image: alpine/socat<br>    <strong>command: &#39;TCP-LISTEN:5000,reuseaddr,fork TCP:logstash:5000,forever,interval=5&#39;<br></strong><br>  logspout:<br>    image: gliderlabs/logspout:v3.2.13<br>    <strong>command: &#39;tcp://socat:5000?filter.name=hello*&#39;</strong><br>    environment:<br>      - LOGSPOUT=ignore<br>      - BACKLOG=false<br>      - &quot;RAW_FORMAT={ \&quot;container\&quot;: {{ toJSON .Container.Name }}, \&quot;message\&quot;: {{ toJSON .Data }}, \&quot;@timestamp\&quot;: {{ toJSON .Time }} }\n&quot;<br>    volumes:<br>      - /var/run/docker.sock:/tmp/docker.sock:ro<br>    depends_on:<br>      - socat<br><br><br>  hello-world1:<br>    image: hello-world<br>    depends_on:<br>      - logspout<br><br>  hello-world2:<br>    image: hello-world<br>    depends_on:<br>      - logspout</pre><p>logstash.conf</p><pre>input {<br>    tcp { <em>port </em>=&gt; 5000 }<br>}<br><br>filter{<br>    json{<br>        <em>source </em>=&gt; &quot;message&quot;<br>    }<br>}<br><br>output {<br>    elasticsearch {<br>        <em>hosts </em>=&gt; [&quot;elasticsearch:9200&quot;]<br>    }<br>}</pre><p>so now logspout sends collected logs to socat which routes them to logstash once it gets ready.</p><p>command explanation :</p><pre>command: &#39;TCP-LISTEN:5000,reuseaddr,fork TCP:logstash:5000,forever,interval=5&#39;</pre><p>That command says : i listen at tcp port 5000 from any source, then i verify if logstash:5000 is ready (i check that every 5s), if yes i send buffered data (logs) to the destination, if not i wait until the destionation gets ready :)</p><p>Now when we start docker-compose socat will buffer the logs emitted from logspout and then resend them to logstash when its gets ready :D</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/195/1*1Ks0S6Lo3KLu5273iMyodA.jpeg" /></figure><p>Thanks for reading my article, if you have a question do not hesitate to contact me at <a href="https://www.linkedin.com/in/chamseddinebenhamed/">https://www.linkedin.com/in/chamseddinebenhamed/</a></p><p>#Docker-compose #ELK #LogSpout #Socat</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1499c442dfdc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>