Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KunalBurangi opened this issue May 23, 2024 · 1 comment
Labels

Comments

@KunalBurangi
Copy link

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
Copy link
Member

nflaig commented Jun 14, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants