Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Upwork Freelancers vs Dedicated React.js Teams: What’s Better for Your Project in 2025?

      August 1, 2025

      Is Agile dead in the age of AI?

      August 1, 2025

      Top 15 Enterprise Use Cases That Justify Hiring Node.js Developers in 2025

      July 31, 2025

      The Core Model: Start FROM The Answer, Not WITH The Solution

      July 31, 2025

      Finally, a sleek gaming laptop I can take to the office (without sacrificing power)

      August 1, 2025

      These jobs face the highest risk of AI takeover, according to Microsoft

      August 1, 2025

      Apple’s tariff costs and iPhone sales are soaring – how long until device prices are too?

      August 1, 2025

      5 ways to successfully integrate AI agents into your workplace

      August 1, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Enhancing Laravel Queries with Reusable Scope Patterns

      August 1, 2025
      Recent

      Enhancing Laravel Queries with Reusable Scope Patterns

      August 1, 2025

      Everything We Know About Livewire 4

      August 1, 2025

      Everything We Know About Livewire 4

      August 1, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      YouTube wants to use AI to treat “teens as teens and adults as adults” — with the most age-appropriate experiences and protections

      August 1, 2025
      Recent

      YouTube wants to use AI to treat “teens as teens and adults as adults” — with the most age-appropriate experiences and protections

      August 1, 2025

      Sam Altman is afraid of OpenAI’s GPT-5 creation — “The Manhattan Project feels very fast, like there are no adults in the room”

      August 1, 2025

      9 new features that arrived on the Windows 11 Insider Program during the second half of July 2025

      August 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Request Hedging: Accelerate Your App by Racing Duplicate Calls

    Request Hedging: Accelerate Your App by Racing Duplicate Calls

    July 31, 2025

    Users notice slow requests; even if 99 % finish quickly, that 1 % “long‑tail” latency can make your app feel sluggish. Request hedging solves this by speculatively firing a second duplicate after a short delay, racing to beat out outliers before they ever impact the UI.


    Why the slowest 1 % of requests matter

    • The time it takes for the slowest 1 % of requests to finish is known as P99 latency. (P99.9 is the slowest 0.1 %, and so on.)
    • Users are sensitive to slowness. One long request is all it takes for an app to feel sluggish.
    • In an architectures where a page render hits 50 microservices, one bad service can drag the whole page down.

    long tail latency

    Google’s Bigtable team discovered that firing a second copy of a read after just 10 milliseconds cut their P99.9 latency by 96 % while adding only 2 % extra traffic. That’s cheaper than a single extra VM instance and far more predictable.


    What exactly is request hedging?

    Send the original request; if no response arrives within a small hedge delay, send a duplicate to another healthy replica. Return whichever finishes first and cancel the other.

    request hedging pattern

    Why it works:

    1. Outliers are random. Network hiccups don’t hit every server at once.
    2. Cheap insurance. Most requests finish fast, so the duplicate rarely runs long. You pay a small burst of extra load to avoid a big, visible stall.

    How to fit hedging into a Next.js + Sitecore Headless + . NET stack

    1. Next.js – browser or Vercel Edge

    // lightweight helper (TypeScript)
    export async function hedgedFetch(urls: string[], delayMs = 50) {
      const controller = new AbortController();
      const timer = setTimeout(() => {
        if (urls.length > 1) fetch(urls[1], { signal: controller.signal });
      }, delayMs);
    
      try {
        const winner = await Promise.any(
          urls.map(u => fetch(u, { signal: controller.signal }))
        );
        return winner;
      } finally {
        clearTimeout(timer);
        controller.abort();
      }
    }

    Example: Hedging a front‑end GraphQL fetch

    const response = await hedgedFetch([
      "https://edge-usw.example.com/graphql",
      "https://edge-use.example.com/graphql"
    ]);
    const json = await response.json();
    

    This code races two region endpoints, returns the fastest response, and cancels the slower request via AbortController. Adjust delayMs if your P95 latency is lower than the default 50 ms.

    2. Next.js API routes or App Router server actions

    Same pattern, but tune delayMs lower (20–30 ms) because the call is already inside the data‑center.

    3. Envoy / Istio sidecars

    An Envoy or Istio sidecar is a small proxy container that runs alongside your application container in the same Kubernetes pod. All inbound and outbound traffic passes through this proxy, so you can add behaviors such as retries, TLS, rate‑limiting, and request hedging by updating proxy settings instead of touching application code.

    If you skip sidecars in your Next.js application
    You can still hedge browser and server‑side calls by writing helpers (like hedgedFetch) or using Polly/gRPC policies. However, each service must implement and maintain its own logic, and any calls that come into your app from other services will not be hedged, leaving long‑tail spikes unprotected. Over time this scattered approach increases maintenance overhead and risks inconsistent latency behavior across the stack.

    route:
      per_filter_config:
        envoy.hedging:
          hedge_on_per_try_timeout: true
          initial_hedge_delay: 0.02s  # 20 ms
          max_requests: 2
    

    Put this in a VirtualService (Istio) or Route Configuration (raw Envoy) to hedge any calls that are safe to repeat without side effects (e.g., GET /product/123), otherwise known as idempotent.

    4. .NET back‑end callers

    What is gRPC? gRPC (short for Google Remote Procedure Call) is an open‑source framework that lets services invoke functions on other services as though they were local methods. It rides on HTTP/2 for efficient, multiplexed connections, uses Protocol Buffers for small binary messages, and generates type‑safe client and server code in many languages. Built‑in features like deadlines, retries, and hedging policies make it a natural place to enable request hedging without extra plumbing.

    • gRPC
      {
        "methodConfig": [{
          "name": [{ "service": "ProductCatalog" }],
          "hedgingPolicy": {
            "maxAttempts": 2,
            "hedgingDelay": "0.03s",
            "nonFatalStatusCodes": ["UNAVAILABLE", "DEADLINE_EXCEEDED"]
          }
        }]
      }
      

    HTTP

    builder.Services.AddHttpClient("edge")
        .AddStandardHedgingHandler(o => o.MaxAttempts = 2);
    

    5. Sitecore Experience Edge

    Experience Edge already runs in multiple regions. Expose two region‑specific GraphQL URLs to the client and let the hedged fetch pick the fastest.

    Roll‑out checklist

    1. Measure first. Capture your current P50, P95, P99, P99.9 latencies per hop.
    2. Pick a hedge delay ≈ P95. Too short wastes capacity, too long misses outliers.
    3. Restrict to idempotent reads. Avoid duplicate writes unless your API supports idempotency keys.
    4. Cap attempts to two. Start small; you rarely need more.
    5. Instrument and watch. Expose metrics like hedged_attempts, cancels, and tail percentiles. Aim for <5 % load overhead.

    Risks and how to mitigate them

    RiskMitigation
    Extra traffic / CPUMonitor overhead; two attempts at most keeps it predictable.
    Duplicate side effects on POST / PUTKeep hedging to GET / GraphQL query unless you have idempotency tokens.
    Window where both copies runCancel losers immediately with AbortController, gRPC cancellations, or Envoy resets.

    Key takeaway

    Request hedging is a tiny change that brings outsized rewards. A few lines of code (or a single header) can erase those embarrassing long‑tail spikes and make your Next.js + Sitecore + . NET experience feel nearly instantaneous.

    request hedging pattern

    Further reading:

    • Vector Search Embeddings and Retrieval-Augmented Generation
    • Postgres RAG Stack: Embedding, Chunking & Vector Search
    • Tool‑Augmented RAG Chatbot: GPT‑4, pgVector & Next.js

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleThe Intersection of Agile and Accessibility – Designing Accessible Agile Artifacts
    Next Article AI in Medical Device Software: From Concept to Compliance

    Related Posts

    Development

    Enhancing Laravel Queries with Reusable Scope Patterns

    August 1, 2025
    Development

    Everything We Know About Livewire 4

    August 1, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Best early Prime Day smartwatch and fitness tracker deals: My 10 favorite sales live now

    News & Updates

    CVE-2025-53157 – Apache HTTP Server Cross-Site Request Forgery

    Common Vulnerabilities and Exposures (CVEs)

    Swift Apprentice: Beyond the Basics [SUBSCRIBER]

    Learning Resources
    Agentic AI in the SOC – Dawn of Autonomous Alert Triage

    Agentic AI in the SOC – Dawn of Autonomous Alert Triage

    Development

    Highlights

    News & Updates

    Windows 11 is getting its own version of the Mac’s “Handoff” feature — resume apps across Android and PC!

    May 20, 2025

    Microsoft has revealed that it’s bringing “Cross Device Resume” to Windows 11, which will let…

    Onym – Flexible Filename Generator

    April 1, 2025

    CVE-2025-46653 – Formidable File Name Guessing Vulnerability

    April 26, 2025

    Rilasciata Archcraft 2025.04.24: la distribuzione GNU/Linux minimalista e moderna basata su Arch Linux

    April 26, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.