Tech Interview RoomStudy guides

GCP

How does Pub/Sub work, and how does it compare with Kafka?

Intermediate · 2 min read · Lesson 2 of 6 in GCP

Model

  • Publishers send messages to a topic; each subscription receives a copy of every message (fan-out across subscriptions); multiple subscribers on the same subscription share the load.
  • Pull subscriptions (the client pulls or uses streaming pull; best for throughput and back-pressure) or push subscriptions (Pub/Sub POSTs to an HTTPS endpoint, such as Cloud Run).
  • Fully managed and global: no partitions or brokers to size; scales automatically.

Delivery semantics

  • At least once by default: a message must be acknowledged within the ack deadline (10–600 s, extended automatically by client libraries) or it is redelivered.
  • Ordering keys: messages with the same key are delivered in order (enable message ordering on the subscription; publish in the same region).
  • Exactly-once delivery option (pull subscriptions): no redelivery once an ack succeeds, within a region.
  • Dead-letter topics after a maximum number of delivery attempts; retry policy with exponential backoff.
  • Retention up to 31 days on topics and seek to a timestamp or snapshot to replay.
  • Filters on subscriptions, schemas (Avro/Protobuf), BigQuery and Cloud Storage subscriptions for direct export.

Pub/Sub vs Kafka

  • Pub/Sub: zero operations, per-message acknowledgement, automatic scaling, global; less control over partitioning and ordering at very high throughput; ecosystem tied to GCP.
  • Kafka (self-managed, Confluent Cloud or Google Managed Service for Apache Kafka): log semantics with consumer-controlled offsets, strong per-partition ordering, Kafka Streams and Connect ecosystem, long retention, portability; you size partitions and clusters.

Follow-up questions interviewers ask

Try answering each one before you open it.

How do you process Pub/Sub messages idempotently in Spring Boot?

Spring Cloud GCP's PubSubTemplate or Spring Cloud Stream binder gives you the message with its messageId (and your own business id attribute). Record processed ids in a table with a unique constraint inside the same transaction as the side effect, or make the operation an upsert, and ack only after the transaction commits. Configure a dead-letter topic so poison messages stop being retried forever.