AWS Builder Center

Real time updates with AppSync subscriptions replaced a polling setup I was embarrassed to admit existed

A frontend that polled an API every five seconds for updates finally got replaced with AppSync GraphQL subscriptions, and the bill for unnecessary requests dropped along with it.

The frontend polled a REST endpoint every five seconds to check for updates to a shared document, which worked, technically, but meant every open tab was making a request whether or not anything had actually changed.


Moving to subscriptions


 AppSync  subscriptions let a client open a persistent connection and receive an update pushed the moment a mutation actually changes the underlying data, instead of asking repeatedly whether something changed. Wiring an existing mutation to notify subscribers required adding the subscription field to the schema and letting AppSync handle the WebSocket connection management entirely.


What changed for users


Updates that used to take up to five seconds to show up now arrive within a second of the actual change, and the constant background polling traffic disappeared completely, which was a bigger win for battery life on mobile than I expected going in.


The tradeoff


The one thing that took adjustment was reasoning about connection state, since a client can be disconnected and miss updates during that window, so I had to add a reconciliation query on reconnect to catch anything missed while offline. That extra piece of logic is a fair trade for removing a polling loop that was running constantly whether anyone needed it or not.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article