durable-streams-server 0.3.0

Durable Streams protocol server in Rust, built with axum and tokio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# durable-streams-server

`durable-streams-server` is a Rust implementation of the
[Durable Streams protocol](https://github.com/durable-streams/durable-streams),
built with `axum` and `tokio`.

It can run as a standalone server or be embedded into an existing `axum`
application with `build_router` or `build_router_with_ready`.

## Features

- Durable Streams HTTP API with create, append, read, head, close, and delete operations
- Live reads via long-polling and Server-Sent Events (SSE)
- In-memory, file-backed, and ACID (`redb`) storage backends
- Explicit transport modes: `http`, `tls`, and `mtls`
- Reverse-proxy trust gating for `X-Forwarded-*` and RFC 7239 `Forwarded`
- Layered configuration from TOML files and `DS_*` environment variables
- Structured startup diagnostics with phase-aware failures
- Structured `application/problem+json` error responses
- Request telemetry via `tracing` with OpenTelemetry/ECS-friendly field names

## Running

From the workspace root:

```bash
cargo run -p durable-streams-server
```

By default the server listens on `http://0.0.0.0:4437`, exposes health checks
at `/healthz` and `/readyz`, and mounts the protocol at `/v1/stream`.

Use `http.stream_base_path` or `DS_HTTP__STREAM_BASE_PATH` to mount the
protocol at another path.

## Storage Backends

The default storage mode is in-memory. For persistence, choose a backend via
`DS_STORAGE__MODE`:

| Mode | Durability | Use case |
|------|------------|----------|
| `memory` | None (lost on restart) | Development and testing |
| `file-fast` | Buffered writes | Lower-latency persistence where recent data loss is acceptable |
| `file-durable` | Fsynced writes | Durable persistence without external dependencies |
| `acid` | Crash-resilient (`redb`) | Production workloads requiring transactional durability |

Examples:

```bash
DS_STORAGE__MODE=file-durable DS_STORAGE__DATA_DIR=./data cargo run -p durable-streams-server
DS_STORAGE__MODE=acid DS_STORAGE__DATA_DIR=./data cargo run -p durable-streams-server
```

## Configuration

Configuration is loaded in this order, with later sources overriding earlier
ones:

1. Built-in defaults
2. Built-in profile defaults (`default`, `dev`, `prod`, `prod-tls`, `prod-mtls`)
3. `config/default.toml`
4. `config/<profile>.toml`
5. `config/local.toml`
6. `--config <path>`
7. Environment variables

Examples:

```bash
cargo run -p durable-streams-server -- --profile dev
cargo run -p durable-streams-server -- --profile prod
cargo run -p durable-streams-server -- --profile prod-tls --config /etc/durable-streams/server.toml
cargo run -p durable-streams-server -- --profile prod-mtls --config /etc/durable-streams/server.toml
```

The effective config schema is nested and operator-facing:

```toml
[server]
bind_address = "0.0.0.0:4437"

[limits]
max_memory_bytes = 104857600
max_stream_bytes = 10485760

[http]
cors_origins = "*"
stream_base_path = "/v1/stream"

[storage]
mode = "memory"
data_dir = "./data/streams"

[transport]
mode = "http" # http | tls | mtls

[transport.http]
versions = ["http1"] # http1 | http2

[transport.connection]
long_poll_timeout_secs = 30
sse_reconnect_interval_secs = 60

[transport.tls]
min_version = "1.2"
max_version = "1.3"
alpn_protocols = ["http/1.1"]

[proxy]
enabled = false
forwarded_headers = "none" # none | x-forwarded | forwarded
trusted_proxies = []

[proxy.identity]
mode = "none" # none | header
require_tls = true

[observability]
rust_log = "info"
```

Environment overrides follow the TOML path with `DS_` prefixes and double
underscores. For example:

- `transport.mode` -> `DS_TRANSPORT__MODE`
- `transport.tls.cert_path` -> `DS_TRANSPORT__TLS__CERT_PATH`
- `proxy.identity.header_name` -> `DS_PROXY__IDENTITY__HEADER_NAME`

In practice, operators usually only need a small subset:

| Purpose | Variables | When to set |
|---------|-----------|-------------|
| Bind and logging | `DS_SERVER__BIND_ADDRESS`, `DS_OBSERVABILITY__RUST_LOG`, `RUST_LOG` | Override the listen address or log verbosity without editing TOML |
| Storage selection | `DS_STORAGE__MODE`, `DS_STORAGE__DATA_DIR` | Pick persistence mode and storage path |
| Direct TLS | `DS_TRANSPORT__MODE`, `DS_TRANSPORT__TLS__CERT_PATH`, `DS_TRANSPORT__TLS__KEY_PATH` | Required for `tls` deployments |
| Direct mTLS | `DS_TRANSPORT__MODE`, `DS_TRANSPORT__TLS__CLIENT_CA_PATH` | Required in addition to TLS vars for `mtls` deployments |
| HTTP/2 and ALPN | `DS_TRANSPORT__HTTP__VERSIONS`, `DS_TRANSPORT__TLS__ALPN_PROTOCOLS` | Only when you need to override the profile defaults |
| Reverse proxy trust | `DS_PROXY__ENABLED`, `DS_PROXY__FORWARDED_HEADERS`, `DS_PROXY__TRUSTED_PROXIES` | When the backend should trust a proxy’s forwarded origin metadata |
| Proxy identity handoff | `DS_PROXY__IDENTITY__MODE`, `DS_PROXY__IDENTITY__HEADER_NAME`, `DS_PROXY__IDENTITY__REQUIRE_TLS` | Only for trusted proxy identity handoff in `mtls` mode |

Less common overrides, such as stream limits or acid-specific shard tuning,
follow the same `DS_<SECTION>__<FIELD>` pattern and generally only need to be
set when deviating from the profile examples.

## Deployment Styles

The server supports three deployment styles today.

### 1. Dev HTTP

Use this for local development, CI, or when TLS is terminated elsewhere and the
inner network is intentionally trusted.

- Profile: `dev`
- Transport: direct `http`
- HTTP versions: `http1`
- Storage: defaults to in-memory unless overridden

Example:

```bash
cargo run -p durable-streams-server -- --profile dev
```

Or with persistence:

```bash
DS_STORAGE__MODE=file-durable \
DS_STORAGE__DATA_DIR=./data \
cargo run -p durable-streams-server -- --profile dev
```

### 2. Direct TLS

Use this when the server terminates TLS itself and clients connect directly.

- Profile: `prod-tls`
- Transport: `tls`
- TLS versions: `1.2` minimum, `1.3` supported
- ALPN: `http/1.1` and `h2`
- Storage: durable file-backed by default

Example config:

```toml
[storage]
mode = "file-durable"
data_dir = "/var/lib/durable-streams"

[limits]
max_memory_bytes = 536870912
max_stream_bytes = 268435456

[transport]
mode = "tls"

[transport.http]
versions = ["http1", "http2"]

[transport.tls]
cert_path = "/etc/durable-streams/tls/server.crt"
key_path = "/etc/durable-streams/tls/server.key"
min_version = "1.2"
max_version = "1.3"
alpn_protocols = ["http/1.1", "h2"]
```

Run it:

```bash
cargo run -p durable-streams-server -- --profile prod-tls --config /etc/durable-streams/server.toml
```

### 3. Direct mTLS

Use this when clients must authenticate with certificates and the server is the
TLS termination point.

- Profile: `prod-mtls`
- Transport: `mtls`
- TLS versions: `1.2` minimum, `1.3` supported
- ALPN: `http/1.1` and `h2`
- Client authentication: required via `transport.tls.client_ca_path`

Example config:

```toml
[storage]
mode = "file-durable"
data_dir = "/var/lib/durable-streams"

[limits]
max_memory_bytes = 536870912
max_stream_bytes = 268435456

[transport]
mode = "mtls"

[transport.http]
versions = ["http1", "http2"]

[transport.tls]
cert_path = "/etc/durable-streams/tls/server.crt"
key_path = "/etc/durable-streams/tls/server.key"
client_ca_path = "/etc/durable-streams/tls/client-ca.crt"
min_version = "1.2"
max_version = "1.3"
alpn_protocols = ["http/1.1", "h2"]
```

Run it:

```bash
cargo run -p durable-streams-server -- --profile prod-mtls --config /etc/durable-streams/server.toml
```

## Reverse Proxy and TLS Topologies

The server intentionally keeps auth out of the application. If you need edge
authn/authz, terminate it at a proxy or gateway and forward only the minimum
origin metadata the server is configured to trust.

### Edge TLS termination, backend HTTP

This is the common pattern for local ingress or a trusted internal mesh:

- Edge terminates external TLS
- Backend link to durable-streams-server is plain HTTP
- `transport.mode = "http"`
- `proxy.enabled = true`
- `proxy.forwarded_headers = "x-forwarded"` or `"forwarded"`
- `proxy.trusted_proxies` contains only the proxy source IPs/CIDRs

Example:

```toml
[transport]
mode = "http"

[proxy]
enabled = true
forwarded_headers = "x-forwarded"
trusted_proxies = ["10.0.0.0/24"]
```

### Edge TLS termination, backend mTLS to the server

Use this when the proxy itself must authenticate to the server:

- Edge or service proxy terminates external TLS
- Proxy establishes mTLS to durable-streams-server
- `transport.mode = "mtls"`
- `proxy.enabled = true`
- `proxy.trusted_proxies` contains only proxy addresses
- `proxy.identity.mode = "header"` is only allowed in this mode

Example:

```toml
[transport]
mode = "mtls"

[transport.http]
versions = ["http1", "http2"]

[transport.tls]
cert_path = "/etc/durable-streams/tls/server.crt"
key_path = "/etc/durable-streams/tls/server.key"
client_ca_path = "/etc/durable-streams/tls/proxy-ca.crt"
alpn_protocols = ["http/1.1", "h2"]

[proxy]
enabled = true
forwarded_headers = "forwarded"
trusted_proxies = ["10.0.10.15/32", "10.0.10.16/32"]

[proxy.identity]
mode = "header"
header_name = "x-client-identity"
require_tls = true
```

### Envoy example

Illustrative deployment shape:

```text
client -> Envoy (TLS or mTLS) -> durable-streams-server (HTTP or mTLS)
```

If Envoy terminates external TLS and forwards to backend HTTP:

- trust only Envoy addresses in `proxy.trusted_proxies`
- configure Envoy to emit either `X-Forwarded-*` or `Forwarded`
- keep `proxy.forwarded_headers` aligned with what Envoy emits

If Envoy forwards to backend mTLS:

- use `transport.mode = "mtls"`
- issue Envoy a client cert signed by `transport.tls.client_ca_path`
- if Envoy injects an identity header, keep `proxy.identity.mode = "header"` and
  scope `trusted_proxies` narrowly

## HTTP Version Policy

Current protocol support:

- `http` mode: `http1` only
- `tls` mode: `http1` or `http1 + http2`
- `mtls` mode: `http1` or `http1 + http2`
- `http3`: rejected at config validation time because it is not implemented

ALPN must match the configured HTTP versions:

- `http1` requires `http/1.1`
- `http2` requires `h2`

If you enable `http2`, keep the ALPN list consistent:

```toml
[transport.http]
versions = ["http1", "http2"]

[transport.tls]
alpn_protocols = ["http/1.1", "h2"]
```

## Migration From Legacy TLS Config

Legacy compatibility still exists for the old TLS/log fields, but new
deployments should author the explicit transport model directly.

Field mapping:

- `server.port` -> `server.bind_address = "0.0.0.0:<port>"`
- `[tls].cert_path` -> `[transport.tls].cert_path`
- `[tls].key_path` -> `[transport.tls].key_path`
- `[log].rust_log` -> `[observability].rust_log`

Typical migration steps:

1. Replace `server.port` with `server.bind_address`.
2. Move legacy TLS paths under `[transport.tls]`.
3. Add `transport.mode = "tls"` or `transport.mode = "mtls"`.
4. Add `transport.http.versions` and matching `transport.tls.alpn_protocols`.
5. If a proxy is in front, move any old implicit trust assumptions into
   `proxy.enabled`, `proxy.forwarded_headers`, and `proxy.trusted_proxies`.
6. If proxy identity handoff is needed, move to `transport.mode = "mtls"` and
   configure `[proxy.identity]`.

Minimal before:

```toml
[server]
port = 4437

[tls]
cert_path = "/etc/ds/server.crt"
key_path = "/etc/ds/server.key"

[log]
rust_log = "info"
```

Minimal after:

```toml
[server]
bind_address = "0.0.0.0:4437"

[transport]
mode = "tls"

[transport.http]
versions = ["http1", "http2"]

[transport.tls]
cert_path = "/etc/ds/server.crt"
key_path = "/etc/ds/server.key"
alpn_protocols = ["http/1.1", "h2"]

[observability]
rust_log = "info"
```

## Startup Failure Troubleshooting

Startup failures are phase-aware. The process reports the failing phase in the
error message, for example `[check_tls_files]` or `[bind_listener]`.

### `[load_config]`

Likely causes:

- TOML syntax errors
- `--config` points at a missing file
- invalid `DS_*` override values

Check:

- `config/default.toml`, `config/<profile>.toml`, `config/local.toml`
- override file path passed with `--config`
- environment values such as `DS_TRANSPORT__MODE`, `DS_TRANSPORT__TLS__MIN_VERSION`

### `[validate_config]`

Likely causes:

- `transport.mode` and TLS fields do not agree
- `transport.http.versions` and `transport.tls.alpn_protocols` do not match
- `proxy.enabled = true` without `trusted_proxies`
- `proxy.identity.mode = "header"` without `transport.mode = "mtls"`
- invalid `server.bind_address`, base path, or CIDR values

Check:

- transport mode and all required TLS paths
- ALPN vs HTTP version consistency
- proxy trust and identity sections

### `[check_tls_files]`

Likely causes:

- cert, key, or client CA path missing
- path points at a directory, not a file
- file exists but the process cannot read it

Check:

```bash
ls -l /etc/durable-streams/tls
```

Make sure the configured path names exist and are readable by the service user.

### `[build_tls_context]`

Likely causes:

- PEM file contents are malformed
- certificate chain is empty
- private key is missing or does not match the certificate
- client CA bundle is invalid

Check:

- cert/key pair correctness
- PEM encoding
- CA bundle contents for mTLS deployments

### `[bind_listener]`

Likely causes:

- address already in use
- insufficient privileges for the port
- invalid bind target at runtime

Check:

```bash
lsof -iTCP:4437 -sTCP:LISTEN
```

If binding to a privileged port, ensure the process manager and user privileges
match your deployment model.

### `[start_server]`

Likely causes:

- runtime listener or service failure after startup
- storage backend initialisation failure

Check:

- storage directory permissions and available space
- acid/file backend startup logs
- any preceding error chain in the process logs

## Library Use

For embedding, the main entry points are:

- `Config` and `ConfigLoadOptions` for configuration loading
- `build_router` and `build_router_with_ready` for mounting the HTTP API
- `InMemoryStorage`, `FileStorage`, and `AcidStorage` for backend selection

## Example Config Files

The crate ships profile-oriented example files:

- `config/default.toml`
- `config/dev.toml`
- `config/prod.toml`
- `config/prod-tls.toml`
- `config/prod-mtls.toml`

Use them as layered building blocks rather than copying the entire merged
config by hand.

## Verification

```bash
cargo build -p durable-streams-server
cargo test -p durable-streams-server
cargo clippy -p durable-streams-server --all-targets
cargo fmt --all
```