<?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 Curtis Porter on Medium]]></title>
        <description><![CDATA[Stories by Curtis Porter on Medium]]></description>
        <link>https://medium.com/@curtis.porter?source=rss-582f13b07205------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0dqATLJLV4nyx_lb</url>
            <title>Stories by Curtis Porter on Medium</title>
            <link>https://medium.com/@curtis.porter?source=rss-582f13b07205------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 18:55:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@curtis.porter/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[Jest Memory in Kubernetes]]></title>
            <link>https://medium.com/@curtis.porter/jest-memory-in-kubernetes-833a25fc4838?source=rss-582f13b07205------2</link>
            <guid isPermaLink="false">https://medium.com/p/833a25fc4838</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[memory-leak]]></category>
            <dc:creator><![CDATA[Curtis Porter]]></dc:creator>
            <pubDate>Wed, 03 Jan 2024 20:01:20 GMT</pubDate>
            <atom:updated>2024-01-03T20:01:20.886Z</atom:updated>
            <content:encoded><![CDATA[<p>Some projects run jest tests as part of a deployment via a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/">kubernetes job</a>. This is typically done to ensure that integration or end-to-end tests succeed before the deployment is completed, so that the deployment rolls back if they fail.</p><p>In this scenario, sometimes the tests run out of memory. This can be confusing since the individual tests might not take anywhere near the amount of memory available to kubernetes jobs by default.</p><p><strong>If you add the flag to your jest command, </strong>--runInBand, <strong>jest will run only one test at a time. This usually resolves the problem.</strong></p><p>Why does that fix it? By default, jest runs tests in parallel through a pool of child processes. The way kubernetes (and docker) enforce memory limits is through a Linux kernel feature called “<a href="https://medium.com/@kkwriting/kubernetes-resource-limits-and-kernel-cgroups-337625bab87d">control groups</a>” (also referred to as “cgroups”) that sum all the memory of the main process <em>and its children</em>. When the sum of all the processes’ memory hits the limit, the processes are killed with an OOMKilled error. If jest doesn’t launch any child processes, then only the main process’ memory contributes toward the limit. Since --runInBand causes jest to run one test at a time, with no child processes, the memory never grows beyond what is needed for a single test.</p><h3>Running Locally with Memory Limits</h3><p>It is much faster and more convenient to test these scenarios if you run them locally. You can do that pretty conveniently with docker, since it uses the same cgroups functionality to control memory limits as kubernetes.</p><p>First, build your docker container. An example docker build command that you might run is:</p><p>docker build -t testimage:v1 .</p><p>Then run it with a --memory flag:</p><p>docker run --memory=500m testimage:v1</p><p>You can now adjust the memory and re-run without waiting for a full kubernetes deployment.</p><h3>Monitoring Jest Memory Usage</h3><p>It can be helpful to monitor the amount of memory used by each test. Jest provides a command-line flag for that, which is --logHeapUsage. When that flag is present, a log is written after each test completes, which shows the amount of heap memory that was used to run that test.</p><p>Note that just adding --logHeapUsage isn’t enough; that flag only works when you also supply --runInBand <em>and pass an additional argument to node</em>, which is the --expose-gc argument. Jest needs the node garbage collector exposed so it can access the heap information. The full command looks something like this:</p><p>node --expose-gc node_modules/.bin/jest --config=./jest.api.config.js --runInBand --logHeapUsage</p><p>Again, notice that --expose-gc is an argument to node, and --runInBand --logHeapUsage are arguments for jest.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=833a25fc4838" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Graceful Termination of KafkaJS Client Processes]]></title>
            <link>https://medium.com/@curtis.porter/graceful-termination-of-kafkajs-client-processes-b05dd185759d?source=rss-582f13b07205------2</link>
            <guid isPermaLink="false">https://medium.com/p/b05dd185759d</guid>
            <category><![CDATA[kafkajs]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Curtis Porter]]></dc:creator>
            <pubDate>Thu, 26 Aug 2021 18:38:23 GMT</pubDate>
            <atom:updated>2021-08-26T18:38:23.423Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently our team built a simple Kafka consumer using <a href="https://kafka.js.org/">KafkaJS</a>. When running locally things seemed fine, but when deployed to our kubernetes cluster, the consumer would not receive messages for several minutes after startup and started logging messages such as:</p><pre>The group is rebalancing, re-joining</pre><p>and then,</p><pre>[ConsumerGroup] Consumer has joined the group</pre><p>at which point the messages would come through.</p><p>The culprit was that our consumer code did not call disconnect()prior to the consumer process being shut down. When running a single consumer instance locally we didn’t notice a problem, but when running in a cluster individual pods would terminate as part of rolling restarts, and neighboring pods were affected by consumer group rebalancing.</p><p>A consumer group rebalance is an action performed by the Kafka broker. If a consumer process is terminated, the Kafka broker will redistribute the topic’s partitions among the healthy consumers. A rebalance cannot complete until each consumer accepts its new partition assignments from the broker.</p><p>If the consumer calls disconnect()before terminating, the broker is aware that the consumer is gone, and can perform the rebalance quickly and with minimal impact. If the consumer terminates suddenly, however, the broker will wait for a response from the consumer for the session timeout period. If no response is received before the session times out, the broker gives up on the consumer and completes the rebalance using the remaining consumers. Since messages are not passed to the consumer group during a rebalance, that means the entire consumer group is blocked while waiting through the offending consumer’s session timeout period.</p><p>The <a href="https://kafka.js.org/docs/introduction">KafkaJS documentation </a>contains a solution for this, but it is easy to overlook since it’s provided in the <a href="https://kafka.js.org/docs/consumer-example">example consumer</a> and <a href="https://kafka.js.org/docs/producer-example">producer code</a> but not discussed in the main docs. The remedy is to add a process signal and error handler that disconnects the client from the broker:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30712a30bf6b07485b8724c2f845528/href">https://medium.com/media/b30712a30bf6b07485b8724c2f845528/href</a></iframe><p>There are two items worth noting:</p><ol><li>This is needed for the producer, as well as the consumer. Any KafkaJS client that connects to the Kafka broker should disconnect gracefully.</li><li><em>Each</em> KafkaJS client should be included. There are some scenarios where you might have more than one client, such as when both a producer and a consumer are in the same application. In that case, you want a single error and signal handler that calls disconnect on <em>each</em> KafkaJS client. Registering multiple handlers will create a condition where one client will disconnect and then the process will exit before the second client’s handler is called.</li></ol><p>There are times when a pod might not be terminated instantly, such as sudden power failure of a node. In those cases the shutdown handler will not be called and behavior will fall back to waiting for the session timeout, but those situations should be rare.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b05dd185759d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>