Sweaty Imposter

Futures / async-await

TechniqueConcurrencyMidSeniorUpdated
Represent an in-flight computation as a value you can compose, chain, and await, no manual callback threading.

When to Use Futures / async-await

I/O-bound services with many in-flight calls (fan-out to downstreams, parallel DB + cache reads). Lets one OS thread drive thousands of logical operations.

In Practice

Java's CompletableFuture (backed by ForkJoinPool), Python's asyncio + await, JavaScript's Promises, Rust's tokio futures. Netflix's Zuul 2 and gRPC-Java both fan out via CompletableFuture.

Sketch

Java

CompletableFuture<User>    u = async(() -> userSvc.get(id));
CompletableFuture<Profile> p = async(() -> profileSvc.get(id));

CompletableFuture<Response> resp = u.thenCombine(p, Response::new)
    .orTimeout(200, TimeUnit.MILLISECONDS)
    .exceptionally(ex -> Response.fallback());

How Futures / async-await Comes Up in Interviews

Scenario

Rendering a profile page requires three independent downstream calls: user, prefs, friends. Sequentially it's 180ms; you want it to take roughly max(user, prefs, friends). One OS thread should be able to drive all three in flight. Which technique?

CompletableFuture (or asyncio.gather) fans out the three calls concurrently and composes the results. A thread pool alone could do it but with one thread blocked per call; futures/async let one event-loop thread own all three.