# Quickstart

> Record a real crash, replay it with no broker, and watch Kavach accept a correct fix and reject one that is too narrow. About five minutes.

The demo is a wallet ledger. It folds a stream of JSON events (`deposit`, `withdraw`, `transfer`) into balances. One upstream event arrives with `"amount": null`, and the handler crashes on it.

You will build three versions of the ledger: the one with the bug, a correct fix, and a fix that is too narrow. The commands use the Go version; every [SDK](https://www.thekavachlabs.com/docs/sdks) ships a port of the same demo with the same bug.

## 1. Get the demo

You need Go 1.24 or later to build the demo (your own services do not need Go).

```bash
git clone https://github.com/kavachlabs/kavach && cd kavach
go install ./cmd/kavach ./cmd/kavach-recorder   # or use the install script
go build -o ledger-old ./examples/ledger/sdk
go build -tags ledgerfix -o ledger-new ./examples/ledger/sdk
go build -tags ledgerpartialfix -o ledger-partial ./examples/ledger/sdk
```

`ledger-new` rejects any event with no amount. `ledger-partial` rejects it only for deposits, the event type in the incident.

## 2. Record the crash

```bash
./ledger-old -in examples/ledger/testdata/events.jsonl
```

The ledger panics on the eighth event. Before the process dies, the flight recorder writes a fixture to `kavach/fixtures/ledger-*.kavach`.

## 3. Read what happened

```bash
kavach inspect kavach/fixtures/ledger-*.kavach
```

`inspect` prints the journal record by record: each input, the clock and random reads the handler made, its outputs, and the marker that says how the step failed. Add `--full` for the panic stack, or `--json` to parse it.

## 4. Replay it

```bash
kavach replay kavach/fixtures/ledger-*.kavach --bin ./ledger-old
```

```text
service   ledger · start genesis · 34 records · 8 steps replayed
recorded  panic at seq 32: runtime error: invalid memory address or nil pointer dereference
result    still_failing@32
```

`--bin` is a **host command**: Kavach starts your own build in replay mode and feeds it the recorded inputs, answering every clock, random and config read from the fixture. Outputs are captured, never delivered, so replay touches no external system. `still_failing@32` means the step for the input at sequence number 32 (the eighth event, `evt-008`) fails, just as it did in production.

## 5. Verify the correct fix

```bash
kavach diff kavach/fixtures/ledger-*.kavach --old ./ledger-old --new ./ledger-new
```

```text
old       still_failing@32
new       fixed
first divergence at step seq 32: output 0 of step 32 differs
  old  (none)
  new  ledger.rejections  {"event":"evt-008","reason":"missing amount"}
variants  51 of 61 reproduce the incident on the old build · 51 of those pass on the new build
regressions 47 of 47 match the old build · 48 variants of 4 earlier inputs resembling the failing one
verdict   fixed
```

`diff` replays the fixture on both builds. It also builds **variants** of the incident (the bad event's fields changed, the event moved earlier, earlier events dropped or redelivered, the clock shifted) and keeps those on which the old build fails the same way. Then it checks **regression variants**: the same changes applied to earlier events that look like the failing one, which the old build handled fine. The new build passes every reproducing variant and leaves every regression variant unchanged, so the verdict is `fixed`.

## 6. Watch a narrow fix fail

```bash
kavach diff kavach/fixtures/ledger-*.kavach --old ./ledger-old --new ./ledger-partial
```

```text
new       fixed
variants  51 of 61 reproduce the incident on the old build · 44 of those pass on the new build
  ✗  7 failing input (seq 32): "type" "deposit" → "transfer"
       still_failing@23: panic: runtime error: invalid memory address or nil pointer dereference
       saved /tmp/kavach-variants-…/ledger-…-000032.variant-07.kavach
  …
verdict   variant_failed(7)@23
```

The partial fix passes the recorded crash (`new fixed`). But variant 7 is the same null amount on a `transfer`, and the partial fix still crashes on it. Kavach saves every failing variant so you can replay it directly:

```bash
kavach replay /tmp/kavach-variants-…/ledger-…-000032.variant-07.kavach --bin ./ledger-partial
```

Pass `--keep DIR` to choose where variants are saved, or `--json` and read `variants.checks[].file`.

## 7. See the flow

```bash
kavach map examples/ledger/.kavach/plan/map.json examples/ledger/testdata/null-amount.kavach \
  --bin ./ledger-old --new ./ledger-new -o ledger-map.html
open ledger-map.html
```

`kavach map` draws one self-contained HTML page: how events flow from entry points through handlers, state and sinks to invariant checks, with each recorded step and the fix's effect on it. See [Flow maps](https://www.thekavachlabs.com/docs/guides/flow-maps).

## The same demo on Kafka

The ledger can also read a Kafka topic, which is its journal: on every start it folds the topic from the first offset. With a local broker:

```bash
docker run -d -p 9092:9092 apache/kafka:3.9.0
./ledger-old -kafka localhost:9092 -seed examples/ledger/testdata/events.jsonl   # load the topic once
./ledger-old -kafka localhost:9092                                               # crashes at offset 0:7
```

The fixture's inputs carry their Kafka positions (`kafka:wallet-events @ 0:7`), and it still replays with no broker running. Kafka is a dependency of the demo only, not of the SDK.

## Next

- [Concepts](https://www.thekavachlabs.com/docs/concepts): the words used in the rest of the docs.
- [Instrument a service](https://www.thekavachlabs.com/docs/guides/instrument-a-service): record your own.
- [Fix an incident](https://www.thekavachlabs.com/docs/guides/fix-an-incident): the loop, step by step.

Source: https://www.thekavachlabs.com/docs/quickstart
