Think about in search of a flight on a journey web site and ready for 10 seconds because the outcomes load up. Appears like an eternity, proper? Fashionable journey search platforms should return outcomes nearly immediately, even beneath heavy load. But, not way back, our journey search engine’s API had a p95 latency hovering round 10 seconds. This meant 5% of consumer searches, usually throughout peak visitors, took 10 seconds or extra. Outcome – pissed off customers, heavy bounce fee, and worse – misplaced gross sales. Latency discount, therefore, is a non-negotiable in such circumstances.
This text is a real-world case examine of how we developed our cloud infrastructure to slay the latency dragon. By leveraging Google Cloud Run for scalable compute and Redis for sensible caching, we reworked our search API’s p95 latency from ~10 seconds all the way down to ~2 seconds. Right here, we’ll stroll by this whole technique of latency discount. This consists of the efficiency bottlenecks, the optimizations, in addition to the dramatic enhancements they introduced on.
The Latency Bottleneck
The sky-high latency was a major problem. Delving deep into it, we discovered a number of culprits dragging down our response occasions. All of those had a typical issue – they made our search API do lots of heavy lifting on every request. Earlier than we might obtain an total latency discount, listed here are the problems we needed to repair:

- A number of backend calls: For every consumer question, the service contacted a number of downstream providers (airline fare suppliers, databases for ancillary information, and so forth.) sequentially. For instance, looking for a flight known as three totally different APIs concurrently at occasions, every taking round 2 to three seconds. The mixed latency stacked up near 10 seconds for some searches.
- No caching layer: Since there was no in-memory or Redis cache to shortly return latest outcomes, each request on the web site began from scratch. Even similar searches have been repeated inside minutes. This meant even standard routes or static knowledge (like airport particulars) have been fetched from the database or third events each time.
- Cloud Run chilly begins: Our service ran on Cloud Run (a serverless container platform). With default settings (0 minimal situations), when visitors was idle and a brand new request got here in, Cloud Run needed to spin up a container occasion. These chilly begins added a major delay (usually 1–2 seconds of overhead). This “startup tax” was deeply hurting our tail latency.
- Single Requests: Initially, we configured every Cloud Run container to deal with just one request at a time (concurrency = 1). This simplified request dealing with, however meant {that a} burst of 10 concurrent searches would instantly spin up 10 separate situations. With chilly begins and restricted CPU per occasion, our system struggled to deal with spikes effectively.
All these elements fashioned an ideal storm for sluggish p95 latency. Underneath the hood, our structure merely wasn’t optimized for pace. Queries have been doing redundant work, and our infrastructure was not tuned for a latency-sensitive workload. The excellent news? Every bottleneck was a chance for latency discount.
Additionally Learn: Cloud Run as a Serverless Platform to Deploy Containers
What We Modified for Latency Discount
We focused latency discount on two main fronts. Caching to keep away from repetitive work, and Cloud Run optimizations to reduce cold-start and processing overhead. Right here is how the backend developed:
Launched a Redis Caching Layer
We deployed a Redis cache to short-circuit costly operations on sizzling paths. The thought was fairly simple: retailer the outcomes of frequent or latest queries, and serve these straight for subsequent requests. For instance, when a consumer looked for flights from NYC to LON for sure dates, our API would fetch and compile the outcomes as soon as. It could then cache that “fare response” in Redis for a brief interval.
If one other consumer (or the identical consumer) made the identical search shortly after, the backend might return the cached fare knowledge in milliseconds, avoiding repeated calls to exterior APIs and database queries. By avoiding costly upstream calls on cache hits, we dramatically decreased latency for decent queries.
We utilized caching to different knowledge as properly, like static or slow-changing reference knowledge. e.g. airport codes, metropolis metadata, foreign money change charges, now used cache. Slightly than hitting our database for airport information on every request, the service now retrieves it from Redis (populated at startup or on first use). This lower down lots of minor lookups that have been including milliseconds right here and there (which add up beneath load).
Caching Musts
As a rule of thumb, we determined to “cache what’s sizzling.” In style routes, just lately fetched costs, and static reference knowledge like airport information have been all stored available in reminiscence. To maintain cached knowledge recent (necessary the place costs change), we set smart TTL (time-to-live) expirations and invalidation guidelines. As an illustration, fare search outcomes have been cached for a couple of minutes at most.
After that, they’d expire, so new searches can get up-to-date costs. For extremely unstable knowledge, we might even proactively invalidate cache entries after we detected adjustments. Because the Redis docs be aware, flight costs usually replace solely “each few hours.” So, a brief TTL mixed with event-based invalidation balances freshness vs. pace.
The end result? On cache hits, the response time per question dropped from a number of seconds to a couple hundred milliseconds or much less. This was all due to Redis, which may serve knowledge blazingly quick over reminiscence. In reality, business experiences present that utilizing an in-memory “fare cache” can flip a multi-second flight question right into a response in simply tens of milliseconds. Whereas our outcomes weren’t fairly that immediate throughout the board, this caching layer delivered an enormous enhance. Important latency discount was achieved, particularly for repeat searches and standard queries.
Optimized Cloud Run Settings for Latency Discount
Caching helped with repeated work, however we additionally wanted to optimise efficiency for first-time queries and scale-ups. We subsequently fine-tuned our Cloud Run service for low latency.
All the time-one heat occasion
We enabled minimal situations = 1 for the Cloud Run service. This assured that no less than one container is up and able to obtain requests even throughout idle durations. The primary consumer request not incurs a chilly begin penalty. Google’s engineers be aware that protecting a minimal occasion can dramatically enhance efficiency for latency-sensitive apps by eliminating the zero-to-one startup delay.
In our case, setting min situations to 1 (and even 2 or 3 throughout peak hours) meant customers weren’t caught ready for containers to spin up. The p95 latency noticed a major drop simply from this one optimisation alone.
Elevated concurrency per container
We revisited our concurrency setting. After making certain our code might deal with parallel requests safely, we raised the Cloud Run concurrency from 1 to the next quantity. We experimented with values like 5, 10, and finally settled on 5 for our workload. This meant every container might deal with as much as 5 simultaneous searches earlier than a brand new occasion wanted to start out.
Outcome – fewer new situations spawned throughout visitors spikes. This, in flip, meant fewer chilly begins and fewer overhead. Basically, we let every container do a bit extra work in parallel, as much as the purpose the place CPU utilization was nonetheless wholesome. We monitored CPU and reminiscence carefully – our aim was to make use of every occasion effectively with out overloading it.
This tuning helped clean out latency throughout bursts: if 10 requests got here in without delay, as an alternative of 10 chilly begins (with concurrency = 1), we’d deal with them with 2 heat situations dealing with 5 every, protecting issues snappy.
Sooner startup and processing
We additionally made some app-level tweaks to start out up faster and run sooner on Cloud Run. Additionally, we enabled Cloud Run’s startup CPU enhance characteristic, which supplies a burst of CPU to new situations throughout startup. We additionally used a slim base container picture and loaded solely important modules at startup.
Sure initialization steps (like loading massive config information or warming sure caches) have been additionally moved to the container startup section as an alternative of at request time. Because of min situations, this startup ran sometimes. In observe, by the point a request arrived, the occasion was already bootstrapped (database connections open, config loaded, and so forth.), so it might begin processing the question instantly.
We primarily paid the startup value as soon as and reused it throughout many requests, somewhat than paying a little bit of that value on every request.
The outcomes have been immediately seen with these optimisations in place. We monitored our API’s efficiency earlier than vs. after. The p95 latency plummeted from roughly 10 seconds, all the way down to round 2 seconds. This was a mind-blowing 5 occasions sooner loading expertise for our customers. Even the common latency improved (for cache-hitting queries, it was usually
Extra importantly, the responses grew to become constant and dependable. Customers not skilled the painful and much-dreaded 10-second waits. The system might deal with visitors spikes gracefully: Cloud Run scaled out to further situations when wanted. With heat containers and better concurrency, it did so with out choking on chilly begins.
In the meantime, Redis caching absorbed repeated queries and decreased load on our downstream APIs and databases. This additionally not directly improved latency by stopping these programs from turning into bottlenecked.
The online impact was a snappier, extra scalable search API that stored up with our prospects’ expectations of fast responses and a clean expertise.
Key Takeaways
From your complete set of optimizations we undertook for latency discount, listed here are the important thing takeaways and necessary factors so that you can contemplate.
- Measure and goal tail latency: Specializing in p95 latency (and above) is essential. It highlights the worst-case delays that actual customers really feel. Decreasing ninety fifth percentile latency from 10s to 2s made our worst experiences 5 occasions higher, sooner. An enormous win for consumer satisfaction! So, all the time monitor these high-percentile metrics, not simply the common.
- Use caching to keep away from redundant work: Introducing a Redis cache proved to be a game-changer for us. Caching regularly requested knowledge dramatically cuts response occasions by serving outcomes from reminiscence. The mixture of in-memory pace and considerate invalidation (utilizing TTLs and updates) can offload costly computations out of your backend.
- Optimize serverless for pace: Cloud Run gave us straightforward scaling, however to make it actually low-latency, we leveraged its notable options – protecting minimal situations heat. This eradicated cold-start lag, and tuned concurrency and sources so situations are used effectively with out getting overwhelmed. A little bit of upfront value (always-on occasion) might be properly well worth the payoff in constant efficiency.
- Parallelize and streamline the place attainable: We re-examined our request circulation to take away useless serialization and delays. By doing issues like parallelizing exterior calls and doing one-time setup throughout startup (not for each request), we shaved seconds off the essential path. Each micro-optimization (non-blocking I/O, sooner code, preloading knowledge) provides up in a high-scale, distributed system.
- Steady profiling and iteration: Lastly, an necessary factor to notice right here is that this was an iterative journey. We used monitoring and profiling to search out the largest bottlenecks, addressed them one after the other, and measured the influence. Efficiency tuning is seldom one-and-done. It’s about data-driven enhancements and typically inventive fixes to succeed in your latency targets.
Conclusion
Whereas these latency discount methods could seem an excessive amount of to deal with without delay, a scientific verify by each is sort of clean in observe. The one unparalleled plus for your complete train is that we turned our journey search API from a sluggish expertise into one which feels immediate. In a world the place customers count on solutions “yesterday,” reducing p95 latency from 10s to 2s made all of the distinction in delivering a clean journey search expertise.
Login to proceed studying and luxuriate in expert-curated content material.