Make the right trade-off / CHAPTER 37
Push versus pull architecture
Choose who initiates delivery based on freshness, consumer control, fanout, and recovery.
Builds on Functional and non-functional requirements, Polling, long polling, WebSockets, and server-sent events.
- Calculate polling cost and freshness delay.
- Explain backpressure and disconnected consumers.
- Design a hybrid hint-and-fetch flow.
01Who asks for the next item?
A shopper keeps an order-status screen open while the order changes from packed to shipped. Who starts the next exchange?
- Pull: the screen asks the order API for current status. It controls when to ask again and how much to fetch.
- Push: a delivery service contacts the registered screen or device when a change occurs. It needs a reachable channel or a way to save delivery work until the device returns.
One application can use both. The server may push “this order changed,” and the screen may then pull its saved current details. A worker may pull jobs from a queue even though another service pushes events into that queue. State the connection being discussed instead of labeling the entire system once.
Push can reduce the time before a screen learns of a change. Pull lets a slow or intermittent receiver control intake. Neither choice alone saves history or proves that the receiving application applied an update.
02Freshness and load come from the contract
Compare a screen's next update:
| Question | Periodic pull | Push |
|---|---|---|
| Who starts the exchange? | The screen requests status. | The delivery service sends toward a registered destination. |
| What if nothing changes? | The API still handles empty or unchanged reads. | Messages may be avoided, but connections or delivery records remain. |
| What if the receiver is slow? | It can ask less often or request smaller batches. | Limit pending messages; combine replaceable updates or disconnect and recover. |
| What if it was offline? | Fetch current state or retained changes on return. | Saved state or replay is still needed; a missed push is not automatically recovered. |
For polling, derive both cost and delay
- Count active screens
Nand intervalTseconds. Evenly spread polling creates aboutN/Trequests/s. - State the timing assumption. If changes occur uniformly between polls, the average wait to the next poll is about
T/2; interval-only worst waiting approachesT. - Add actual failures and processing. Those arithmetic waits are not hard network-inclusive freshness guarantees.
- Vary polling times. Small timing variation, called jitter, prevents all devices asking at the same instant. It spreads work but does not reduce the total average requests.
For pushed updates, count recipients too. Sending one change to 100,000 subscribers can create 100,000 delivery attempts before retries. This multiplication is fanout.
03Worked example: order status
Assume 60,000 order screens poll every 30 seconds. They create 2,000 requests/s even if no orders change, with about 15 seconds mean interval wait under the uniform-timing assumption. Polling every two seconds raises the rate to 30,000/s.
Now follow a hybrid update. Order 42 is the saved order these screens display; its status has an increasing version number.
- The order service saves
shippedas version 8. If the product promises notification attempts, save pending notification work with the change so a crash cannot erase it. - The delivery service sends a small hint: order
42now has version 8. This hint is not the only saved copy of the status. - A screen holding version 7 calls the authenticated order API. The screen includes version 8 from the hint as the minimum it needs. The API verifies that the caller may view this order, then returns version 8 or later. If its selected copy still has 7, it waits, chooses an updated source or returns a failure instead of presenting 7 as the requested update.
- A repeated hint does not replace newer state. After applying version 8, the screen ignores a delayed version 7 hint and need not apply version 8 twice.
- A sleeping device wakes. It fetches current status, or requests retained events after its saved position if every transition matters. Correctness does not require it to have received every live hint.
- An active device occasionally checks its version anyway. This detects a missed signal even when the connection appeared healthy.
For a status display, keeping only the latest pending status may be enough. Do not apply that shortcut to payments or audit events where every transition triggers a separately required action. Also distinguish network receipt from the application saving or displaying the update; use an application acknowledgment when that milestone matters.
Push versus pull architecture — decision flow
Follow the numbered arrows
- Saved order 42 → Change notificationsannounce the committed newer version
- Change notifications → Order-status screensend a small order-changed hint
- Order-status screen → Order read APIrequest current order details
- Order read API → Saved order 42read the permitted saved order
Trace the diagram in words
- Save order 42 as shipped at version 8 before announcing the change.
- A client holding version 7 receives a hint and asks the order API for version 8 or later; that API checks the viewer’s permission.
- After sleeping or missing a hint, the client fetches saved state or retained history instead of assuming live delivery was complete.
04Choose a delivery and recovery pair
Choose delivery together with recovery:
- Use pull when receivers run intermittently, need intake control or tolerate the calculated polling delay.
- Use push when sparse changes need prompt notice and its connection/delivery cost is justified.
- Use hints plus fetches when quick notice is useful but current saved state can repair missed notifications.
- Limit each receiver's buffer. One slow subscriber must not consume unlimited shared memory. Explain whether updates combine, wait, reject or resume from storage.
- Define acknowledgment and history retention. Reaching a device's network connection does not prove its application applied the update.
In an interview, name the sender, receiver, exact data and recovery source. Calculate active consumers, polling or update rate, recipients and payload. Then disconnect one receiver and slow another. Those traces determine the choice more accurately than calling push “real time” or pull “inefficient.”
DRAW. CALCULATE. EXPLAIN.
Choose weather delivery
20 minutes · guided practice
100,000 awake devices need fresh weather within roughly one minute; many later sleep for hours. Estimate 60 s polling, choose a delivery/recovery pair, and handle a version 10 update followed by a delayed version 9 hint.
Compare the worked solution
- Evenly spread 60-second polling by 100,000 devices averages about 1,667 requests/s. Network and processing time are additional.
- Push small change hints to active devices if faster notice is useful. On wake, fetch the current authorized forecast instead of replaying hours of obsolete hints.
- A device already holding forecast version 10 keeps it when a delayed version 9 hint arrives.
- If only current weather matters, replace obsolete pending forecasts with the newest one. Keep pending storage finite.
- Periodic version checks discover missed hints. If the device cannot contact the service, label its saved forecast with the last update time and show that updates are unavailable; do not present it as a newly verified one-minute-old forecast.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01Who initiates push and pull?
- Pull: the receiver asks for data or work.
- Push: a delivery service sends toward the receiver.
- Name the specific connection; one application can push hints and then pull current records.
CARD 02How is polling cost estimated?
- Divide N active clients by their T-second interval for mean requests/s.
- Uniformly timed changes wait about T/2 on average.
- Add network, processing and failure delays separately.
CARD 03Why combine versioned hints and fetches?
- A small hint provides quick notice.
- Fetching saved state repairs missed notifications.
- Versions stop old hints replacing newer state; retained history positions support replay when every event matters.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.