Publicación–Suscripción y Streams de Eventos
Topics · Log estilo Kafka
Escrito por Darjus Kazakevic · revisado el
Publish once; every subscriber group gets its own copy and its own position.
In publish–subscribe, a producer sends a message to a topic without knowing who reads it. Every subscribing group gets its own copy. When an order is placed, billing, search and email each react, and the order service never calls them. Simple pub/sub, such as SNS, Google Pub/Sub or Redis pub/sub, pushes each message to the current subscribers.
An event stream, such as Kafka, Redpanda or Kinesis, goes further. The topic is a log kept on disk for days. It is split into partitions, and each group tracks its own offset, so a new service can replay history. The costs: order holds only within a partition, and there is a cluster to run.
En esencia — Publish each event once to a topic; every consumer group reads all of it at its own pace, and inside a group the partitions are shared out.
Cuándo recurrir a él
Recurre a él cuando
- Several services must each react to the same event
- A new consumer must be able to replay past events from the start
- High event volume that must stay in order per customer or entity
Reconsidéralo cuando
- One job per message for a pool of workers: use a queue
- The producer needs a reply: make a direct API call
- A few events per minute and one consumer: a queue is simpler
Conceptos clave
- Topic and fan-out
- A topic is a named stream of messages, such as order-events. Each subscriber, or each consumer group, receives every message. Adding a new subscriber needs no change to the producer.
- Partitions and keys
- A topic is split into partitions, each an ordered log. The message key, such as the order ID, picks the partition, so all events for one key stay in order.
- Consumer groups
- A set of consumers that share the work of reading a topic. Kafka gives each partition to exactly one consumer in the group. Different groups are independent, so billing and search each read every event.
- Offsets
- A consumer's position in a partition: the number of the next message to read. The group commits offsets as it goes. After a crash it restarts from the last commit, so some messages are read twice.
- Retention and replay
- A log keeps messages for a set time or size, whether or not anyone read them. A new consumer can start from the beginning, and after a bug fix you can replay old events.
Reglas de diseño
- Key by the entity that needs order
- Use the order ID or user ID as the message key. All its events then land in one partition, in order. Without a key, one order's events can be handled out of order.
- Choose the partition count up front
- Partitions cap a group's parallelism. Adding them later moves keys to new partitions and breaks per-key order. Start with room to grow, such as 2–3× today's consumers.
- Commit offsets after processing
- Commit only when the work is done, so a crash replays a few messages instead of losing them. Make consumers idempotent, because replays and rebalances cause duplicates.
- Watch consumer lag per group
- Lag is how far a group is behind the newest message. Alert when it keeps growing: that group is slower than the producers, even if it logs no errors.
Errores habituales
- No message key. One order's events land in different partitions and are handled out of order, for example “shipped” before “paid”.
- More consumers than partitions. With 6 partitions, consumers 7 to 10 in the group sit idle.
- Redis pub/sub for events that must not be lost. It stores nothing, so a subscriber that restarts misses everything sent meanwhile.
- Handlers slower than the consumer's time limit, such as max.poll.interval.ms in the Java client. The group thinks the consumer died, rebalances, and the same messages are handled again in a loop.