Events and pub/sub

Play this lesson

Everyone wants to know

When four friends clear a wave, four parts of Shard Garden care: leaderboards, achievements, analytics and the season pass.

If the match server calls each one directly, it has to know all of them, wait for all of them, and change every time a new one is added.

Publish and subscribe

An event is a message that something happened: wave_cleared. The match server publishes it to a topic. Each interested part has its own subscription and gets its own copy.

Google Cloud's Pub/Sub docs: "Publishers send events to the Pub/Sub service, without regard to how or when these events are to be processed."

Quick check

What is the main difference between a work queue and pub/sub?

  1. In pub/sub every subscription gets its own copy of each event; a work queue gives each message to one worker
  2. Pub/sub is always faster
  3. A queue cannot store messages
Show the answer

In pub/sub every subscription gets its own copy of each event; a work queue gives each message to one worker

A work queue shares jobs out. Pub/sub fans one event out to every subscriber.

Loose coupling

Kai wants a new "most helpful gardener" award, which needs to hear about cleared waves. With pub/sub, Remy adds a subscription. The match server does not change at all.

Google calls this asynchronous integration, which "increases the flexibility and robustness of the overall system". Parts can be added, or go down for a while, without stopping the game.

Name events as facts

An event says what happened, in the past tense: wave_cleared, not give_rewards. A command tells one listener what to do. A fact lets every listener decide for itself.

Each event carries an ID, a time and the details: garden 7, wave 3.

Quick check

When Remy adds a new subscriber, the match server that publishes the event must change too.

Show the answer

False

The publisher does not know who listens. New subscribers join the topic without touching it.

Pick the event

Which is the best event for the match server to publish?

  1. wave_cleared, with the garden ID, the wave number and the time
  2. send_achievement_email
  3. update_leaderboard_now
Show the answer

wave_cleared, with the garden ID, the wave number and the time

wave_cleared states a fact any part of the game can react to. The other two are commands aimed at one listener.

Reading the same thing again and again

Events handle news. But most traffic is the same few pages, read over and over.

Bolt says: Next: caching, the hardest easy thing.

Fact-checked 12 October 2026