I'm trying to add "autocomplete" functionality to a search bar. An example of the functionality I want to replicate is Twitter's search function: you type in a few letters, and Twitter returns a few of the closest-matching users. Looking at network logs, it looks like the search does a POST request followed by a few GET requests containing users' profile pictures and maybe other data.
After looking through related questions on the topic, most of the solutions I've seen use JQuery to populate autocomplete with a list on page load. My initial thoughts were to send an AJAX request on keystroke to an endpoint that queries the database and returns the top N results. This seems like a really inefficient use of resources, though, and I can't imagine it scaling well.
This thread addresses some of those issues: Ajax request to SQL server opens too many pooled connections
From what I currently understand, these are the major problems I see:
- If the dataset I want to search has a large volume of records, putting them all into autocomplete defeats the point of convenience.
- If I truncate the list, certain items will never appear (e.g., there might be ten users with names starting with "AAA" and one with "AAZ," etc.)
- If I update the list, I run the risk of generating an excessive amount of requests.
For the last point, at least, I've considered some kind of timed buffer that waits for the user to stop typing before sending a request.
Basically, how can I balance responsiveness with server load? Is this just a naturally resource-intensive process?