Developing on NodeJS as a hobby is a real pleasure, but when it comes to production for many users, there are a couple of things you should know to avoid long responses and crashes.
As part of the work at MyHeritage, we developed the doppelgänger service for Eurovision 2019, with which you can find out which contestants you are most like by uploading selfies.
In addition to the face recognition logic, the application had an extremely clear requirement: it had to serve tens of thousands of simultaneous users, because Eurovision is watched by millions of people around the world.
Very quickly, we realized that the load balancer before the application, configured using Auto Scaling , is not enough for fault tolerance. The following helped us a lot:
fs.readSync
) are tempting because the code looks cleaner, but they literally kill performance. Instead, use async
/ await
operations, because during the execution of asynchronous operation, the CPU will also be available for other tasks (see Event loop ).const res = fs.readSync('file.txt');
const res = fs.readSync('file.txt');
After: const res = await fs.readAsync('file.txt');
Node
is configured by default to a limit of 1 GB. If the server is available, say, 4 GB specifically for your application, you will have to set the maximum memory limit manually using the CLI with the following flag: - --max-old-space-size
--max-old-space-size
Example: node --max-old-space-size=4096 server.js
Node
runs in the same thread. If you did not specifically set up a configuration that would run several threads, save money by choosing a server with 1 core.In our case, these tips led to a ten-fold improvement in performance and helped keep the production environment clean, even when thousands of users had to be served at the same time.
Thank you for reading.
Source: https://habr.com/ru/post/456036/
All Articles