← All lessons/Working with Records
8
Working with Records

Batch Writes

Write many records atomically, understand the GroupCommitter, and measure throughput.

Prerequisite: Lesson 5 complete

What you'll learn

  • POST /v1/agents/:id/write/batch with records array
  • GroupCommitter — all records in one fsync
  • Throughput: ~200–500 req/s individual vs ~3,000–5,000 records/s batch
  • Batch response — written count + per-record hashes
  • Mixed TTLs within a single batch
Challenge

Write 100 events in one batch. Measure wall time. Compare to 100 individual writes.

Interactive Walkthrough

What you'll learn

Write many records atomically, understand the GroupCommitter, and measure the throughput difference.

Single-agent batch

curl -s -X POST http://localhost:7475/v1/agents/events/write/batch \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "records": [
      {"payload": {"type": "click", "element": "hero_cta"}},
      {"payload": {"type": "scroll", "depth": 50}},
      {"payload": {"type": "click", "element": "pricing_link"}},
      {"payload": {"type": "page_exit", "time_on_page_ms": 12400}}
    ]
  }' | python3 -m json.tool

All four records are written in one fsync. Sequences are contiguous. If the process crashes mid-batch, the WAL ensures full recovery.

Batch response

{
  "written": 4,
  "records": [
    {"content_hash": "...", "sequence": 1, "ts_hlc": ...},
    {"content_hash": "...", "sequence": 2, "ts_hlc": ...},
    {"content_hash": "...", "sequence": 3, "ts_hlc": ...},
    {"content_hash": "...", "sequence": 4, "ts_hlc": ...}
  ]
}

Throughput difference

Write modeApprox. throughput
Individual writes (1 fsync each)~200–500 req/s
Batch writes (1 fsync per batch)~3,000–5,000 records/s

Use batch writes whenever you are ingesting events, logs, or sensor data.

Batch with mixed TTLs

curl -s -X POST http://localhost:7475/v1/agents/sessions/write/batch \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "records": [
      {"payload": {"token": "a"}, "ttl_ms": 3600000},
      {"payload": {"token": "b"}, "ttl_ms": 7200000},
      {"payload": {"token": "c"}}
    ]
  }' | python3 -m json.tool

Each record in a batch can have its own TTL.

Challenge

Write a Python or shell script that generates 100 event records and sends them in a single batch call. Measure wall time vs. 100 individual writes.

---

← Previous
Lesson 7: Soft Delete & Tombstones
Next →
Lesson 9: Per-Record TTL