โ๐๐จ๐ฐ ๐๐จ๐จ๐ ๐ฅ๐ ๐ฌ๐ก๐จ๐ฐ๐ฌ ๐ฒ๐จ๐ฎ ๐ฌ๐๐๐ซ๐๐ก ๐ซ๐๐ฌ๐ฎ๐ฅ๐ญ๐ฌ ๐ข๐ง ๐.๐ ๐ฌ๐๐๐จ๐ง๐๐ฌ (๐ญ๐ก๐๐ฒโ๐ซ๐ ๐๐ก๐๐๐ญ๐ข๐ง๐ , ๐๐ง๐ ๐ฒ๐จ๐ฎ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐ญ๐จ๐จ)โ
Source: Dev.to
Hmโฆ I think internet cannot be searched; you only look up on Google, right?
You type โbest jollof rice in Lagosโ into Google. Three hundred milliseconds later, you have ten perfect results. In that time, Google supposedly searched through hundreds of billions of web pages, ranked them by relevance, personalized results based on your location and history, and sent everything back to your phone.
Except they didnโt. Thatโs physically impossible. Hereโs what actually happened, and why it changes everything about how you should build fast applications.
The illusion of realโtime search
When you press search, youโre not actually searching the internet. Youโre searching Googleโs preโcomputed index of the internet, which is a completely different thing.
Imagine a library with one million books and someone asks you to find every book that mentions โjollof rice.โ Scanning every page would take days or weeks. Instead, you could spend months beforehand creating an indexโa massive book that lists every word that appears in the library and exactly which books and pages contain that word.
When someone asks for โjollof rice,โ you just open the index to the โjollofโ section, see the list of books and page numbers, do the same for โrice,โ find the books that appear in both lists, and hand them the answer in seconds. You did the hard work before anyone asked the question.
Thatโs exactly what Google does, just at planetary scale.
What actually happens when you search
Letโs break down the steps that occur in those three hundred milliseconds:
- Frontโend servers (GFE) โ Your query hits Googleโs globally distributed frontโend servers, located close to you (e.g., a West African GFE if youโre in Lagos).
- Spelling correction โ Google checks for typos and autoโcorrects if needed. This runs on Googleโs internal infrastructure called Borg (the precursor to Kubernetes).
- Index shards โ The corrected query is sent to Googleโs index shards. The index is split into thousands of shards distributed across data centers worldwide. Each shard holds a portion of the inverted index plus the actual document data.
- Topโk fetch โ Each relevant shard quickly fetches the top ~1,000 possible documents that match the query. The index is already sorted and optimized for this lookup.
- Ranking โ All those results are sent to Googleโs ranking system, which uses over two hundred signals (location, search history, freshness, backlinks, mobileโfriendliness, page speed, etc.) to sort them in milliseconds.
- Formatting โ The top ten results are formatted and sent back to your browser. The whole process takes under half a second.
Critical insight: Google isnโt searching billions of pages in real time; itโs looking up preโcomputed results in an index that is continuously updated in the background.
The inverted index: Googleโs secret weapon
An inverted index stores words and the list of documents that contain them, rather than storing whole documents.
Normal storage
Document 1 contains โI love jollof riceโ
Inverted index
- โjollofโ appears in: Documentโฏ1, Documentโฏ2
- โriceโ appears in: Documentโฏ1
- โLagosโ appears in: Documentโฏ2
When someone searches for โjollof Lagos,โ the system instantly knows Documentโฏ2 is the only one containing both wordsโno document scanning required.
Googleโs inverted index tracks hundreds of billions of web pages and trillions of words. Because it is preโcomputed and sharded across thousands of servers, lookups are extremely fast.
Incremental indexing: How Google stays current
The web changes constantlyโnew pages appear, old pages update, sites go offline. Rebuilding the entire index from scratch each time would be impossible.
Google uses incremental indexing:
- Crawlers continuously browse the web, looking for new or changed content.
- When a change is detected, only the relevant portions of the index are updated.
- Popular news sites are crawled every few minutes; smaller sites may be crawled every few days or weeks.
The index is never perfectly upโtoโdate, but itโs current enough that users donโt notice. Breaking news can appear in search results within minutes; a small blog post might take hours or days.
Why 99โฏ% of apps are slow
Most applications are slow because they perform full database scans or complex calculations on every request. Googleโs approach is the opposite: preโcompute everything you possibly can.
Example: Foodโdelivery app
Slow approach
- User opens app.
- App gets userโs location.
- App queries the database: โFind all restaurants, calculate distance, filter within 5โฏkm, sort by rating.โ
- Database scans thousands of rows, performs distance calculations, and returns results after a few seconds.
Googleโstyle approach
- Every few minutes, preโcompute which restaurants are near every major neighborhood.
- Store these lists in fast memory (e.g., Redis).
- When the user opens the app, look up the preโcomputed list for the relevant neighborhood.
- Results appear in a few hundred milliseconds.
The expensive work (distance calculations, filtering, sorting) is moved to background jobs, not request time.
Real examples where preโcomputation matters
- YouTube recommendations โ Algorithms run continuously in the background, updating recommendation lists for hundreds of millions of users. When you open the app, you see a list that was preโcomputed minutes or hours ago.
- Instagram feed โ The feed is assembled in the background based on who you follow and your engagement history. Pullโtoโrefresh simply fetches the preโbuilt feed.
- Eโcommerce search (Amazon) โ Inverted indexes of products by keywords, categories, and attributes allow instant lookups instead of scanning the entire product catalog.
- Banking dashboards โ Account balances and transaction histories are aggregated and cached for dashboard views; the main transaction database handles new writes, while reporting views are preโcomputed.
The tradeโoff: freshness vs. speed
Preโcomputation introduces a single major tradeโoff: the data may be slightly stale. If Googleโs index was updated ten minutes ago and a website changed five minutes ago, the search results wonโt reflect that change yet.
For most use cases, this is acceptable. Users expect fast responses, not perfect realโtime accuracy. The slight lag is a small price to pay for the dramatic performance gains that preโcomputed indexes provide.