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

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

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.


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.