I'm on the newer side of web development. I use React so obviously I focus on client side rendering, but for a certain application I was thinking to make a request to my server for every page (this probably isn't necessary but just a workaround due to ignorance on my part); however, the thought came to me, how many fetch requests are too much?
I want to divide this a little bit, I know different fetch requests can take different amounts of time, a GET request for 1 item of data is faster than a POST request that adds 20 rows and you can't account for all the variations.
But in general,
How long does a fetch request to a server (performing some sort of CRUD operation on the database) take?
** 2. How long does a fetch request to a server (NOT performing any operation to a database) take?**
Option 2 is obviously faster (if we're just imagining simple requests) and I know this can probably vary from server to server, but I think it would be helpful to know, so I can structure my site more efficiently and have some sort of knowledge about this topic?
There is no general way to know how long a request will take, it depends on so many factors like internet speed (assuming it is not a local server), the amount of data being submitted (in a POST) or retrieved, the amount of processing done on the server before returning the response etc.
If you have the answers to all of the above questions you can just calculate the time as a simple calculation.
Related
I'm trying to learn Node.js and adequate design approaches.
I've implemented a little API server (using express) that fetches a set of data from several remote sites, according to client requests that use the API.
This process can take some time (several fecth / await), so I want the user to know how is his request doing. I've read about socket.io / websockets but maybe that's somewhat an overkill solution for this case.
So what I did is:
For each client request, a requestID is generated and returned to the client.
With that ID, the client can query the API (via another endpoint) to know his request status at any time.
Using setTimeout() on the client page and some DOM manipulation, I can update and display the current request status every X, like a polling approach.
Although the solution works fine, even with several clients connecting concurrently, maybe there's a better solution?. Are there any caveats I'm not considering?
TL;DR The approach you're using is just fine, although it may not scale very well. Websockets are a different approach to solve the same problem, but again, may not scale very well.
You've identified what are basically the only two options for real-time (or close to it) updates on a web site:
polling the server - the client requests information periodically
using Websockets - the server can push updates to the client when something happens
There are a couple of things to consider.
How important are "real time" updates? If the user can wait several seconds (or longer), then go with polling.
What sort of load can the server handle? If load is a concern, then Websockets might be the way to go.
That last question is really the crux of the issue. If you're expecting a few or a few dozen clients to use this functionality, then either solution will work just fine.
If you're expecting thousands or more to be connecting, then polling starts to become a concern, because now we're talking about many repeated requests to the server. Of course, if the interval is longer, the load will be lower.
It is my understanding that the overhead for Websockets is lower, but still can be a concern when you're talking about large numbers of clients. Again, a lot of clients means the server is managing a lot of open connections.
The way large services handle this is to design their applications in such a way that they can be distributed over many identical servers and which server you connect to is managed by a load balancer. This is true for either polling or Websockets.
I'm ingesting a codebase that is a React-NodeJS stack. One of the concepts that I am trying to grasp is regarding the back-end API and how its handled client side.
The codebase is essentially dumping an entire collection from MongoDB with an API call, and then doing a good amount of parsing and client side logic with React in order to render custom views. The HTTP responses here are pretty large, and will only get larger as data is added to the DB.
Is there any advantage/disadvantage to this approach, as opposed to creating multiple endpoints in NodeJS, and utilizing something like Mongoose to return filtered data to the client, making rendering easy and responses smaller.
Things to take into consideration could be resource consumption, how this would be billed if in the cloud, the impact of SPA's, etc.
Hopefully i get some more clarity at the end of this?
Client-side processing is best because as you know our server-side resources are free and can easily handle requests.but Sending large amounts of data to the client for processing will incur client overhead and make their browsing experience less acceptable, data security may be compromised, or the network may be overwhelmed and bandwidth consumed. Processing data server side will increase your server load per client.
So to avoid these issues, it is best to first hand over some of these conflicts to the side of the database handled (with filtering and special conditions) and then filter the security data processed in server side with command and coding that Do not send clients.
But let's do the heavy processing on the client machine. SPA has other benefits, of course.
So I mostly do server-side processing - unless it's really basic stuff like simple sorting etc etc.
Also, don't assume JavaScript is enabled. You have to fall back gracefully and that would require the server to do the processing anyhow.
this link say to you differences between server-side and client-side programming
I have to collect real timer ticker data on trading pairs (usd/eur etc) from APIs of different sites. The data is usually a small JSON object with mostly ~10 numbers. The naive strategy is to make a request every 5 or so seconds to get the up-to-date ticker data of each of those sites. Some of them, though, provide a websocket option, which allows them to notify me directly when a change occurs, and, I believe, is more efficient. The issue is some of those sites don't offer that option, so the overall code will be simpler to organize and read if I use the same method for all sites (i.e., http requests). I'm also not sure the data is heavyweight enough to justify that choice.
From the experts who dealt with similar situations, is this case one where a relevant performance improvement can be expected from using sockets instead of timed http requests when it is available?
It depends:
WebSockets make only sense if you keep them open most of the time. If you instead open a new WebSocket connection each time you want to have new data the overhead is larger compared to a simple HTTP request. It is not so much the bandwidth (but this too) but you need more round trips to get your data which makes everything slower.
WebSockets take more resources at your end because you have to keep a TCP connection open for each open WebSocket connection. If there are only a small number of sites you need to ask it does not matter, if there are a lot it will matter. While it can be an advantage (less latency) to keep normal HTTP connections alive too you can close them if you have less resources.
If most of the time the data you get is the same then WebSockets might be more efficient because you only get send the new data when it actually changes.
If you want to be informed of new data as soon as possible then WebSockets perform better. If you only need a 5 second precision anyway it does not matter much.
We're looking at speccing out a system which broadcasts small amounts of frequently changing data (using JSON or XML or something) to multiple recipients at a reasonably high frequency (our updates will be 1000s per second).
We were initially thinking of using HTTP POST to broadcast the data to each endpoint, maybe once every few seconds (the clients will vary as they're other people's webapps), but we're now wondering if there's a better way to hold up to the load/frequency we're hoping. I imagine we'd need to version/timestamp the messages in some way at the very least.
We're using RabbitMQ for preparing all the things ready for sending and to choose what needs to go where (from a Django app, if that matters), but we can't get all of the endpoints to use a MQ.
The HTTP POST thing just doesn't seem quite right. What else should we be looking in to? Is this where things like node or socket.io or some of the new real time frameworks fit in? We're happy to find the right expertise to help with this, just need steering the correct direction.
Thanks!
You don't want to do thousands of POSTs per second to multiple clients. You're going to introduce the HTTP overhead on your end pushing it out, and for all you know, you might end up flooding the server on the other end with POSTs that just swamp it.
Option 1: For clients that can't or won't read a queue, POSTS could work, but to avoid killing the server and all the HTTP overhead, could you bundle updates? Once every minute or two, take all the aggregate data and then post it to the client? This way, you don't have 60+ POST requests going to one client every minute or two for time and eternity. It'll help save on bandwidth as well, since you only send all the header info once with more data instead of sending all the header information and pieces of data.
Option 2: Have you thought about using a good 'ole socket connection? Either you open a socket to the client, or vice versa, and push the data over that? That avoids the overhead of HTTP and lets the client read at the rate data arrives. If the client no longer wants to receive data, they can just close the connection. It's on the arcane side, but it'd avoid completely killing the target server.
If you can get clients to read a MQ, set up a group just for them and make your life easier so you only have to deal with those that can't or won't read the queue instead of trying for a one size fits all solution.
As a hypothetical example, let's say that I wanted to make an application that displays peoples twitter networks. I would provide an API that would allow a client to query on a single username. That user's top x tweets would be sent to the client. Then, each person that had been mentioned by the initial person would be scanned. Their top x tweets would be sent to the client. This process would recursively continue, breadth-first, until a pre-defined depth was reached. The client would be receiving the data in real time, displaying statistics such as number of users scanned, number of known users remaining to scan, and a growing list of the tweet data. None of the processing is complicated (regex of small amounts of text), but many, many network requests would be spawned from a single initial request.
I really want the fantastic realtime capabilities of node.js with socket.io, but I feel like this is an abuse of those technologies - they're not meant for heavy server-side lifting. Is there a more appropriate toolset for what I am trying to accomplish, or a particular way to use these tools to that end? Milewise is doing something similar-ish, but I think that my application would consume significantly more network resources than theirs.
Thanks.
The best network transport which you can get on the web now are WebSockets which offers persistent bi-directional real-time connection between server and client. Although not every browser supports them, socket.io gives you a couple of fallback solutions which may however decrease the network performance when compared to WebSockets as stated in this article:
During making connection with WebSocket, client and server exchange
data per frame which is 2 bytes each, compared to 8 kilo bytes of http
header when you do continuous polling.
...
Reducing kilobytes of data
to 2 bytes…and reducing latency from 150ms to 50ms is far more than
marginal. In fact, these two factors alone are enough to make
WebSocket seriously interesting to Google.
Apart from network transport, other things may also be important, for example how are you fetching, formating and processing the data on the server side. In node.js heavy CPU bound computations may block processing of other asynchronous operations, therefore these kind of operations should be dispatched to separate threads or processes in order to prevent blocking.