#community-help

Network Errors and Duplicate Requests in Typesense

TLDR Ethan and Rushil were experiencing more than 300 network errors and duplicate requests from Typesense. After rigorous troubleshooting, Jason identified the issue as an Instantsearch-induced multiple re-rendering and implemented a fix. Multiple requests reduced, but networking errors persisted, suggested to be due to client-side internet connectivity.

Powered by Struct AI

4

1

1

68
2mo
Solved
Join the chat
Jul 28, 2023 (2 months ago)
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
02:49 AM
Been getting a lot of errors (>300) from the axios library in typesense after implementing - are these normal to search or did we mess up somewhere in our implementation
Image 1 for Been getting a lot of errors (>300) from the axios library in typesense after implementing - are these normal to search or did we mess up somewhere in our implementationImage 2 for Been getting a lot of errors (>300) from the axios library in typesense after implementing - are these normal to search or did we mess up somewhere in our implementation
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:59 AM
It says “Network error”… Are you able to share the exact request URL and query parameters to Typesense that throws this error?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
03:03 AM
dm’d
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:29 AM
Looks like your cluster is spiking to 100% CPU every now and then
03:29
Jason
03:29 AM
I would recommend upgrading to 4vCPUs for the volume of traffic you have
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
08:23 PM
Are there lower RAM configurations for 4vCPUs? 8 GB seems overkill given dataset size 😅
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:22 PM
Unfortunately no…
Jul 31, 2023 (2 months ago)
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:23 PM
Any plans to add this in the future? It seems that things have stabilized now though
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:25 PM
We would love to! But most cloud platforms (including AWS which is what we run Typesense Cloud on at the moment) only offer a fixed set of RAM / CPU configs, so we’re kinda restricted by that. When we do colo servers in the future, we’d want to add more configs for sure.
09:27
Jason
09:27 PM
Btw, looking at last 3 days max CPU usage, your nodes are still exhausting CPU every now and then…
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:29 PM
Yes, you are right. Out of curiosity, shouldn’t we fall back to the other nodes in case of CPU exhaustion?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:30 PM
The failover only happens if the health endpoint is unhealthy for over 30s. In this case, the spikes seem much shorter than that
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:32 PM
Gotcha. Hmm, the next upgrade tier bumps our yearly spend to more than Algolia actually. We might need to look into self-hosting on GCP. In the meantime, we’ll bump the resource limits
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:36 PM
Out of curiosity, may I know how many searches per month you had in Algolia?
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:37 PM
~375k searches/m
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:41 PM
Hmmm! That looks way less than the traffic your Typesense cluster is getting… Almost 8x more if I extrapolate your avg requests per second to requests per month
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:42 PM
Perhaps Ethan can explain more into that. I think it’s because we need to make many more typesense requests to get to feature parity with Algolia
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
09:43 PM
I noticed in the network console when integrating the typesense instant search adapter that every change in query sends about 10 requests

1

09:43
Ethan
09:43 PM
Even if we only change one filter or letter in the query
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:43 PM
Hmm, is the UI public somewhere that I can take a look?
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
09:44 PM
Perhaps we can set the cache period to longer than 2 minutes
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
09:46 PM
I am seeing about 8 requests for each search, which lines up with what you were saying earlier Jason
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:47 PM
Yup, I see that too… And all of them are identical.

The typesense-instantsearch-adapter shouldn’t do this, unless it’s being somehow re-instantiated multiple times, may be due to some reactivity?
09:47
Jason
09:47 PM
For eg, have a look at the network requests in this demo which uses typesense-instantsearch-adapter: https://songs-search.typesense.org
09:50
Jason
09:50 PM
The adapter also has a client-side cache… so if it’s the same instance of the adapter, duplicate requests should just be served from the JS-based cache. Since it’s making separate duplicate network requests, my guess is that it’s being instantiated multiple times
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
09:51 PM
here’s a snippet of our implementation on the jobs page: https://gist.github.com/ethanhoroschak/42273d93f0c8069f5c0609945dfaafbf
09:52
Ethan
09:52 PM
we instantiate the adapter outside of the component so it would be strange if it was getting instantiated that many times
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:53 PM
I’m not too familiar with react, so I can’t tell if there’s any particular issue with this implementation… Could any of the widgets be re-rendered repeatedly somehow, that’s triggering a search?
09:54
Jason
09:54 PM
I see the same issue on page load as well btw, without typing in any query
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
09:55 PM
I console logged right below the adapter instantiation and made a bunch of requests and it doesnt look like the adapter was reinstantiated
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:57 PM
Are any of the params passed to any of the instantsearch widgets reactive, and might be changing values repeatedly as different parts of the UI load?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
09:57 PM
Image 1 for

1

09:59
Ethan
09:59 PM
we have query params set up which prefill the filters
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:03 PM
I meant to say “props” btw
10:06
Jason
10:06 PM
Hmm, so I tried changing the network throttling to Slow 3G in Chrome, and reloaded the page, I now see 16 duplicate requests… Could the loader component be causing repaints somehow?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:06 PM
yeah we have a query param hook that updates the url parameters and the states of the query and filters which uses useEffect with the params as a dependency
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:08 PM
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:21 PM
I believe it is an issue with out query param implementation causing rerenders
10:22
Ethan
10:22 PM
although the prop doesnt change - do rerenders cause typesense requests even if the props dont change?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:23 PM
I’ve seen search requests generated by Instantsearch anytime any of the widgets are re-rendered
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:28 PM
im testing with our algolia implementation we didnt have the duplicate request issue
10:28
Ethan
10:28 PM
it only sends 1 request with our same setup so switch to the typesense adapter was the only change
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:34 PM
Are you using SSR?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:37 PM
not for this page
10:50
Ethan
10:50 PM
first tab is algolia
Image 1 for first tab is algolia
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:52 PM
I now remember one user running into a similar issue a while ago… But I’m not sure if they managed to fix it or not.
10:53
Jason
10:53 PM
Here’s a Next.js app that uses the adapter and doesn’t do multiple requests if that helps narrow it down: https://showcase-nextjs-typesense-ecommerce-store.vercel.app

This is the source-code: https://github.com/typesense/showcase-nextjs-typesense-ecommerce-store
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:58 PM
hmm yeah this implementation doesnt use query params so likely wont have the rerender issues
10:58
Ethan
10:58 PM
I believe it may be the typesense adapter - since that was the only change
11:23
Ethan
11:23 PM
we have multiple rerenders but the prop values for the widgets dont change
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:27 PM
The adapter is a simple stateless (except for the cache) data transformation library that is called by instantsearch… So it doesn’t initiate any search requests by itself, unless it has a set of search requests given to it by Instantsearch…

So I’m not sure if there’s any fixes in the adapter that can be done to solve for this.
11:28
Jason
11:28 PM
Scratch that - I just found this issue which seems related: https://github.com/algolia/react-instantsearch/issues/2847
Rushil
Photo of md5-153deb374053266af0ef241c0c4ac510
Rushil
11:29 PM
Was just on that exact issue 😅

1

Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:29 PM
Looks like Instantsearch does trigger multiple searches, but it expects some different form of cache in aany custom search client used… Let me implement this and we’ll see if that fixes the issue

2

Aug 01, 2023 (2 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:32 AM
Could you upgrade to 2.7.1-5 of the adapter on your local machine and check if you see the issue now?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
06:10 PM
Looks like it works!
06:10
Ethan
06:10 PM
only sending 1 request per query change now
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:11 PM
Awesome! 🙌
06:12
Jason
06:12 PM
Once you deploy this to production your requests per second should go down significantly
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
06:13 PM
deploying now - will keep an eye out on requests

1

Aug 02, 2023 (2 months ago)
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
07:19 PM
seem to still be getting the network errors even though our cpu hasnt gone above 40% in the last hour
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:17 PM
Do you have a timestamp in UTC when you saw the last couple of errors? And also the exact search request with all the search params?
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:47 PM
last happened: Aug 2, 2023 9:05:00 PM UTC
10:47
Ethan
10:47 PM
sent the request in dms
Aug 03, 2023 (2 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:30 PM
Hmm, I can’t seem to find any evidence of timeouts from metrics on our side
Ethan
Photo of md5-43493fa2e131ee4b59d15256c7c083bf
Ethan
10:03 PM
hmm we got 181 errors in the last 24 hours - any other reason they could be happening?
Aug 04, 2023 (2 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:55 PM
I guess client network connectivity (internet issues) could also cause this…