Lambda architecture
medium 27 minLambda 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
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
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
criticalEvents that came while the job was running are in no view.
Wronglive.drop_before(now())Rightlive.drop_before(view.covers_until)Swapping in two steps
criticalBetween the two steps a query counts a day twice or not at all.
Wrongserving.put(view)live.drop_before(cutoff)Rightserving.put(view, cutoff) # one write# query: live.since(cutoff); drop laterChanging the master data
majorNothing can be recomputed from data that was overwritten.
Wrongmaster.update(event)Rightmaster.append(correction)Adding what cannot be added
majorUnique visitors of two parts are not a sum: one person can be in both.
Wronguniques_batch + uniques_liveRightmerge(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.