Our Journey to a High-Performance Node.js Library
Tags: hazelcast, node, performance
Published: 2020-11-19 06:47:15

---
title: Our Journey to a High-Performance Node.js Library
published: true
description: Hazelcast Node.js library went through a number of performance analysis and optimization runs and we would like to share this journey.
tags: #hazelcast #nodejs #performance
canonical_url: https://hazelcast.com/blog/our-journey-to-a-high-performance-node-js-library/
//cover_image: https://direct_url_to_image.jpg
---

<p>As you may already know, the Hazelcast <a href="https://hazelcast.org/imdg/">In-Memory Data Grid</a> (IMDG) ecosystem includes a <a href="https://hazelcast.org/imdg/clients-languages/">variety of clients</a> for different languages and runtimes, which includes Node.js <a href="https://github.com/hazelcast/hazelcast-nodejs-client">client library</a> as a part of that list.</p>
<p>You can use Hazelcast clients in various cases, including, but not limited to the following:</p>
<ul>
<li>Building a multi-layer cache for your applications with <a href="https://github.com/hazelcast/hazelcast-nodejs-client/blob/9e53459a15c479eed7ef4df2752c833a9c311d53/DOCUMENTATION.md#741-using-map">IMap</a>, a distributed, replicated key-value store, and its <a href="https://github.com/hazelcast/hazelcast-nodejs-client/blob/9e53459a15c479eed7ef4df2752c833a9c311d53/DOCUMENTATION.md#782-near-cache">NearCache</a>.</li>
<li>Enabling pub-sub communication between application instances.</li>
<li>Dealing with high load for views or likes events by using a <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">conflict-free replicated</a> counter.</li>
<li>Preventing races when accessing 3rd-party services by using <a href="https://hazelcast.org/blog/long-live-distributed-locks/">FencedLock</a> and other distributed concurrency primitives available in Hazelcast CP Subsystem (powered by <a href="https://raft.github.io/">Raft</a> consensus algorithm).</li>
</ul>
<p>High performance and low latency for data access have always been a key feature of Hazelcast. So, it’s not surprising that we put a lot of time and effort into optimizing both server-side and client libraries.</p>
<p>Our Node.js library went through numerous performance analysis and optimization runs over the course of several releases, and we think it’s worth telling you the story and sharing the gathered experience. If you develop a library or an application for Node.js and performance is something you care about, you may find this blog post valuable.</p>
<p>TL;DR</p>
<ul>
<li>Performance analysis is not a one-time action but rather a (sometimes tiring) process.</li>
<li>Node.js core and the ecosystem includes useful tools, like the built-in profiler, to help you with the analysis.</li>
<li>Be prepared for the fact that you will have to throw many (if not most) of your experiments into the trash as part of the optimization process.</li>
<li>While “high-performance library” title may sound too loud, we do our best to deserve it for Node.js and all the other Hazelcast client libraries.</li>
</ul>
<p>We’re going to start this story in spring 2019, in the times of 0.10.0 version of the Node.js client. Back then, the library was more or less feature complete, but there was little understanding of its performance. Obviously, it was necessary to analyze the performance before the first non-0.x release of the client and that’s where this story starts.</p>

<h2>Benchmarks</h2>

<p>It’s not a big secret that benchmarking is tricky. Even VMs themselves may introduce noticeable variation in results and even <a href="https://tratt.net/laurie/blog/entries/why_arent_more_users_more_happy_with_our_vms_part_1.html">fail</a> to reach a steady performance state. Add Node.js, library, and benchmark code on top of that and the goal of reliable benchmarking will get even harder. Any performance analysis has to rely on inputs provided by some kind of benchmark. Luckily, version 0.10.0 of the library included a simple <a href="https://github.com/hazelcast/hazelcast-nodejs-client/blob/v0.10.0/benchmark/SimpleMapBenchmark.js">benchmark</a> used in early development phases. That benchmark had some limitations which needed to be resolved before going any further.</p>
<p>The existing benchmark supported only a single scenario with randomly chosen operations. There is nothing wrong with having a random-based scenario in the benchmark suite, but only when more narrow scenarios are present in the suite. In the case of a client library, that would be “read-heavy” and “write-heavy” scenarios. The first assumes sending lots of read operations, thus moving the hot path to the I/O read-from-socket code and further data deserialization. You may have already guessed that the second scenario involves lots of writes and moves write-to-socket and serialization code to the hot path. So, we added these additional scenarios.</p>
<p>Another noticeable addition to scenarios was support for the payload size option. Variation in payload size is important when running benchmarks, as it helps with finding potential bottlenecks in the serialization code. Using different payload types is also valuable, but for a start, we decided to deal with strings only. String type is used for storing JSON data on the Hazelcast cluster, so our choice had a nice side-effect of testing a significant part of the hot path for JSON payload type (i.e., for plain JavaScript objects).</p>
<p>The second problem was self-throttling of the benchmark. Simply put, the benchmark itself was acting as a bottleneck hiding real bottleneck issues present in the client library. Each next operation run by the benchmark was <a href="https://github.com/hazelcast/hazelcast-nodejs-client/blob/v0.10.0/benchmark/SimpleMapBenchmark.js#L34">scheduled</a> with the setImmediate() function without any concurrency limit for the sent operations. Apart from becoming a bottleneck, this approach also created a significant level of noise (sometimes it’s called “jitter”) in the benchmark results. Even worse, such logic puts the benchmark very far from real-world Node.js applications.</p>
<p>That’s why we improved the benchmark by enforcing the given concurrency limit. The end behavior of our <a href="https://github.com/hazelcast/hazelcast-nodejs-client/blob/353edf80b5bf86676f5b9d025b35c630d8cfbd03/benchmark/BenchmarkRunner.js#L20">benchmark runner</a> is close to the popular <a href="https://www.npmjs.com/package/p-limit">p-limit</a> package and can be visualized as the following diagram:</p>

![New benchmark logic](https://dev-to-uploads.s3.amazonaws.com/i/2i4m8hh5ojha33fz0sy8.png)

<p>The diagram shows how operations are executed when the concurrency limit is set to 3 and the total count of operations to be run is 7. As a result, the load put on both the client and the server-side instances is evenly distributed, which helps to minimize the jitter.</p>
<p>Finally, we added a warm-up phase into the benchmark to give both client and server VMs some time to reach a steady state.</p>
<p>Now, with our new shiny benchmark, we were ready to start the actual analysis.</p>
<h2>Here Come the Bottlenecks</h2>
<p>The very first benchmark run showed the following results in scenarios based on IMap’s get() (“read-heavy”) and set() (“write-heavy”) operations.</p>
<table>
<tbody>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6;">Scenario</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 100KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 100KB</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;">Throughput (ops/sec)</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">90,933</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">23,591</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">105</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">76,011</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">44,324</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">1,558</td>
</tr>
</tbody>
</table>

<p>Each result here stands for an average throughput calculated over a number of benchmark runs. Result variation, median and outliers are omitted for the sake of brevity, but they were also considered when comparing results.</p>
<p>Data sizes (3B, 1KB, and 100KB) in the table stand for the value size. Of course, absolute numbers are not important here, as we didn’t yet have a baseline. Still, the results for the smallest value size look more or less solid and, if we would only run these benchmarks, we could stop the analysis, give the library a green light for the first major release, and arrange the release party. But results for larger values are much more disturbing. They scale down almost linearly with the growth of the value size, which doesn’t look good. This gave us a clue that there was a bottleneck somewhere on the hot path, presumably in the serialization code. Further analysis was required.</p>
<p>Node.js is quite mature and there are a number of tools in the ecosystem to help you with finding bottlenecks. The first one is the V8’s sampling profiler <a href="https://nodejs.org/en/docs/guides/simple-profiling/">exposed</a> by Node.js core. It collects information about call stacks in your application with a constant time interval and stores it in an intermediate profile file. Then it allows you to prepare a text report based on the profile. The core logic is simple: the more samples contain a function on the top of the call stack, the more time was spent in the function when profiling. Thus, potential bottlenecks are usually found among the most “heavy” functions.</p>
<p>Profiler reports are helpful in many situations, but sometimes you may want to start the analysis with visual information. Fortunately, <a href="http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html">flame graphs</a> are there to help. There are a number of ways to collect flame graphs for Node.js applications, but we were more than fine with <a href="https://www.npmjs.com/package/0x">0x library</a>.</p>
<p>Here is a screenshot of the flame graph collected for the set() 3B scenario.</p>

![Flame graph for set() 3B scenario](https://dev-to-uploads.s3.amazonaws.com/i/jkgy97xjwfi6rys6o320.png)

<p>This screenshot is static, while 0x produces an interactive web page allowing you to zoom and filter through the contents of the flame graph. In this particular case, it took us some time to iterate over so-called “platos” in search of suspicious calls. Finally, we found a good candidate highlighted in the next picture.</p>

![Flame graph for set() 3B scenario (bottleneck highlighted)](https://dev-to-uploads.s3.amazonaws.com/i/pabx8ohnde7lcpbidrw6.png)

<p>It appeared that the library was doing a lot of unnecessary allocations for <a href="https://nodejs.org/api/buffer.html">Buffer</a> objects. Buffers are low-level objects based on V8’s ArrayBuffer class, which represents contiguous arrays of binary data. The actual data is stored off-heap (there are some exceptions to this rule, but they are not relevant for our case), so allocating a Buffer may be a relatively expensive operation.</p>
<p>As a simple fix, we tried to get rid of certain Buffer allocations happening in the library by doing those allocations in a <a href="https://github.com/hazelcast/hazelcast-nodejs-client/pull/473/files#diff-0f3afe789013f0c716b073253fe702593cd67adc4221cb0b1434cfd76b26cfbaR39">greedy manner</a>. With this change, the benchmark showed us the following.</p>
<table>
<tbody>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6;"></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 100KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 100KB</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;">v0.10.0</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">90,933</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">23,591</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">105</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">76,011</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">44,324</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">1,558</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;">Candidate</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">104,854</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">24,929</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">109</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">95,165</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">52,809</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">1,581</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;"></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+15%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+5%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+3%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+25%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+19%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+1%</strong></td>
</tr>
</tbody>
</table>

<p>The improvement was noticeable for smaller payloads, but the scalability issue was still there. While the fix was very simple, if not primitive, the very first bottleneck was found. The fix was good enough as the initial optimization and further improvements were put into the backlog for future versions of the library.</p>
<p>The next step was to analyze so-called “read-heavy” scenarios. After a series of profiler runs and a thoughtful analysis, we found a suspicious call. The call is highlighted on the following screenshot for get() 100KB flame graph.</p>

![Flame graph for get() 100KB scenario (bottleneck highlighted)](https://dev-to-uploads.s3.amazonaws.com/i/9v9xnxsmt3vih8mre4nu.png)

<p>The ObjectDataInput.readUtf() method appeared to be executed on a significant percentage of collected profiler samples, so we started looking into that. The method was responsible for string deserialization (i.e., creating a string from the binary data) and looked more or less like the following TypeScript code.</p>

```typescript
private readUTF(pos?: number): string {
  const len = this.readInt(pos);
  // ...
  for (let i = 0; i < len; i++) {
    let charCode: number;
    leadingByte = this.readByte(readingIndex) & MASK_1BYTE;
    readingIndex = this.addOrUndefined(readingIndex, 1);
    const b = leadingByte & 0xFF;
    switch (b >> 4) {
      // ...
    }
    result += String.fromCharCode(charCode);
  }
  return result;
}
```

<p>In general, the method was similar to what we had in the Hazelcast Java client. It was reading UTF-8 chars one by one and concatenating the result string. That looked like a suboptimal code, considering that Node.js provides the <a href="https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end">buf.toString()</a>&nbsp;method as a part of the standard library. To compare these two implementations, we wrote simple microbenchmarks for both string <a href="https://github.com/puzpuzpuz/microbenchmarks/blob/daac1f55412d0e2fd72af73dc0e68c7f86c0efed/src/utf-deserializers.js">deserialization</a> and <a href="https://github.com/puzpuzpuz/microbenchmarks/blob/daac1f55412d0e2fd72af73dc0e68c7f86c0efed/src/utf-serializers.js">serialization</a>. Here is a trimmed result for the serialization microbenchmark.</p>

![Serializers microbenchmark results](https://dev-to-uploads.s3.amazonaws.com/i/iiwuw7q1ze3cuil9st94.png)

<p>As it is clearly seen here, the standard API is significantly (around x6) faster than our custom implementation when it comes to ASCII strings (which are a frequent case in user applications). Results for deserialization and other scenarios look similar with the respect to the string size correlation. That was the exact reason for the scalability issue.</p>
<p>The standard library is significantly faster in the ASCII string case, as V8 is smart enough to detect the case and go over the fast path where it simply copies string contents instead of decoding/encoding individual chars. For those of you who are curious about the corresponding V8 source code, here is <a href="https://github.com/v8/v8/blob/lkgr/6.8/src/heap/factory.cc#L609">the place</a> responsible for the buf.toString()’s fast path.</p>
<p>Anyhow, before making the final verdict, it was necessary to confirm the hypothesis with a proper experiment. To do so, we implemented a fix and compared it with the baseline (v0.10.0).</p>

<table>
<tbody>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6;"></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">get() 100KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 3B</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 1KB</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; background-color: #e6e6e6; text-align: right;">set() 100KB</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;">v0.10.0</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">90,933</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">23,591</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">105</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">76,011</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">44,324</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">1,558</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;">Candidate</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">122,458</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">104,090</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">7,052</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">110,083</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">73,618</td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;">8,428</td>
</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px;"></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+34%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+341%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+6,616%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+45%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+66%</strong></td>
<td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: right;"><strong>+440%</strong></td>
</tr>
</tbody>
</table>

<p>Bingo! Lesson learned: always bet on the standard library. Even if it’s slower today, things may change dramatically in the future releases.</p>
<p>As a result of this short (~1.5 weeks) initial analysis, Hazelcast Node.js client v3.12 was <a href="https://github.com/hazelcast/hazelcast-nodejs-client/releases/tag/v3.12">released</a> with both of the discussed performance improvements.</p>
<p>Now, when there is an understanding of our usual process, let’s speed up the narration and briefly describe optimizations shipped in later versions of the library.</p>
<h2>Automated Pipelining</h2>
<p><a href="https://en.wikipedia.org/wiki/Protocol_pipelining">Protocol pipelining</a> is a well-known technique used to improve the performance of blocking APIs. On the user level, it usually implies an explicit batching API, which is only applicable to a number of use cases, like <a href="https://en.wikipedia.org/wiki/Extract,_transform,_load">ETL</a> pipelines.</p>
<p>Obviously, the same approach can be applied to Node.js with its non-blocking APIs. But we wanted to apply the technique in an implicit fashion so that most applications would benefit from the new optimization. We ended up with the feature called <a href="https://github.com/hazelcast/hazelcast-nodejs-client/pull/481">automated pipelining</a>. It can be illustrated with the following diagram.</p>

![Automated pipelining logic](https://dev-to-uploads.s3.amazonaws.com/i/rlypnphhranfs5ztwo9q.png)

<p>The main idea is to accumulate outbound messages based on the event loop lifecycle instead of writing them into a TCP socket immediately when the user starts an operation. The messages are scheduled to be concatenated into a single Buffer (with a configured size threshold) and only then are written into the socket. This way we benefit from batch writes without having to ask the user to deal with an explicit pipelining API.</p>
<p>Another important aspect here is that the client keeps one persistent connection per cluster member (note: we’re talking of smart client mode). Consequently, network communication over each connection is intensive enough to make the described batching logic valuable in terms of throughput.</p>
<p>Hazelcast Java client <a href="https://github.com/hazelcast/hazelcast/blob/97ff57ba92dc1fbfa77720885e7db40757999080/hazelcast/src/main/java/com/hazelcast/client/impl/protocol/util/ClientMessageEncoder.java">implements</a> something close to this optimization by concatenating messages before writing them into the socket. A similar approach is used in other Node.js libraries, like DataStax Node.js driver for Apache Cassandra.</p>
<p>Benchmark measurements for automated pipelining showed 24-35% throughput improvement in read and write scenarios. The only drawback was a certain degradation (~23%) in scenarios with large message writes (100KB), which is expected considering the nature of the optimization. As real-world applications read data more frequently than write it, it was decided to enable automated pipelining by default and allow users to disable it via the client configuration.</p>
<p>Later on, we have improved automated pipelining by optimizing the code, which was manipulating the write queue. The main improvement came from <a href="https://github.com/hazelcast/hazelcast-nodejs-client/pull/585">reusing</a> the outbound Buffer instead of allocating a new one on each write. Apart from this, we also were able to <a href="https://github.com/hazelcast/hazelcast-nodejs-client/pull/605">get rid</a> of the remaining unnecessary Buffer allocations that we had in the library. As a result, we got around 8-10% throughput improvement. This latest version of automated pipelining may be found in the 4.0 release of the client.</p>
<h2>Boomerang Backups</h2>
<p>As you may guess, it’s not all about Node.js specific optimizations. Periodically, all Hazelcast clients get common optimizations. Client backup acknowledgments (a.k.a. boomerang backups) are a recent example of this process.</p>
<p>Previously, the client was waiting for the sync backups to complete on the member. This was causing 4 network hops to complete a client operation with sync backup. Since sync backup configuration is our out-of-the-box experience, boomerang backups optimization was introduced. The following diagram illustrates the change in terms of client-to-cluster communication.</p>

![Client backup acknowledgments flow](https://dev-to-uploads.s3.amazonaws.com/i/axs5ewrji6000iy39q7q.png)

<p>As it may be seen above, boomerang backups decrease network hops to 3. With this change, we saw up to 30% throughput improvement in our tests. This optimization was shipped in client v4.0.</p>
<h2>Migration to Native Promises</h2>
<p>Everyone knows that callbacks lost the battle and most Node.js applications are written with promises. That’s why Hazelcast Node.js client had a Promise-based API from the day one. In older versions, it was using the <a href="https://www.npmjs.com/package/bluebird">bluebird</a> Promise library for performance reasons. But since then, V8’s native Promise implementation got <a href="https://v8.dev/blog/fast-async">much faster</a> and we decided to give native promises a try.</p>
<p>Benchmark measurements <a href="https://github.com/hazelcast/hazelcast-nodejs-client/issues/559#issuecomment-694012526">showed</a> no performance regression after the migration, so the switch was shipped in v4.0. As a nice side effect of this change, we got an out-of-the-box integration with <a href="https://nodejs.org/api/async_hooks.html">async_hooks</a>&nbsp;module.</p>
<h2>Other Optimizations</h2>
<p>Expectedly, there were a bunch of smaller optimizations done on the way. Say, to reduce the amount of litter generated on the hot path we switched from new Date() calls to Date.now(). Another example is the default serializer implementation for Buffer objects. It allows users to deal with Buffers instead of plain arrays of numbers. Not saying that the internal code responsible for manipulations with Buffers also improved a lot. It’s hard to notice an effect of individual optimization here, but they’re certainly worth it.</p>
<h2>A Self-Check</h2>
<p>Before the wrap-up, let’s try to look at what we achieved in about one year. To do so, we’re going to run a couple of benchmarks for versions 0.10.0 (our baseline) and 4.0 (the latest one).</p>
<p>For the sake of brevity we’re going to compare IMap.set() and get() operations for 1KB ASCII values. Hopefully, the payload is close enough to what one may see on average in Node.js applications. Here is how the result looks like.</p>

![v0.10.0 vs. v4.0 performance comparison](https://dev-to-uploads.s3.amazonaws.com/i/ngv7k70gx8w8w9z1hqxx.png)

<p>In the above chart, we see almost x3 throughput improvement in both operations. The value of all implemented optimizations should be obvious now.</p>
<h2>What’s Next?</h2>
<p>There are multiple things we want to give a try in both the library and the tooling. For instance, we’re <a href="https://github.com/hazelcast/hazelcast-nodejs-client/issues/652">experimenting</a> with the onread option available in the net.Socket class. This option allows one to reuse Buffer when reading from the socket. Unfortunately, tls module used by the client for encrypted communication lacks the counterpart option, so recently we <a href="https://github.com/nodejs/node/pull/35753">contributed</a> to the Node.js core to improve things.</p>
<p>Our benchmarking approach also needs some improvements. First of all, we want to start considering operation latency by collecting latency data into an <a href="http://hdrhistogram.org/">HDR histogram</a> throughout benchmark execution. Another nice addition would be integration with <a href="https://github.com/hazelcast/hazelcast-simulator">Hazelcast Simulator</a>, our distributed benchmarking framework. Finally, support for more data structures and payload types won’t hurt.</p>
<h2>Lessons Learned</h2>
<p>Yes, we know that the “high-performance library” title may be too loud, but we do our best to deserve it. For us, as open-source library maintainers, performance analysis is a process that requires constant attention. Necessary routing actions, like pre-release performance analysis, may be tiring. We had to throw many (if not most) of our experiments into the trash can. But in the end, performance is something we aim to deliver in all of our <a href="https://hazelcast.org/imdg/clients-languages/">client libraries</a>.</p>