DEV Community

Cover image for โ€œ๐‡๐จ๐ฐ ๐†๐จ๐จ๐ ๐ฅ๐ž ๐ฌ๐ก๐จ๐ฐ๐ฌ ๐ฒ๐จ๐ฎ ๐ฌ๐ž๐š๐ซ๐œ๐ก ๐ซ๐ž๐ฌ๐ฎ๐ฅ๐ญ๐ฌ ๐ข๐ง ๐ŸŽ.๐Ÿ‘ ๐ฌ๐ž๐œ๐จ๐ง๐๐ฌ (๐ญ๐ก๐ž๐ฒโ€™๐ซ๐ž ๐œ๐ก๐ž๐š๐ญ๐ข๐ง๐ , ๐š๐ง๐ ๐ฒ๐จ๐ฎ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐ญ๐จ๐จ)โ€
Creative Coder
Creative Coder

Posted on • Originally published at dev.to

โ€œ๐‡๐จ๐ฐ ๐†๐จ๐จ๐ ๐ฅ๐ž ๐ฌ๐ก๐จ๐ฐ๐ฌ ๐ฒ๐จ๐ฎ ๐ฌ๐ž๐š๐ซ๐œ๐ก ๐ซ๐ž๐ฌ๐ฎ๐ฅ๐ญ๐ฌ ๐ข๐ง ๐ŸŽ.๐Ÿ‘ ๐ฌ๐ž๐œ๐จ๐ง๐๐ฌ (๐ญ๐ก๐ž๐ฒโ€™๐ซ๐ž ๐œ๐ก๐ž๐š๐ญ๐ข๐ง๐ , ๐š๐ง๐ ๐ฒ๐จ๐ฎ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐ญ๐จ๐จ)โ€

๐‡๐ฆ๐ฆ ๐ˆ ๐ญ๐ก๐ข๐ง๐ค ๐ข๐ง๐ญ๐ž๐ซ๐ง๐ž๐ญ ๐œ๐š๐ง๐ง๐จ๐ญ ๐›๐ž ๐ฌ๐ž๐š๐ซ๐œ๐ก๐ž๐, ๐ฒ๐จ๐ฎ ๐จ๐ง๐ฅ๐ฒ ๐ฅ๐จ๐จ๐ค ๐ฎ๐ฉ ๐จ๐ง ๐ ๐จ๐จ๐ ๐ฅ๐ž ๐ฌ๐ž๐ž ๐ฐ๐ก๐ฒ?

You type โ€œ๐’ƒ๐’†๐’”๐’• ๐’‹๐’๐’๐’๐’๐’‡ ๐’“๐’Š๐’„๐’† ๐’Š๐’ ๐‘ณ๐’‚๐’ˆ๐’๐’”โ€ 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.

๐“๐ก๐ž ๐ข๐ฅ๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง ๐จ๐Ÿ ๐ซ๐ž๐š๐ฅ-๐ญ๐ข๐ฆ๐ž ๐ฌ๐ž๐š๐ซ๐œ๐ก

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 you have a library with one million books and someone asks you to find every book that mentions โ€œjollof rice.โ€ If you have to open every single book and scan every page, youโ€™d need days or weeks. But what if you spent months beforehand creating an index, a massive book that lists every word that appears in your library and exactly which books and pages contain that word?

Now when someone asks for โ€œjollof rice,โ€ you just open your index book 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.

๐–๐ก๐š๐ญ ๐š๐œ๐ญ๐ฎ๐š๐ฅ๐ฅ๐ฒ ๐ก๐š๐ฉ๐ฉ๐ž๐ง๐ฌ ๐ฐ๐ก๐ž๐ง ๐ฒ๐จ๐ฎ ๐ฌ๐ž๐š๐ซ๐œ๐ก

Letโ€™s break down what happens in those three hundred milliseconds:

๐’๐ญ๐ž๐ฉ ๐Ÿ: Your search query hits Googleโ€™s Front End servers (GFE). These are globally distributed servers that sit close to users. If youโ€™re in Lagos, youโ€™re hitting a GFE somewhere in West Africa, not one in California.

๐’๐ญ๐ž๐ฉ 2: The query goes through spelling correction. Googleโ€™s algorithms check if you meant something else and might auto-correct obvious typos. This runs on Googleโ€™s internal infrastructure called Borg (which later inspired Kubernetes).

๐’๐ญ๐ž๐ฉ 3: Your corrected query gets sent to Googleโ€™s index shards. The index isnโ€™t one huge database. Itโ€™s split into thousands of shards (pieces) distributed across data centers worldwide. Each shard holds a portion of the internetโ€™s inverted index plus the actual document data.

๐’๐ญ๐ž๐ฉ 4: Each relevant shard quickly fetches the top one thousand possible documents that match your query. This is fast because the index is already sorted and optimized for exactly this type of lookup.

๐’๐ญ๐ž๐ฉ 5: All those results get sent to Googleโ€™s ranking system, which uses over two hundred signals to sort them. Your location, past search history, how recently the page was updated, how many other sites link to it, whether itโ€™s mobile-friendly, page load speed, and dozens of other factors all get calculated in milliseconds.

๐’๐ญ๐ž๐ฉ 6: The top ten results get formatted and sent back to your browser. The whole process from start to finish takes under half a second.

The critical insight: Google isnโ€™t searching billions of pages in real-time. Theyโ€™re looking up pre-computed results in an index that was built ahead of time and is constantly being updated in the background.

๐“๐ก๐ž ๐ข๐ง๐ฏ๐ž๐ซ๐ญ๐ž๐ ๐ข๐ง๐๐ž๐ฑ: ๐†๐จ๐จ๐ ๐ฅ๐žโ€™๐ฌ ๐ฌ๐ž๐œ๐ซ๐ž๐ญ ๐ฐ๐ž๐š๐ฉ๐จ๐ง

An inverted index is one of the most powerful data structures in computer science. Instead of storing documents and their contents, you store words and lists of which documents contain them.

Normal storage: Document 1 contains โ€œI love jollof riceโ€
Normal storage: Document 2 contains โ€œBest jollof in Lagosโ€

Inverted index:

  • โ€œ๐’‹๐’๐’๐’๐’๐’‡โ€ ๐’‚๐’‘๐’‘๐’†๐’‚๐’“๐’” ๐’Š๐’: ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’• 1, ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’• 2
  • โ€œ๐’“๐’Š๐’„๐’†โ€ ๐’‚๐’‘๐’‘๐’†๐’‚๐’“๐’” ๐’Š๐’: ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’• 1
  • โ€œ๐‘ณ๐’‚๐’ˆ๐’๐’”โ€ ๐’‚๐’‘๐’‘๐’†๐’‚๐’“๐’” ๐’Š๐’: ๐‘ซ๐’๐’„๐’–๐’Ž๐’†๐’๐’• 2

Now when someone searches for โ€œjollof Lagos,โ€ you instantly know Document 2 is the only one that contains both words. You didnโ€™t scan any documents. You just looked up two words in your index.

Googleโ€™s inverted index is incomprehensibly massive. It tracks hundreds of billions of web pages and trillions of words. But because itโ€™s pre-computed and sharded across thousands of servers, lookups are extremely fast.

๐ˆ๐ง๐œ๐ซ๐ž๐ฆ๐ž๐ง๐ญ๐š๐ฅ ๐ข๐ง๐๐ž๐ฑ๐ข๐ง๐ : ๐‡๐จ๐ฐ ๐†๐จ๐จ๐ ๐ฅ๐ž ๐ฌ๐ญ๐š๐ฒ๐ฌ ๐œ๐ฎ๐ซ๐ซ๐ž๐ง๐ญ

The internet changes constantly. New pages appear, old pages update, sites go offline. If Google had to rebuild their entire index from scratch every time something changed, theyโ€™d never keep up.

Instead, they use incremental indexing. Googleโ€™s crawlers constantly browse the web, looking for new or changed content. When they find something new, they update just the relevant portions of the index. Popular news sites get crawled every few minutes. Smaller sites might get crawled every few days or weeks.

This means Googleโ€™s index is never perfectly up-to-date, but itโ€™s current enough that users donโ€™t notice. A breaking news article might appear in search results within minutes of being published. A small blog post might take hours or days.

๐–๐ก๐ฒ ๐Ÿ—๐Ÿ—% ๐จ๐Ÿ ๐š๐ฉ๐ฉ๐ฌ ๐š๐ซ๐ž ๐ฌ๐ฅ๐จ๐ฐ

Hereโ€™s the brutal truth: most applications are slow because they do full database scans or complex calculations every single time a user makes a request. Googleโ€™s approach is the opposite: pre-compute everything you possibly can.

Letโ€™s say you built a food delivery app. A user opens the app and wants to see restaurants near them. The slow approach is:

  1. User opens app
  2. App gets userโ€™s current location
  3. App queries database: โ€œFind all restaurants, calculate distance from user, filter by those within five kilometers, sort by ratingโ€
  4. Database scans through thousands of restaurants doing complex distance calculations
  5. User waits three seconds staring at a loading spinner

The Google approach would be:

  1. Every few minutes, pre-compute which restaurants are near every major neighborhood in your city
  2. Store these pre-computed lists in fast memory (like Redis)
  3. User opens app
  4. App gets userโ€™s location
  5. App looks up the pre-computed list for that neighborhood
  6. Results appear in two hundred milliseconds

You moved the expensive work (calculating distances, filtering, sorting) from request time to background time. The user doesnโ€™t wait for computation to happen. They just see the pre-computed result.

๐‘๐ž๐š๐ฅ ๐ž๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž๐ฌ ๐ฐ๐ก๐ž๐ซ๐ž ๐ฉ๐ซ๐ž-๐œ๐จ๐ฆ๐ฉ๐ฎ๐ญ๐š๐ญ๐ข๐จ๐ง ๐ฆ๐š๐ญ๐ญ๐ž๐ซ๐ฌ

๐˜๐จ๐ฎ๐“๐ฎ๐›๐ž ๐ซ๐ž๐œ๐จ๐ฆ๐ฆ๐ž๐ง๐๐š๐ญ๐ข๐จ๐ง๐ฌ: YouTube doesnโ€™t calculate โ€œwhat should we recommend to this userโ€ every time you open the app. Their algorithms run constantly in the background, updating recommendation lists for hundreds of millions of users. When you open YouTube, youโ€™re seeing a list that was pre-computed minutes or hours ago.

๐ˆ๐ง๐ฌ๐ญ๐š๐ ๐ซ๐š๐ฆ ๐Ÿ๐ž๐ž๐: Instagram doesnโ€™t generate your feed in real-time when you pull to refresh. They pre-assemble your feed in the background based on who you follow and what you engage with. Refreshing just fetches the pre-built feed.

๐„-๐œ๐จ๐ฆ๐ฆ๐ž๐ซ๐œ๐ž ๐ฌ๐ž๐š๐ซ๐œ๐ก: Amazon doesnโ€™t scan their entire product database every time you search. They maintain inverted indexes of products by keywords, categories, and attributes. Your search looks up the index, not the raw product data.

๐๐š๐ง๐ค๐ข๐ง๐  ๐๐š๐ฌ๐ก๐›๐จ๐š๐ซ๐๐ฌ: When you check your account balance and transaction history, the bank isnโ€™t querying their main transaction database in real-time. That data was already aggregated and cached specifically for dashboard views. The main database handles new transactions, but reporting views are pre-computed.

๐“๐ก๐ž ๐ญ๐ซ๐š๐๐ž๐จ๐Ÿ๐Ÿ: ๐Ÿ๐ซ๐ž๐ฌ๐ก๐ง๐ž๐ฌ๐ฌ ๐ฏ๐ฌ ๐ฌ๐ฉ๐ž๐ž๐

Pre-computation has one major tradeoff: the data might be slightly stale. If Googleโ€™s index was updated ten minutes ago and a website changed five minutes ago, Googleโ€™s search results wonโ€™t reflect that change yet.

For most use cases, this is acceptable. Users donโ€™t expect real-time perfection. They expect fast results that are reasonably current. The trick is deciding what level of staleness your application can tolerate.

For a social media feed, showing posts from five minutes ago is fine. For a stock trading app, showing prices from five minutes ago would be disastrous. You need real-time data there, which means you canโ€™t fully pre-compute.

But even in scenarios requiring real-time data, you can pre-compute parts of the work. A stock trading app might pre-compute which stocks a user is watching and just update the prices in real-time, rather than recalculating the entire watchlist for every price update.

๐‡๐จ๐ฐ ๐ญ๐จ ๐ญ๐ก๐ข๐ง๐ค ๐ฅ๐ข๐ค๐ž ๐†๐จ๐จ๐ ๐ฅ๐ž ๐ฐ๐ก๐ž๐ง ๐›๐ฎ๐ข๐ฅ๐๐ข๐ง๐  ๐ฒ๐จ๐ฎ๐ซ ๐š๐ฉ๐ฉ

Ask yourself these questions:

๐–๐ก๐š๐ญ ๐š๐ฆ ๐ˆ ๐œ๐š๐ฅ๐œ๐ฎ๐ฅ๐š๐ญ๐ข๐ง๐  ๐ซ๐ž๐ฉ๐ž๐š๐ญ๐ž๐๐ฅ๐ฒ ๐ญ๐ก๐š๐ญ ๐๐จ๐ž๐ฌ๐งโ€™๐ญ ๐œ๐ก๐š๐ง๐ ๐ž ๐จ๐Ÿ๐ญ๐ž๐ง? If youโ€™re showing the same product catalog to every user, calculate it once and cache it. Donโ€™t rebuild it for every request.

๐–๐ก๐š๐ญ ๐œ๐š๐ง ๐ˆ ๐ฉ๐ซ๐ž๐๐ข๐œ๐ญ ๐ฎ๐ฌ๐ž๐ซ๐ฌ ๐ฐ๐ข๐ฅ๐ฅ ๐š๐ฌ๐ค ๐Ÿ๐จ๐ซ? If most users search for similar things, pre-compute those popular queries. Google doesnโ€™t pre-compute every possible search, but they definitely have fast paths for common ones.

๐–๐ก๐š๐ญ ๐œ๐š๐ฅ๐œ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง๐ฌ ๐œ๐š๐ง ๐ก๐š๐ฉ๐ฉ๐ž๐ง ๐ข๐ง ๐ญ๐ก๐ž ๐›๐š๐œ๐ค๐ ๐ซ๐จ๐ฎ๐ง๐?Analytics, reports, recommendations, leaderboards, these donโ€™t need to be real-time. Calculate them every few minutes or hours and show the pre-computed result.

๐‚๐š๐ง ๐ˆ ๐›๐ซ๐ž๐š๐ค ๐ž๐ฑ๐ฉ๐ž๐ง๐ฌ๐ข๐ฏ๐ž ๐จ๐ฉ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง๐ฌ ๐ข๐ง๐ญ๐จ ๐ฌ๐ฆ๐š๐ฅ๐ฅ๐ž๐ซ ๐ฉ๐ข๐ž๐œ๐ž๐ฌ?Instead of one massive calculation at request time, can you do small incremental updates constantly in the background?

๐“๐ก๐ž ๐œ๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐จ๐ง ๐ญ๐จ ๐ž๐ฏ๐ž๐ซ๐ฒ๐ญ๐ก๐ข๐ง๐  ๐ฐ๐žโ€™๐ฏ๐ž ๐ฅ๐ž๐š๐ซ๐ง๐ž๐

Day 1 taught us to move data closer to users (Netflixโ€™s Open Connect). Day 2 showed us efficiency through simplicity (WhatsAppโ€™s Erlang). Day 3 explained caching frequently accessed data. Day 4 covered distributing work across servers. Now Day 5 reveals the ultimate optimization: donโ€™t do the work when users ask, do it before they ask.

All these principles work together. Googleโ€™s search results are cached, served from nearby data centers, distributed across thousands of servers, and most importantly, pre-computed. Thatโ€™s why it feels instant.

Join the dev Community - https://www.linkedin.com/groups/16192044/

The practical takeaway

Stop making users wait for expensive operations. Move that work to background jobs. Pre-compute what you can predict. Cache what you canโ€™t predict but gets requested frequently. Update in the background at regular intervals.

Your app doesnโ€™t need to be as complex as Google to benefit from this thinking. Even simple pre-computation can transform a three-second load time into a three-hundred-millisecond load time.

Tomorrow (Day 6): Weโ€™re diving into why your messages on WhatsApp get delivered even when the recipientโ€™s phone is off. The secret is message queues, the invisible infrastructure that makes asynchronous communication possible.

JOIN THE CLASS AND SEE HOW GOOGLE INVERTED INDEX APPROACH COULD MAKE YOUR APP FASTER AND MORE RELIABLE? {https://ssic.ng}

Type โ€œKeep goingโ€ if youโ€™re ready to think like Google and stop making users wait for calculations that couldโ€™ve been done hours ago.

SystemDesign #LoadBalancing #WebDevelopment #SoftwareEngineering #BackendDevelopment #Scalability #CloudComputing #DevOps #TechArchitecture #DistributedSystems #HighAvailability #SoftwareArchitecture #NigerianDevelopers #TechCommunity #CodingTips #tcm #creativethinkers #claymic

Top comments (0)