AlgoViz

Lambda architecture

medium 27 min

Lambda architecture sends every event down two roads.

See it run

Two roads from the same events: an exact one that is slow and a quick one that is temporary, added together when somebody asks.

Operations

One day through the layers

Events go both ways. At night the batch job replaces what the speed layer guessed.

Step 1 of 5

Events go both ways

Our systemEventsMaster data800 eventsBatch jobasleepBatch viewemptySpeed jobcountingLive view798Servingidle

Each event is appended to the master data and is also counted by the speed job.

Step 1: Events go both ways

def on_event(e):    master.append(e)                 # batch layer, never changed    live.add(e)                      # speed layer, at oncedef nightly(cutoff):                 # for example 00:00    view = count(e for e in master if e.time < cutoff)    serving.swap(view, cutoff)    live.drop_before(cutoff)def query():    return serving.view + live.since(serving.cutoff)
Fresh after
Seconds
You run
Two pipelines

The live view is allowed to be wrong, because its life is short.

In the real world

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

A night without a batch run.

Step 1 of 4

Tuesday

Our systemEventsMaster data2 daysBatch jobasleepBatch viewMondaySpeed jobcountingLive viewTuesdayServingcomplete

The batch view covers Monday. The live view covers Tuesday.

Step 1: Tuesday

Common mistakes

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

  • Expiring by the job's end time

    critical

    Events that came while the job was running are in no view.

    Wrong
    live.drop_before(now())
    Right
    live.drop_before(view.covers_until)
  • Swapping in two steps

    critical

    Between the two steps a query counts a day twice or not at all.

    Wrong
    serving.put(view)live.drop_before(cutoff)
    Right
    serving.put(view, cutoff)  # one write# query: live.since(cutoff); drop later
  • Changing the master data

    major

    Nothing can be recomputed from data that was overwritten.

    Wrong
    master.update(event)
    Right
    master.append(correction)
  • Adding what cannot be added

    major

    Unique visitors of two parts are not a sum: one person can be in both.

    Wrong
    uniques_batch + uniques_live
    Right
    merge(sketch_batch, sketch_live)

Interview tips

  • How to explain it

    • Batch layer: all data, exact, slow.
    • Speed layer: new data, quick, temporary.
    • Serving layer: adds both.
  • Say the boundary

    • Batch covers everything before the cutoff.
    • Speed covers everything since.
    • The cutoff is stored with the batch view.
  • The price

    • Two codes for one meaning.
    • Two systems to run and to debug.
    • A merge in every query.
  • Today

    • Stream processors can be exact on their own.
    • One engine can run both modes.
    • Keep the idea: raw events that never change.