Pub/Sub and Kafka
Professional Data Engineer · Lesson 7 of 23 · 49:09
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Google Cloud Professional Data Engineer exam, on the official exam guide. In this lesson: how Pub/Sub topics and subscriptions work, at-least-once and exactly-once delivery, ordering keys, replay with seek, dead-letter topics, schemas, and Apache Kafka on Google Cloud. Includes three original practice questions, with Cymbal Mobility, a fictional ride-hailing and scooter company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Fifty thousand scooters
Fifty thousand scooters send their position every few seconds. The real-time map, the fraud team, and the data lake all need those events, each at its own pace. And the North American unit, bought last year, already runs Apache Kafka with its own tools.
How Pub/Sub works
Publishers send messages to a Pub/Sub topic. Each subscription receives its own copy of every message, so the map, the fraud team, and the lake each read at their own pace. With a pull subscription, the subscriber asks for messages, as Dataflow does, and with a push subscription, Pub/Sub sends each message to an HTTPS endpoint. Export subscriptions write messages straight into BigQuery or Cloud Storage, with no code at all. And there are no partitions to size: Pub/Sub scales automatically, and a schema in Avro or Protocol Buffer format can validate messages.
Delivery and ordering
By default, Pub/Sub delivers each message at least once. The subscriber must acknowledge each message before its deadline, or Pub/Sub delivers it again, so consumers must handle repeated messages. Exactly-once delivery can be turned on for pull subscriptions, within one region. Messages are not ordered by default, but messages that share an ordering key, like a scooter ID, and are published in the same region, arrive in order. Messages with different keys still flow in parallel, so the system keeps scaling.
Replay and errors
Mistakes happen, so plan for replay. A topic can keep messages, even acknowledged ones, for up to thirty-one days, and a subscription can seek back to a timestamp to replay them. A snapshot saves a subscription's acknowledgment state, for example just before new subscriber code is deployed, so you can seek back to it if that code misbehaves. Messages that keep failing go to a dead-letter topic after a set number of delivery attempts, so one bad message does not block the rest. Grant Pub/Sub the roles it needs on that topic, and attach a subscription to it, to examine the failures.
Apache Kafka on Google Cloud
Apache Kafka works differently. Its topics are split into partitions spread across brokers, order is kept within each partition, and consumer groups share the partitions between consumers. Managed Service for Apache Kafka runs open source Kafka clusters for you: you choose the vCPUs and memory, and it provisions brokers, rebalances partitions, and uses tiered storage. Clients connect privately through Private Service Connect, authenticate with IAM identities, and every connection is encrypted. Keep Kafka when teams rely on its clients and tools, and choose Pub/Sub for a serverless service with no partitions or clusters to manage.
Recap
Let's recap. A topic gives each subscription its own copy of every message, through pull, push, or export subscriptions into BigQuery or Cloud Storage. Delivery is at least once by default, exactly once is possible on pull subscriptions within a region, and ordering keys keep related messages in order. Topic retention and seek replay messages, snapshots protect deployments, and dead-letter topics isolate messages that keep failing. Kafka keeps order within partitions, and Managed Service for Apache Kafka runs its brokers for you. Choose Kafka for existing Kafka applications, and Pub/Sub for a serverless service with nothing to size.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Raw position events must land in a BigQuery table exactly as they are, and the team wants no code to maintain. What should the data engineer use?
- A push subscription to a service that inserts rows
- A BigQuery subscription on the topic
- A Dataflow pipeline written in Java
- A pull subscriber running on a VM
Show the answer
A BigQuery subscription on the topic
A BigQuery subscription writes each message straight into a BigQuery table, with no code to run. “A push subscription to a service that inserts rows” needs a service that someone must write and maintain. “A Dataflow pipeline written in Java” is useful when events need transforming, but here it only adds code. And “A pull subscriber running on a VM” also needs code, and a VM to manage.
Tomorrow, new subscriber code will be deployed, and it might acknowledge messages without processing them. The team wants a way to process those messages again. What should they prepare?
- A dead-letter topic with five delivery attempts
- A longer acknowledgment deadline
- Exactly-once delivery on the pull subscription
- A snapshot of the subscription before deploying
Show the answer
A snapshot of the subscription before deploying
A snapshot saves which messages were acknowledged, so the subscription can seek back to it, and receive those messages again. “A dead-letter topic with five delivery attempts” only catches messages that keep failing, not messages that were wrongly acknowledged. “A longer acknowledgment deadline” only gives subscribers more time before a message is delivered again. And “Exactly-once delivery on the pull subscription” prevents repeated delivery, but it cannot bring back an acknowledged message.
The North American unit runs many applications on Kafka clients and connectors, and wants to stop running its own brokers, without rewriting those applications. What should the data engineer propose?
- Managed Service for Apache Kafka
- Pub/Sub, after rewriting the clients
- Kafka on Compute Engine virtual machines
- Datastream with change data capture
Show the answer
Managed Service for Apache Kafka
Managed Service for Apache Kafka runs real Kafka clusters, so existing clients keep working, and Google runs the brokers. “Pub/Sub, after rewriting the clients” would mean rewriting every application. “Kafka on Compute Engine virtual machines” would still leave the team running brokers. And “Datastream with change data capture” only captures changes from databases, and offers no Kafka service.