Async and long-running jobs over gRPC
A server-owned Operation lets an RPC return a handle to a job that outlives the call, with polling, streaming progress, cancellation and a rerunnable reconcile job.
Learning objectives
By the end of this chapter you can:
- Model a server-owned
Operation(id,kind,done,progress_pct,message) as a handle to work that outlives the RPC that started it. - Put
GetOperation,WatchOperationandCancelOperationon a dedicatedOperationsService, and say why quarkus-grpc forces the split. - Choose between client-paced polling and server-paced streaming for a long job.
- Tell server-owned cancellation from request-scoped cancellation, and guard it cooperatively.
- Build a rerunnable reconcile RPC and say how rerunnable differs from idempotent.
Why this matters
RecomputeRoutes reruns a routing optimization across every lane and carrier in the catalog. On a real fleet that takes minutes, which is too long for a caller to hold a thread, a connection and a deadline. Raising the deadline doesnât fix it: the caller still canât check progress, change its mind, or walk away and come back. The RPC has to return something other than the result.
Pattern: Long-Running Operation
The problem
A unary RPCâs response is the answer. That holds while the work fits inside a deadline a caller will wait on, and breaks when the job runs for minutes. The client then needs something to check back with, a way to stop the job, and a job that survives its own request.
The solution
A long-running operation is a server-owned record: the starting RPC returns it immediately, and separate RPCs read, watch or cancel it by id.
Start, check and stop are three different questions with different cardinalities: one RecomputeRoutes per job, but as many GetOperation or WatchOperation calls as the client wants before the job finishes. This is the shape of Googleâs AIP-151 and the google.longrunning.Operations service.
How Quarkus and Python model it
shipping.type.v1.Operation is small:
| Field | Meaning |
|---|---|
id |
Opaque, server-generated (op_ prefix). The only thing a client keeps between calls. |
kind |
What the job is (recompute_routes). Useful once several kinds of job share one lookup surface. |
done |
The one boolean that says whether to keep polling or watching. |
progress_pct |
Best effort, 0 to 100. It need not advance linearly, but it ends at exactly 100 when done becomes true. |
message |
A status line; after a cancellation, why the job stopped short. |
A client canât set progress_pct or flip done. Those change only inside the process that owns the job, driven by the job or by a CancelOperation the server itself processes. Both stacks keep an in-memory OperationRegistry of OperationState records keyed by id. A real service backs it with a durable store so operations survive a restart; the create, poll, watch and cancel contract is the same either way.
Each stack builds one registry and shares it between two service implementations. On Quarkus, CDI injects the same @ApplicationScoped singleton into RoutingServiceImpl and OperationsServiceImpl. On Python, app/main.py passes one OperationRegistry to both servicers before registering them on the same grpc.aio server.
RecomputeRoutes creates the record and returns the done=false snapshot without waiting for the job:
- Quarkus: the handler submits the work to
Infrastructure.getDefaultExecutor(), the worker pool@Blockingmethods use. - Python: the handler calls
asyncio.create_task(...). It keeps a strong reference to the task in an instance-levelsetand discards it in a done-callback, becauseasyncioholds only a weak reference to a scheduled task and an unreferenced one can be garbage-collected mid-job.
In both cases the thread or event-loop turn that handled the RPC is never the one doing the job, so a slow recompute canât starve other calls. The job runs a bounded loop: sleep, check whether the record has gone terminal, otherwise advance progress_pct and message. It takes five 200 millisecond steps for an ordinary reason and twenty when reason contains slow, a window long enough for a real cancellation to land mid-flight.
A dedicated OperationsService
The obvious draft puts the three lookup RPCs next to RecomputeRoutes on RoutingService. That change is wire-safe: buf breaking has nothing to report, because a new RPC on an existing service never breaks a client built against the old .proto. It is source-breaking for every Quarkus implementor. quarkus-grpc turns each service block into a fully abstract Java interface with one method per RPC, so three new RPCs stop every existing RoutingServiceImpl from compiling. A checker that watches only the wire canât see this; you have to know how the framework generates code.
The fix is a separate OperationsService with exactly three methods, declared in the same routing.proto. RoutingService keeps the five RPCs it had, and every existing implementor compiles unchanged. A new bean (OperationsServiceImpl on Quarkus, a second servicer on Python) implements the new service and shares the registry.
This is also what AIP-151 prescribes: operation lookups live on a service decoupled from the one that created the job, so adding a job kind to any resource-owning service never touches that serviceâs RPC surface. Here it is a narrower version, with one job kind and one creating service.
Returning Operation directly
Every other RPC in the contract takes a dedicated FooRequest and returns a dedicated FooResponse, so an envelope can grow a field without changing a signature. The three operation RPCs return shipping.type.v1.Operation directly, as AIP-151 does. The operation is the response, and a single-field wrapper would add nesting with no flexibility. Two scoped lint exceptions sit above each declaration:
// buf:lint:ignore RPC_REQUEST_RESPONSE_UNIQUE
// buf:lint:ignore RPC_RESPONSE_STANDARD_NAME
rpc GetOperation(GetOperationRequest) returns (shipping.type.v1.Operation);
RPC_RESPONSE_STANDARD_NAME flags a response not named <RpcName>Response. RPC_REQUEST_RESPONSE_UNIQUE flags several RPCs sharing one response type, which is usually a copy-paste mistake and here is the point: all three answer âwhat does this operation look like right now.â Each buf:lint:ignore suppresses one named rule on one RPC, so a later reader sees which rules were judged inapplicable.
A lint exception is not a loophole
Keep the reasoning next to the exception, as the comment above does. Treat a new buf:lint:ignore anywhere else in the contract as a question to ask before it merges.
Polling and watching
GetOperation takes an operation_id and returns the current snapshot. It is a plain idempotent read. Two identical calls may return identical responses, because the record may not have changed. An unknown or evicted id returns NOT_FOUND, with nothing operation-specific layered on the generic code.
WatchOperation flips the pacing: the client asks once, and the server pushes a snapshot down one HTTP/2 stream on each meaningful update, then closes the stream when the operation is terminal. It is ordinary server streaming, the same shape as WatchShipment, applied to a record a background job mutates.
A REST client polls GET /operations/{id} at an interval it picks. REST has no direct equivalent of WatchOperation; it needs Server-Sent Events or a WebSocket, a second protocol on top of the first.
Both stacks implement the watch by reading the record the job mutates, so no notification channel connects job and watcher and a snapshot is never staler than one poll interval:
- Quarkus: a
MultifromMulti.createFrom().emitter(...)loops on the worker pool, emits a snapshot, sleeps briefly, and completes once a snapshot isdone. - Python: an async generator does the same with
yieldandasyncio.sleep; returning from the loop half-closes the stream.
A minutes-long job could push far more snapshots than a consumer wants. The demo keeps the interval short; a production watch emits only when progress_pct or message changed, not on a timer.
| Â | GetOperation |
WatchOperation |
|---|---|---|
| Pacing | Client decides how often to ask | Server pushes each meaningful update |
| Cost | One round trip per look | One open stream for the jobâs duration |
| Use it when | A UI refresh button, or a script checking back every few seconds | The client wants every update without wasted round trips |
Cancelling the operation, not just the watch
synchronized boolean cancel() {
if (done) {
return false;
}
this.done = true;
this.message = "operation cancelled by client request at " + progressPct + "% progress";
return true;
}
def cancel(self) -> bool:
"""Called by CancelOperation. Returns True if this call is the one
that moved the operation to terminal; False if it was already done
(the cancel arrived too late, or twice)."""
if self.done:
return False
self.done = True
self.message = f"operation cancelled by client request at {self.progress_pct}% progress"
return True
CancelOperation moves the record to a terminal state: done becomes true, message names the cancellation, and progress_pct stays where it was, below 100, because the job stopped short. The job checks done between steps and stops advancing. It never races the cancel for the final state, because cancel() and the jobâs own complete() go through the same guarded transition and the first one wins. A late or duplicate cancel is a no-op.
This is cooperative cancellation. The record reflects the cancel immediately, but the jobâs loop has to notice and return. A job stuck in one long blocking call between checks keeps running under a done=true record until that call yields.
Closing a WatchOperation stream, letting its deadline expire or simply not reading changes nothing about the job. It keeps advancing toward done=true whether or not anyone watches. That differs from a downstream call under a deadline: cancelling an inbound GetQuote stops the QuoteService.GetQuote work beneath it, because that workâs lifetime was the callâs. The jobâs lifetime is the operationâs own, ended only by completion or CancelOperation.
| Action | Effect on the job |
|---|---|
CancelOperation |
Stops it |
Close or abandon WatchOperation |
None; one client stops watching |
RecomputeRoutes as a rerunnable reconcile job
RecomputeRoutes takes a reason and nothing else: no partial state to merge, no prior run to resume. A second call doesnât continue the first. It starts a new operation with a new id that recomputes routing state from scratch and reaches the same kind of terminal result, the way a reconcile loop re-derives desired state from current inputs instead of replaying history.
That is rerunnable, which isnât idempotency in the single-RPC sense. A GetQuote retry is idempotent because the same request yields the same response with no accumulating side effect. Each RecomputeRoutes call creates a new Operation, which is a side effect. What makes it safe to repeat is that the job is pure with respect to its inputs: it can run on a schedule, or after a suspected inconsistency, without anyone reasoning about what an interrupted earlier run left behind.
Decision
Return an Operation instead of a result once a job outlasts any deadline a caller should hold. Use GetOperation when the client wants a point-in-time answer on its own schedule and a long-lived stream isn't worth the plumbing; use WatchOperation when it wants every meaningful update and can hold one connection open for the job's duration. Only CancelOperation stops the job; closing a watch stream stops one client watching. Put the lookup RPCs on a dedicated OperationsService: three RPC declarations and one extra bean or servicer buy a lookup surface that can grow, or absorb a second job kind, without touching RoutingService's generated interface.
đ€ Explore with your agent
âIn examples/16-async-long-running-jobs, read OperationsService in routing.proto and OperationState in the Quarkus and Python stacks. Check that CancelOperation leaves progress_pct below 100 and that a second cancel on the same op id is a no-op, and that WatchOperation closes its stream once done is true. Fix any place where the job could overwrite a cancelled record.â
Check: run
./verify.shin examples/16-async-long-running-jobs, then callOperationsService/CancelOperationtwice with grpcurl and confirm both replies are identical.
Build, run, observe
cd examples/16-async-long-running-jobs && ./verify.sh
verify.sh builds and starts both stacks. Quarkus listens on 9160 and Python on 9161. It passes when RecomputeRoutes returns a non-terminal Operation, GetOperation converges to done=true at progress_pct=100, WatchOperation streams at least two snapshots ending in that state, CancelOperation stops the slow variant below 100, and two RecomputeRoutes calls each finish with their own id.
Start a job and read back its non-terminal Operation:
grpcurl -plaintext -d '{"reason":"reconcile"}' \
localhost:9160 shipping.routing.v1.RoutingService/RecomputeRoutes
{
"operation": {
"id": "op_7f3a9c1e4b2d4a0f9b3c",
"kind": "recompute_routes",
"message": "started"
}
}
done and progress_pct are absent because proto3âs JSON mapping omits a field at its zero value. Absence is the signal that the job just started. Poll with the returned id:
grpcurl -plaintext -d '{"operationId":"op_7f3a9c1e4b2d4a0f9b3c"}' \
localhost:9160 shipping.routing.v1.OperationsService/GetOperation
Watch it instead, and grpcurl prints one object per snapshot until the half-close:
grpcurl -plaintext -d '{"operationId":"op_7f3a9c1e4b2d4a0f9b3c"}' \
localhost:9160 shipping.routing.v1.OperationsService/WatchOperation
{"id":"op_7f3a9c1e4b2d4a0f9b3c","kind":"recompute_routes","progress_pct":20,"message":"recomputing routes (20%)"}
{"id":"op_7f3a9c1e4b2d4a0f9b3c","kind":"recompute_routes","progress_pct":60,"message":"recomputing routes (60%)"}
{"id":"op_7f3a9c1e4b2d4a0f9b3c","kind":"recompute_routes","done":true,"progress_pct":100,"message":"recompute complete: routes reconciled"}
Start {"reason":"slow reroute"} and cancel it with OperationsService/CancelOperation. The reply has done: true, progress_pct below 100 (25 in one run) and the message operation cancelled by client request at 25% progress. Swap 9160 for 9161 to run every call against the Python stack.
Experiments:
- Send
CancelOperationtwice for the same id. The second call returns the same terminal record unchanged. - Cancel after the job has finished. The record keeps
progress_pct100 and its completion message. - Call
GetOperationwith an id that never existed. The status isNOT_FOUND.
For deeper coverage
- AIP-151: Long-running operations: the
google.longrunning.Operationsshape thatOperationsServicenarrows to one creating service and one job kind. - Cloud Native Design Patterns, âWorkflows and Jobsâ chapter: background-job and reconcile-loop patterns beyond a single
RecomputeRoutes. - grpc.io: Core concepts: server streaming: the RPC shape
WatchOperationreuses fromWatchShipment. - Chris Richardson, Microservices Patterns, Manning, 2018: asynchronous messaging and saga coordination for work that outlives a single request.