# Phase 34: Core ClickHouse Writer - Context

**Gathered:** 2026-03-28
**Status:** Ready for planning
**Mode:** Auto-generated (infrastructure phase — discuss skipped)

<domain>
## Phase Boundary

A fully tested Rust `ClickHouseWriterSink` can serialize bars and POST them to ClickHouse with correct dedup tokens, batch buffering, retry, and response validation -- all in isolation without touching the sidecar.

Requirements: WRITER-01 through WRITER-09

</domain>

<decisions>
## Implementation Decisions

### Claude's Discretion

All implementation choices are at Claude's discretion — pure infrastructure phase. Use ROADMAP phase goal, success criteria, and codebase conventions to guide decisions.

Key guidance from research:

- Use `clickhouse` crate v0.14.2 (official Rust client, RowBinary over HTTP)
- Use `md5` crate 0.7 for dedup token parity with Python
- Use `parquet` crate 58 for dead-letter (Phase 35, but dep added now)
- Reuse existing `backon` crate for retry/backoff
- Flush thread pattern: `std::thread` with `reqwest::blocking` and bounded `SyncSender`
- Feature flag behind `clickhouse-sink` Cargo feature gate
- Plugin columns discovered via `system.columns` query, not hardcoded

</decisions>

<code_context>

## Existing Code Insights

### Reusable Assets

- `BarSink` trait at `crates/opendeviationbar-streaming/src/engine/traits.rs` (on_bar, flush, name)
- `SinkError` enum: Recoverable/Unrecoverable variants
- `ChannelSink` reference implementation (mpsc-based)
- `CompletedBar` struct with all OHLCV + microstructure fields
- `arrow_export.rs` for RecordBatch schema reference (column names + types)
- Existing `reqwest` in workspace with `rustls-tls` feature

### Established Patterns

- Crate organization: streaming crate has engine/ subdirectory with mod.rs, traits.rs, sink_channel.rs
- PyO3 bindings in `src/streaming_bindings.rs` and `src/stream_manager_bindings.rs`
- Configuration via environment variables (pydantic-settings pattern in Python)

### Integration Points

- `LiveBarEngine` in `live_engine/mod.rs` — will eventually dispatch to this sink (Phase 36)
- `StreamManager` exposes sinks to Python via PyO3 (Phase 36)
- ClickHouse schema at `python/opendeviationbar/clickhouse/schema.sql`
- Dedup token formula: `MD5(f"{symbol}_{threshold}_{start_ts}_{end_ts}_{ouroboros_mode}")`

</code_context>

<specifics>
## Specific Ideas

No specific requirements — infrastructure phase. Refer to ROADMAP phase description and success criteria.

</specifics>

<deferred>
## Deferred Ideas

None — infrastructure phase.

</deferred>
