Skip to main content

Search Results

Depending on how you implement searches (if you choose to do so), you will need to cache the results temporarily somewhere. Below is an explanation of th two main ways you might go about this.

Option 1: Using a pre-built search solution

SaaS (software-as-a-service) providers often provide search features for websites that you can integrate. These will usually have a caching solution built into them, in which case you won't need to think about this. If they don't, you will likely need to configure them to use your own caching server for this purpose.

Option 2: Doing it yourself

If you're implementing search functionality yourself, then bear in mind that most SQL queries designed to perform searches can be fairly expensive in computation terms. For instance, searching 50 million records in a table for a whole bunch of specific attributes, especially when e.g. subqueries are involved, can take significant time and put a lot of strain on your database server. This is especially so if every single search made by a user runs the query again.

You should store the results of a query - in some minimised form - in your caching server, with an expiry time. For example, if a user searches your website for "huge anime tiddies", you should add a corresponding cache entry that expires in some fairly short period (e.g. 15 minutes). Then, when another user searches for the same query, you can return the cached result if one exists, meaning the query won't be run more often than once every 15 minutes and saving your database a load of strain.

In Redis, that might look something like this:

HSET "SEARCH:ARTWORKS:HUGE ANIME TIDDIES" "PAGE:0:25" "<the search results in JSON form>"