开发者

Ajax requests/responses: how to make them lightning fast?

开发者 https://www.devze.com 2023-01-30 08:16 出处:网络
I came across a site that does something very similar to Google Suggest. When you type in 2 characters in the search box (e.g. \"ca\" if you are searching for \"canon\" products), it makes 4 Ajax requ

I came across a site that does something very similar to Google Suggest. When you type in 2 characters in the search box (e.g. "ca" if you are searching for "canon" products), it makes 4 Ajax requests. Each request s开发者_JAVA百科eems to get done in less than 125ms. I've casually observed Google Suggest taking 500ms or longer.

In either case, both sites are fast. What are the general concepts/strategies that should be followed in order to get super-fast requests/responses? Thanks.

EDIT 1: by the way, I plan to implement an autocomplete feature for an e-commerce site search where it 1.) provides search suggestion based on what is being typed and 2.) a list of potential products matches based on what has been typed so far. I'm trying for something similar to SLI Systems search (see http://www.bedbathstore.com/ for example).


This is a bit of a "how long is a piece of string" question and so I'm making this a community wiki answer — everyone feel free to jump in on it.

I'd say it's a matter of ensuring that:

  1. The server / server farm / cloud you're querying is sized correctly according to the load you're throwing at it and/or can resize itself according to that load
  2. The server /server farm / cloud is attached to a good quick network backbone
  3. The data structures you're querying server-side (database tables or what-have-you) are tuned to respond to those precise requests as quickly as possible
  4. You're not making unnecessary requests (HTTP requests can be expensive to set up; you want to avoid firing off four of them when one will do); you probably also want to throw in a bit of hysteresis management (delaying the request while people are typing, only sending it a couple of seconds after they stop, and resetting that timeout if they start again)
  5. You're sending as little information across the wire as can reasonably be used to do the job
  6. Your servers are configured to re-use connections (HTTP 1.1) rather than re-establishing them (this will be the default in most cases)
  7. You're using the right kind of server; if a server has a large number of keep-alive requests, it needs to be designed to handle that gracefully (NodeJS is designed for this, as an example; Apache isn't, particularly, although it is of course an extremely capable server)
  8. You can cache results for common queries so as to avoid going to the underlying data store unnecessarily


You will need a web server that is able to respond quickly, but that is usually not the problem. You will also need a database server that is fast, and can query very fast which popular search results start with 'ca'. Google doesn't use conventional database for this at all, but use large clusters of servers, a Cassandra-like database, and a most of that data is kept in memory as well for quicker access.

I'm not sure if you will need this, because you can probably get pretty good results using only a single server running PHP and MySQL, but you'll have to make some good choices about the way you store and retrieve the information. You won't get these fast results if you run a query like this:

select
  q.search
from
  previousqueries q
where
  q.search LIKE 'ca%'
group by
  q.search
order by
  count(*) DESC
limit 1

This will probably work as long as fewer than 20 people have used your search, but will likely fail on you before you reach a 100.000.


This link explains how they made instant previews fast. The whole site highscalability.com is very informative.

Furthermore, you should store everything in memory and should avoid retrieving data from the disc (slow!). Redis for example is lightning fast!


You could start by doing a fast search engine for your products. Check out Lucene for full text searching. It is available for PHP, Java and .NET amongst other.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号