How does "dynamic" searching work?
Hello, consider a social media platform where we have a search bar. Normally, let say we are looking for a person John Doe. When we first type John, without even having to press enter, there are a list of suggestions that appear. I was wondering how does that work?
I might have some ideas:
The ineffective one:
There is kind of an infinite loop that triggers and send request each time the state of the search bar is changed.
Another one is maybe (a bit more effective I guess), store every user somewhere, like in the cache, I know we use Redis to cache info? So maybe instead of always sending a request to the server, just cache the users and look from there? Maybe we try to invalidate the cache and fetch a new one at regular time interval or when particular actions are triggered?
12 Replies
just use "input" event on your input to capture changes
make sure to cancel request when users keep typing nonstop
ah ok didn't know we have an input event, basically it is triggered each time we type in something?
in addition to what he said, you should consider some debouncing
also, the
search
event for an input with type search
(on google chrome) already does some of the debouncing for youoh ok didn't know the debouncing thing, seems interesting
do we use a cache to store the info though?
it depend on your use cases
when would we need to use a cache?
I just know the theoretical aspect of a cache
didn't use one yet
you cache something when it is frequently used but rarely changed
ohhhh ok
in this case it's frequently changed
Cache is also something to worry about when you run into performance issues
Don't worry about it now
yeah cache invalidation is one of the 2 hardest problems in programming
the other is naming things and off-by-one errors
I forgot who tell me that but yeah I was told the same thing 😂
two hardest thing in programming is cache invalidation and naming things
and off-by-one errors