AlgoViz

Kappa architecture

medium 27 min

Kappa architecture uses one stream job and one log that keeps its events.

See it run

One log, one stream job. When the logic changes, a second job reads the same log from the start into a new table.

Operations

The normal day

Events go into the log, the job reads them in order and updates a table.

Step 1 of 4

Four events in the log

Our systemEventsKafka log4 eventsJob version 1reads E4Table 13 countedAppreads table 1

log

E1 · E2 · E3 · E4

E1 to E4 are appended. The job has handled three and is reading the fourth.

Step 1: Four events in the log

log.append(event)                  # the source of truthfor offset, e in log.read(after=saved):    table[e.kind] += 1             # one logic    saved = offset                 # stored with the table
Takes
Seconds
Needs
Log and one table

The table is only a copy. The log can build it again.

In the real world

The same kappa architecture doing real jobs. Pick one and step through it.

A bug from 12 days ago, and a topic that keeps 7.

Step 1 of 4

A new topic

Our systemEventsKafka log7 days keptJob version 1liveTable 190 daysAppreads table 1

The team left the retention at Kafka's default: 7 days.

Step 1: A new topic

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Clearing the table before the replay

    critical

    Users see zeros for hours, and the live job writes into the same table.

    Wrong
    table.clear()replay(into=table)
    Right
    replay(into=new_table)   # then switch
  • Reusing the consumer group

    critical

    The setting auto.offset.reset is used only when the group has no valid position. With an old position nothing is replayed.

    Wrong
    group="main"; reset="earliest"
    Right
    group="main-v2"; reset="earliest"
  • Sending emails again

    critical

    A replay repeats every side effect of the job.

    Wrong
    on_order(e): send_email(e)
    Right
    on_order(e): emit(e)# one separate sender, ids remembered
  • A random key

    major

    With the event id as key, events of one user land in different partitions and lose their order.

    Wrong
    append(key=event.id)
    Right
    append(key=event.user)

Interview tips

  • How to explain it

    • One log, one stream job, views built from it.
    • Reprocess means: read again from the start.
    • No batch layer.
  • Five steps of a replay

    • Deploy version 2.
    • Start from the first offset, new group.
    • Rebuild into a new table.
    • Catch up and compare.
    • Switch, then remove the old one.
  • K-A-P-P-A

    • Kafka, or any log.
    • Append only.
    • Processing as a stream.
    • Playback to reprocess.
    • Architecture with one road.
  • Be careful with

    • Retention shorter than your bugs.
    • Replay time.
    • Side effects.
    • Calls to systems that changed since.