loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.88k stars 1.06k forks source link

Loopback/rest stringifies the responce which takes time and hangs the app #10529

Open KunalBurangi opened 1 month ago

KunalBurangi commented 1 month ago

Describe the bug

I have a loopback 4 application where I have API which returns large data 500000 records to explain the problem I have added the jaeger trace image if you see 1) is my API which has responded in 7-8 sec but the complete API took 12-13 sec I want to know what happens in those remaining 5-6 sec?

I debugged the API and found out that the control goes to loopback/rest where it is stringifying the response that too using JSON.stringify() and not asyncrously image

what happens is that during this time all other requests goes pending in my case liveliness check goes pending and as per my configuration if liveliness failed the container restarts . how to fix this problem ?

Logs

No response

Additional information

Can we do a faster stringify? or maybe make this asynchronous? I am ready to contribute

Reproduction

.

nflaig commented 2 weeks ago

Can we do a faster stringify?

There are options like fast-json-stringify which relies on json schema to be defined for the payload. This would work well with Loopback as response schemas should exist for all apis.

I doubt this will help in your case though because it does not improve the performance a lot for large payloads. JSON is just a extremely inefficient serialization format, especially for large payloads.

or maybe make this asynchronous?

There is no cheap way to make JSON serialization async, could run the serialziation in a worker thread but I would assume passing the object and then the serialized json through worker boundaries is quite expensive as well.

I haven't used this library myself, but streaming the serialization via JSONStream might help, likely takes more overall time but you have run the chunks in different iterations of the event loop which will give your app time to respond to other requests.

what happens is that during this time all other requests goes pending in my case liveliness check goes pending and as per my configuration if liveliness failed the container restarts . how to fix this problem ?

Could just run the server hosting the liveness api in a different thread. That way it would still be able to respond even if main thread is busy.