Think about searching for a flight on a journey web site and ready for 10 seconds because the outcomes load up. Looks like an eternity, proper? Fashionable journey search platforms should return outcomes virtually immediately, even below heavy load. But, not way back, our journey search engine’s API had a p95 latency hovering round 10 seconds. This meant 5% of person searches, typically throughout peak site visitors, took 10 seconds or extra. Consequence – pissed off customers, heavy bounce fee, and worse – misplaced gross sales. Latency discount, therefore, is a non-negotiable in such instances.
This text is a real-world case research of how we advanced our cloud infrastructure to slay the latency dragon. By leveraging Google Cloud Run for scalable compute and Redis for good caching, we reworked our search API’s p95 latency from ~10 seconds right down to ~2 seconds. Right here, we’ll stroll via this complete 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 instances. All of those had a typical issue – they made our search API do a variety of heavy lifting on every request. Earlier than we may obtain an total latency discount, listed here are the problems we needed to repair:

- A number of backend calls: For every person question, the service contacted a number of downstream companies (airline fare suppliers, databases for ancillary data, and so forth.) sequentially. For instance, trying to find a flight known as three totally different APIs concurrently at instances, 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 current outcomes, each request on the web site began from scratch. Even equivalent searches had been repeated inside minutes. This meant even common routes or static knowledge (like airport particulars) had 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 cases), when site 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 big delay (typically 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 cases. 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. Beneath the hood, our structure merely wasn’t optimized for velocity. Queries had 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 attenuate cold-start and processing overhead. Right here is how the backend advanced:
Launched a Redis Caching Layer
We deployed a Redis cache to short-circuit costly operations on scorching paths. The thought was fairly easy: retailer the outcomes of frequent or current queries, and serve these straight for subsequent requests. For instance, when a person looked for flights from NYC to LON for sure dates, our API would fetch and compile the outcomes as soon as. It will then cache that “fare response” in Redis for a brief interval.
If one other person (or the identical person) made the identical search shortly after, the backend may 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 diminished latency for decent queries.
We utilized caching to different knowledge as nicely, like static or slow-changing reference knowledge. e.g. airport codes, metropolis metadata, forex alternate charges, now used cache. Moderately than hitting our database for airport data on every request, the service now retrieves it from Redis (populated at startup or on first use). This lower down a variety of minor lookups that had been including milliseconds right here and there (which add up below load).
Caching Musts
As a rule of thumb, we determined to “cache what’s scorching.” Fashionable routes, not too long ago fetched costs, and static reference knowledge like airport data had been all saved available in reminiscence. To maintain cached knowledge recent (necessary the place costs change), we set wise TTL (time-to-live) expirations and invalidation guidelines. As an illustration, fare search outcomes had been cached for a couple of minutes at most.
After that, they might expire, so new searches can get up-to-date costs. For extremely unstable knowledge, we may even proactively invalidate cache entries after we detected modifications. Because the Redis docs word, flight costs typically replace solely “each few hours.” So, a brief TTL mixed with event-based invalidation balances freshness vs. velocity.
The end result? On cache hits, the response time per question dropped from a number of seconds to some hundred milliseconds or much less. This was all due to Redis, which may serve knowledge blazingly quick over reminiscence. Actually, business reviews 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 prompt throughout the board, this caching layer delivered an enormous enhance. Important latency discount was achieved, particularly for repeat searches and common 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 due to this fact fine-tuned our Cloud Run service for low latency.
All the time-one heat occasion
We enabled minimal cases = 1 for the Cloud Run service. This assured that not less than one container is up and able to obtain requests even throughout idle durations. The primary person request now not incurs a chilly begin penalty. Google’s engineers word that preserving a minimal occasion can dramatically enhance efficiency for latency-sensitive apps by eliminating the zero-to-one startup delay.
In our case, setting min cases 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 big drop simply from this one optimisation alone.
Elevated concurrency per container
We revisited our concurrency setting. After guaranteeing our code may deal with parallel requests safely, we raised the Cloud Run concurrency from 1 to a better quantity. We experimented with values like 5, 10, and ultimately settled on 5 for our workload. This meant every container may deal with as much as 5 simultaneous searches earlier than a brand new occasion wanted to start out.
Consequence – fewer new cases spawned throughout site 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 directly, as a substitute of 10 chilly begins (with concurrency = 1), we’d deal with them with 2 heat cases dealing with 5 every, preserving issues snappy.
Quicker 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 function, which provides a burst of CPU to new cases throughout startup. We additionally used a slim base container picture and loaded solely important modules at startup.
Sure initialization steps (like loading massive config recordsdata or warming sure caches) had been additionally moved to the container startup section as a substitute of at request time. Because of min cases, this startup ran sometimes. In follow, by the point a request arrived, the occasion was already bootstrapped (database connections open, config loaded, and so forth.), so it may begin processing the question instantly.
We primarily paid the startup value as soon as and reused it throughout many requests, fairly than paying a little bit of that value on every request.
The outcomes had 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, right down to round 2 seconds. This was a mind-blowing 5 instances sooner loading expertise for our customers. Even the common latency improved (for cache-hitting queries, it was typically <500 ms).
Extra importantly, the responses grew to become constant and dependable. Customers now not skilled the painful and much-dreaded 10-second waits. The system may deal with site visitors spikes gracefully: Cloud Run scaled out to extra cases 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 diminished load on our downstream APIs and databases. This additionally not directly improved latency by stopping these techniques from turning into bottlenecked.
The online impact was a snappier, extra scalable search API that saved up with our prospects’ expectations of fast responses and a clean expertise.
Key Takeaways
From your entire 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 instances higher, sooner. An enormous win for person 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 ceaselessly requested knowledge dramatically cuts response instances by serving outcomes from reminiscence. The mix of in-memory velocity and considerate invalidation (utilizing TTLs and updates) can offload costly computations out of your backend.
- Optimize serverless for velocity: Cloud Run gave us simple scaling, however to make it really low-latency, we leveraged its notable options – preserving minimal cases heat. This eradicated cold-start lag, and tuned concurrency and assets so cases are used effectively with out getting overwhelmed. A little bit of upfront value (always-on occasion) might be nicely definitely worth the payoff in constant efficiency.
- Parallelize and streamline the place potential: We re-examined our request movement to take away pointless 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 important 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 impression. Efficiency tuning is seldom one-and-done. It’s about data-driven enhancements and typically inventive fixes to succeed in your latency objectives.
Conclusion
Whereas these latency discount methods could appear an excessive amount of to deal with directly, a scientific verify via each is kind of clean in follow. The one unparalleled plus for your entire train is that we turned our journey search API from a sluggish expertise into one which feels prompt. In a world the place customers anticipate 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 revel in expert-curated content material.