Professional Documents
Culture Documents
Sheets of Chandsn Jeje
Sheets of Chandsn Jeje
js API
The tips I will share in this article will help you reduce response
time, lower latency, manage errors and throughput, and minimize
CPU and memory usage.
I/O operations include the processes which perform read and write data
operations. It can be the database, cloud storage, or any local storage disk on
which the I/O operations are performed.
Here's an example:
var fs = require('fs');
// Performing a blocking I/O
var file = fs.readFileSync('/etc/passwd');
console.log(file);
// Performing a non-blocking I/O
fs.readFile('/etc/passwd', function(err, file) {
if (err) return err;
console.log(file);
});
Now, stateless APIs are common and provide JWT, OAuth, and other
authentication mechanisms. These authentication tokens are kept on the client
side and protect the servers to manage the state.
JWT is a JSON-based security token for API Authentication. JWTs can be seen
but they're not modifiable once they're sent. JWT is just serialized, not
encrypted. OAuth is not an API or a service – rather, it's an open standard for
authorization. OAuth is a standard set of steps for obtaining a token.
Also, don’t waste your time in making your Node.js server serve static files.
Use NGINX and Apache instead, as they work far better than Node for this
purpose.
While building APIs in Node, don’t send the full HTML page in the response of
the API. Node servers work better when only data is sent by the API. Generally,
this kind of application works with JSON data.
Let's say we have a DB schema without indexing and the database contains 1
million records. A simple find query will go through a larger number of records
to find the matching one compared to the schema with indexing.
1039 1
Application downtime is almost zero using PM2. Overall, PM2 can really
improve the performance and concurrency of your API.
Deploy the code on production and run the following command to see how the
PM2 cluster has scaled on all available CPUs:
pm2 start app.js -i 0
It is unlikely that the page all users are accessing on the web browser loads
within 100ms. This is simply because of the physical distance between the
server and the users.
Here, we can reduce the Time to First Byte by using a CDN and caching content
in local data centers across the globe. This helps users access the content with
minimal latency. Cloudflare is one of the CDN solutions you can use to start
with.
The best way to monitor the proper functioning of your APIs is to keep track of
their activity. This is where logging the data comes into play.
logger.stream = {
write: function(message, encoding) {
logger.info(message);
}
};
In addition to these techniques, we can also apply some other techniques like
using HTTP/2 over HTTP, as it has the following advantages:
Multiplexing
Header compression
Server push
Binary format
It focuses on the performance and issues that the previous version of HTTP has.
It makes web browsing faster and easier and consumes less bandwidth.
Use async.js to help you run tasks. Parallelizing tasks has a great impact on the
performance of your API. It reduces latency and minimizes blocking operations.
Parallel means running multiple things at the same time. However, when you
run things in parallel, you don’t need to control the execution sequence of the
program.
Here's a simple example using async parallel with an array:
In the following code snippets, we have called the APIs without and with Redis,
respectively, and compared the response time.
900ms 0.621
'use strict';
app.get('/book', getBook);
app.listen(3000, function() {
console.log('Your node is running on port 3000 !!!')
});
And here's Node with Redis:
'use strict';
res.send(book);
})
.catch(err => {
res.send('The book you are looking for is not found !!!');
});
};
app.listen(3000, function() {
console.log('Your node is running on port 3000 !!!')
)};