Sub topic 9
Controlling delivery with num_requested
This chapter assumes you can open a Subscribe stream and decode Avro payloads, as covered in the previous chapters. You should also know what a FetchRequest is and have a working Python gRPC stub.
Why the server does not just push everything
A Subscribe stream is bidirectional. The client sends FetchRequest messages, and the server sends FetchResponse messages. The server will not send an event unless the client has asked for it. The field that grants that permission is num_requested.
You can think of num_requested as credit. Each unit of credit allows the server to deliver one event. Every event delivered uses one unit. When the credit reaches zero, the server stops sending, even if more events are waiting on the topic.
This design is flow control. A slow subscriber cannot be flooded with events, because it controls how many events can be in flight.
How the credit accumulates
The first FetchRequest on a stream sets the topic, the replay preset, and the initial num_requested. Later requests on the same stream can carry just the topic name and a new num_requested. Credit from each request is added to what remains.
Replenishing credit in code
The subscriber from the previous chapter follows this pattern. After it processes a response, it asks for as many events as it just handled.
import queue
import pubsub_api_pb2 as pb2
BATCH_SIZE = 10
def subscribe(stub, metadata, topic_name, decode_event):
pending = queue.Queue()
pending.put(pb2.FetchRequest(
topic_name=topic_name,
replay_preset=pb2.ReplayPreset.LATEST,
num_requested=BATCH_SIZE,
))
def requests():
while True:
yield pending.get()
for response in stub.Subscribe(requests(), metadata=metadata):
for consumer_event in response.events:
print(decode_event(stub, metadata, consumer_event.event))
if response.events:
pending.put(pb2.FetchRequest(
topic_name=topic_name,
num_requested=len(response.events),
))The request generator blocks on pending.get(). This keeps the request side of the stream open without sending anything. The loop adds a new request only after the events have been processed. Because credit is returned after processing, the number of unprocessed events never exceeds the credit you granted.
Choosing a value
There is no universal best value. Consider these trade-offs:
| Choice | Effect |
|---|---|
| Small (1 to 10) | Low memory use and simple ordering. Many round trips. |
| Medium (tens) | Good default for most subscribers. |
| Large | Higher throughput, but more events are in flight if your process crashes. |
The Pub/Sub API limits how many events one request can ask for. Check the current documentation for the exact maximum.
Common mistakes
- Requesting once and never again. The stream stays open but goes quiet when the credit runs out. This looks like a hang.
- Replenishing before processing. The server keeps sending while your handler is still busy. Your backlog then grows without bound.
- Sending a replay preset on every request. Only the first request sets the starting position. Later requests should carry just the topic name and
num_requested. - Treating a response with no events as an error. The server may send a response with no events to keep the stream alive. Do not add credit for it.
len(response.events)is zero in that case, so the code above already skips it.
Exercise
Your subscriber receives 10 events, then stops receiving anything, although new events are being published to the topic. The stream has not raised an error. What is the most likely cause, and how do you fix it?
Show answer
The subscriber used up its credit. It requested 10 events and never sent another FetchRequest, so the server has nothing left to deliver against. Fix it by sending a new FetchRequest with the topic name and num_requested equal to the number of events just processed, after processing each response.