> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Replaying ten years as if they were happening live

> docs/architecture/04-kafka-replay.mmd

```mermaid theme={null}
%% Calendar Effects · 4 of 8 · Replaying ten years as if they were happening live.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart LR
  master[("clean/master.parquet<br/>10 years, 98,046 rows")]

  subgraph KAFKA["The real way · Kafka · Sanchi"]
    create["scripts/create_topics.py<br/>makes the topic once<br/>1 partition = one ordered line of messages"]
    producer["src/producers/replay_master.py · the PRODUCER<br/>sorts rows by date, then ticker<br/>sends each row as one JSON message, key = ticker<br/>waits --seconds-per-day between trading days, 0.1 s = about 4 min"]
    late["--late-fraction 0.02 --late-days 3<br/>holds back about 2% of messages<br/>and sends them 3 trading days late<br/>to test how the job handles late data"]
    topic[["Kafka TOPIC calendar-effects-master<br/>on the course cluster node1.local:9092, node2.local:9092<br/>a durable queue: messages stay until expired"]]
    peek["scripts/peek_topic.py<br/>prints the first few messages, to check"]
  end

  subgraph FILES["The laptop way · no Kafka needed"]
    tofiles["src/producers/replay_to_files.py<br/>same messages and the same late option,<br/>written as JSON-lines files instead"]
    folder[/"data/replay/00000.json, 00001.json ...<br/>one file per 5 trading days"/]
  end

  msg["ONE MESSAGE looks like<br/>ticker WMT, date 2024-11-29, adj_close 92.1,<br/>volume 12000000, ret 0.012, vol_ratio 1.4,<br/>views 6500.0, event_type black_friday,<br/>country US, retailer_type store<br/>empty values are sent as null"]

  job["src/streaming/job.py<br/>--source kafka reads the topic<br/>--source files reads the folder, one file per micro-batch"]

  master --> producer
  create --> topic
  late -.-> producer
  producer --> topic
  topic --> peek
  topic --> job
  master --> tofiles --> folder --> job
  producer -.-> msg
```

## Guide

```mermaid theme={null}
%% Calendar Effects · Guide 4 · Replaying ten years as if they were live · goes next to diagram 04-kafka-replay.mmd
%% Miro: paste onto a board, or Creation bar > Diagram > Build with code. Read top to bottom.
flowchart TB
  T["GUIDE 4 · KAFKA AND THE REPLAY<br/>src/producers/ · scripts/create_topics.py"]

  subgraph S1["1 · WHY REPLAY"]
    direction TB
    y1["LIVE DATA WOULD TAKE 10 YEARS<br/>so we resend 2016–2025 a day at a time<br/>the job can't tell the difference"]
  end

  subgraph S2["2 · KAFKA WORDS"]
    direction TB
    k1["BROKER<br/>a Kafka server"]
    k2["TOPIC<br/>a named queue<br/>calendar-effects-master"]
    k3["PRODUCER · CONSUMER<br/>replay_master.py · the job"]
    k4["PARTITION<br/>ours: 1, so strict order"]
    k5["OFFSET<br/>where a reader resumes"]
    k6["RETENTION<br/>old messages expire"]
  end

  subgraph S3["3 · THE PRODUCER · replay_master.py · Sanchi"]
    direction LR
    p1["READ<br/>master.parquet"] --> p2["SORT<br/>by date, then ticker"] --> p3["SEND<br/>1 JSON message per row<br/>key = ticker"] --> p4["WAIT<br/>0.1 s per day<br/>≈ 4 min for 10 years"]
  end

  subgraph S4["4 · THE LATE-DATA TEST · task 7"]
    direction TB
    l1["THE SETTING<br/>2% of messages sent 3 days late"]
    l2["WHY<br/>real feeds arrive late<br/>or out of order"]
    l3["REPEATABLE<br/>the same messages every run (--seed)"]
  end

  subgraph S5["5 · ONE MESSAGE · one row of master"]
    direction TB
    m1["EXAMPLE<br/>WMT · 2024-11-29 · ret 0.012<br/>views 6500 · black_friday"]
    m2["EMPTY VALUES → null<br/>NaN is not valid JSON"]
  end

  subgraph S6["6 · THE LAPTOP WAY · no Kafka needed"]
    direction LR
    f1["replay_to_files.py<br/>same messages"] --> f2["data/replay/<br/>1 file per 5 days"] --> f3["job.py --source files<br/>≈ 5 minutes"] -.- f4["NOTE<br/>Spark reads the files by<br/>modified time, not name"]
  end

  T --> S1 --> S2 --> S3 --> S4 --> S5 --> S6

  classDef head fill:#2C2C2A,stroke:#2C2C2A,color:#FFFFFF
  classDef idea fill:#E6F1FB,stroke:#185FA5,color:#0C447C
  classDef step fill:#E1F5EE,stroke:#0F6E56,color:#085041
  classDef num fill:#FAEEDA,stroke:#854F0B,color:#633806
  classDef file fill:#F1EFE8,stroke:#5F5E5A,color:#444441
  classDef warn fill:#FAECE7,stroke:#993C1D,color:#712B13
  class T head
  class y1,k1,k2,k3,k4,k5,k6 idea
  class p1,p2,p3,p4,f1,f3 step
  class l1,l2,l3 num
  class m1,m2,f2 file
  class f4 warn
  style S1 fill:#FFFFFF,stroke:#B4B2A9
  style S2 fill:#FFFFFF,stroke:#B4B2A9
  style S3 fill:#FFFFFF,stroke:#B4B2A9
  style S4 fill:#FFFFFF,stroke:#B4B2A9
  style S5 fill:#FFFFFF,stroke:#B4B2A9
  style S6 fill:#FFFFFF,stroke:#B4B2A9
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.