satoridb 0.1.2

Embedded vector database for approximate nearest neighbor search (experimental).
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# SatoriDB Architecture

SatoriDB is an embedded vector database for approximate nearest neighbor (ANN) search.
It runs entirely in-process and persists vector data to Walrus, a topic-based storage engine.

---

## System Overview

```
                                 ┌─────────────────────────────────────────────────────────────┐
                                 │                        SatoriDB                             │
                                 └─────────────────────────────────────────────────────────────┘
                         ┌──────────────────────────────────┼──────────────────────────────────┐
                         │                                  │                                  │
                         ▼                                  ▼                                  ▼
              ┌────────────────────┐             ┌────────────────────┐             ┌────────────────────┐
              │   SatoriHandle     │             │   Router Manager   │             │     Rebalancer     │
              │      (API)         │             │     (Thread)       │             │     (Thread)       │
              │                    │             │                    │             │                    │
              │  • query()         │────────────▶│  • HNSW Router     │             │  • Monitor sizes   │
              │  • insert()        │             │  • Bucket metadata │             │  • Split buckets   │
              │  • flush()         │             │  • Centroid mgmt   │             │  • K-means (k=2)   │
              └────────────────────┘             └────────────────────┘             └────────────────────┘
              ┌─────────────────────────────────────────────────────────────────────────────────────────┐
              │                              Consistent Hash Ring                                       │
              │                            bucket_id  ───▶  worker_shard                                │
              └─────────────────────────────────────────────────────────────────────────────────────────┘
         ┌───────────────┼───────────────┬───────────────┬───────────────┐
         │               │               │               │               │
         ▼               ▼               ▼               ▼               ▼
   ┌───────────┐   ┌───────────┐   ┌───────────┐   ┌───────────┐   ┌───────────┐
   │  Worker 0 │   │  Worker 1 │   │  Worker 2 │   │  Worker 3 │   │  Worker N │
   │  (CPU 0)  │   │  (CPU 1)  │   │  (CPU 2)  │   │  (CPU 3)  │   │  (CPU N)  │
   │           │   │           │   │           │   │           │   │           │
   │  Glommio  │   │  Glommio  │   │  Glommio  │   │  Glommio  │   │  Glommio  │
   │  Executor │   │  Executor │   │  Executor │   │  Executor │   │  Executor │
   └─────┬─────┘   └─────┬─────┘   └─────┬─────┘   └─────┬─────┘   └─────┬─────┘
         │               │               │               │               │
         └───────────────┴───────────────┴───────┬───────┴───────────────┘
              ┌─────────────────────────────────────────────────────────────────────────────────────────┐
              │                                                                                         │
              │                                   Storage Layer                                         │
              │                                                                                         │
              │    ┌─────────────────────────────────────────────────────────────────────────────┐     │
              │    │                              Walrus                                          │     │
              │    │                     (Core Storage Engine)                                    │     │
              │    │                                                                              │     │
              │    │   Topic-based append-only storage for bucket/cluster data                   │     │
              │    │   • io_uring batch I/O (Linux)                                              │     │
              │    │   • 10MB blocks, 1GB files                                                  │     │
              │    │   • FNV-1a checksums                                                        │     │
              │    └─────────────────────────────────────────────────────────────────────────────┘     │
              │                                                                                         │
              │    ┌────────────────────────────┐       ┌────────────────────────────┐                 │
              │    │       VectorIndex          │       │       BucketIndex          │                 │
              │    │        (RocksDB)           │       │        (RocksDB)           │                 │
              │    │     id ──▶ vector          │       │     id ──▶ bucket_id       │                 │
              │    └────────────────────────────┘       └────────────────────────────┘                 │
              │                                                                                         │
              └─────────────────────────────────────────────────────────────────────────────────────────┘
```

---

## Core Concepts

### Buckets (Clusters)

Vectors are organized into **buckets** - clusters of similar vectors. Each bucket has:

```
┌──────────────────────────────────────────────────────────────────────┐
│                           Bucket                                     │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│   ID:        42                                                      │
│   Centroid:  [0.12, 0.45, 0.78, 0.33, ...]   ◄── mean of all vectors│
│   Count:     1,847                                                   │
│                                                                      │
│   Vectors (stored in Walrus topic "bucket_42"):                      │
│   ┌────────────────────────────────────────────────────────────┐    │
│   │  id: 1001  │  [0.11, 0.44, 0.79, 0.32, ...]               │    │
│   ├────────────────────────────────────────────────────────────┤    │
│   │  id: 1002  │  [0.13, 0.46, 0.77, 0.34, ...]               │    │
│   ├────────────────────────────────────────────────────────────┤    │
│   │  id: 1003  │  [0.12, 0.45, 0.78, 0.33, ...]               │    │
│   ├────────────────────────────────────────────────────────────┤    │
│   │    ...     │              ...                              │    │
│   └────────────────────────────────────────────────────────────┘    │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘
```

### Two-Tier Search Architecture

```
                              ┌───────────────────┐
                              │    Query Vector   │
                              │  [0.1, 0.2, ...]  │
                              └─────────┬─────────┘
                    ════════════════════╪════════════════════
                         TIER 1: ROUTING (Router Manager)
                    ════════════════════╪════════════════════
                    ┌───────────────────────────────────────┐
                    │             HNSW Index                │
                    │       (over bucket centroids)         │
                    │                                       │
                    │   Quantized to u8, cosine distance    │
                    │   M=24, ef_construction=180           │
                    │                                       │
                    │   Input:  query vector, top_k=200     │
                    │   Output: [bucket_12, bucket_47, ...] │
                    └───────────────────────────────────────┘
                    ┌───────────────────────────────────────┐
                    │      Selected Bucket IDs              │
                    │   [12, 47, 89, 103, 156, 201, ...]    │
                    └───────────────────────────────────────┘
                    ════════════════════╪════════════════════
                        TIER 2: SCANNING (Workers)
                    ════════════════════╪════════════════════
              ┌─────────────────────────┼─────────────────────────┐
              │                         │                         │
              ▼                         ▼                         ▼
     ┌─────────────────┐       ┌─────────────────┐       ┌─────────────────┐
     │    Worker 0     │       │    Worker 1     │       │    Worker 2     │
     │                 │       │                 │       │                 │
     │  Buckets:       │       │  Buckets:       │       │  Buckets:       │
     │  [12, 89, 201]  │       │  [47, 156]      │       │  [103]          │
     │                 │       │                 │       │                 │
     │  For each:      │       │  For each:      │       │  For each:      │
     │  1. Load from   │       │  1. Load from   │       │  1. Load from   │
     │     Walrus      │       │     Walrus      │       │     Walrus      │
     │  2. L2 scan     │       │  2. L2 scan     │       │  2. L2 scan     │
     │  3. Return      │       │  3. Return      │       │  3. Return      │
     │     top-100     │       │     top-100     │       │     top-100     │
     └────────┬────────┘       └────────┬────────┘       └────────┬────────┘
              │                         │                         │
              └─────────────────────────┼─────────────────────────┘
                    ┌───────────────────────────────────────┐
                    │         Merge & Sort Results          │
                    │                                       │
                    │    Sort all candidates by distance    │
                    │    Return final top_k to caller       │
                    └───────────────────────────────────────┘
```

---

## Component Details

### Router Manager

Single-threaded component that manages bucket metadata and the HNSW routing index:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              Router Manager                                  │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────┐      ┌─────────────────────────────────┐  │
│  │      Bucket Metadata        │      │          HNSW Router            │  │
│  │                             │      │                                 │  │
│  │  HashMap<bucket_id,         │      │  • Stores quantized centroids  │  │
│  │    BucketMeta {             │─────▶│  • Hierarchical graph layers   │  │
│  │      id: u64,               │      │  • Cosine similarity search    │  │
│  │      centroid: Vec<f32>,    │      │                                 │  │
│  │      count: u64,            │      │  Query: O(log N) bucket lookup │  │
│  │    }                        │      │                                 │  │
│  │  >                          │      └─────────────────────────────────┘  │
│  └─────────────────────────────┘                                            │
│                                                                             │
│  Centroid update formula (running mean):                                    │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  new_centroid[i] = (old_centroid[i] * count + vector[i]) / (count+1)│   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  Persisted to Walrus topics:                                                │
│    • "router_snapshot" ─── full bucket state                               │
│    • "router_updates"  ─── incremental centroid updates                    │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

### Workers

Each worker runs a Glommio executor pinned to a CPU:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                           Worker Architecture                                │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         Glommio Executor                             │   │
│  │                    (io_uring async runtime)                          │   │
│  │                                                                      │   │
│  │   • CPU-pinned for cache locality                                   │   │
│  │   • Bounded message channel (capacity: 1000)                        │   │
│  │   • Max 32 concurrent query tasks                                   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                           LRU Cache                                  │   │
│  │                                                                      │   │
│  │   Caches hot bucket data to avoid repeated Walrus reads             │   │
│  │                                                                      │   │
│  │   Default config:                                                   │   │
│  │     • max_buckets: 64                                               │   │
│  │     • max_bytes_per_bucket: 64 MB                                   │   │
│  │     • total: ~4 GB arena                                            │   │
│  │                                                                      │   │
│  │   Eviction: LRU with routing-version invalidation                   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  Message Types:                                                             │
│    ├── Query   ──▶ search buckets, return (id, distance) pairs            │
│  │    ├── Upsert  ──▶ persist single vector                                 │
│    ├── Ingest ──▶ batch persist (with duplicate detection)                │
│    ├── Flush  ──▶ drain pending writes                                    │
│    └── FetchVectors ──▶ retrieve by ID                                    │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

### Consistent Hash Ring

Maps bucket IDs to worker shards:

```
                          Consistent Hash Ring
            ┌──────────────────────────────────────────────┐
            │                                              │
            │                  ┌─────┐                     │
            │             ┌────┤ W0  ├────┐                │
            │        ┌────┘    └─────┘    └────┐           │
            │   ┌────┘                        └────┐      │
            │ ┌─────┐                            ┌─────┐  │
            │ │ W3  │                            │ W1  │  │
            │ └─────┘                            └─────┘  │
            │   └────┐                        ┌────┘      │
            │        └────┐    ┌─────┐    ┌────┘           │
            │             └────┤ W2  ├────┘                │
            │                  └─────┘                     │
            │                                              │
            │   hash(bucket_id) lands on ring              │
            │   → assigned to next clockwise worker        │
            │                                              │
            │   Virtual nodes per worker: 8 (default)      │
            │                                              │
            └──────────────────────────────────────────────┘

    Code: src/tasks.rs:23-50
```

---

## Storage Layer

### Walrus - Core Storage Engine

Walrus is a topic-based append-only storage engine that stores all bucket/cluster data:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                                  Walrus                                      │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  Topics (one per bucket + system topics):                                   │
│                                                                             │
│    bucket_0          ──▶  [entry][entry][entry][entry]...                  │
│    bucket_1          ──▶  [entry][entry]...                                │
│    bucket_42         ──▶  [entry][entry][entry][entry][entry]...           │
│    bucket_103        ──▶  [entry][entry][entry]...                         │
│    router_snapshot   ──▶  [snap][snap]...                                  │
│    router_updates    ──▶  [update][update][update]...                      │
│    bucket_meta       ──▶  [active:0][active:1][retired:5]...               │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  Entry Format (per vector):                                                 │
│  ┌──────────────────────────────────────────────────────────────────────┐  │
│  │ PREFIX_META (256B)  │  len (8B)  │  id (8B)  │  dim (8B)  │  f32[]   │  │
│  │                     │            │           │            │          │  │
│  │  • meta length      │  payload   │  vector   │  vector    │  vector  │  │
│  │  • owned_by topic   │  size      │  id       │  dimension │  data    │  │
│  │  • next_block_start │            │           │            │          │  │
│  │  • FNV-1a checksum  │            │           │            │          │  │
│  └──────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  Physical Layout:                                                           │
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                         File (1 GB)                                  │   │
│  │  ┌─────────┬─────────┬─────────┬─────────┬─────────────────────────┐│   │
│  │  │ Block 0 │ Block 1 │ Block 2 │   ...   │      Block 99           ││   │
│  │  │  10 MB  │  10 MB  │  10 MB  │         │       10 MB             ││   │
│  │  └─────────┴─────────┴─────────┴─────────┴─────────────────────────┘│   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  Constants (src/storage/wal/config.rs):                                     │
│    • DEFAULT_BLOCK_SIZE:  10 MB                                            │
│    • BLOCKS_PER_FILE:     100                                              │
│    • PREFIX_META_SIZE:    256 bytes                                        │
│    • MAX_BATCH_ENTRIES:   2,000,000                                        │
│    • MAX_BATCH_BYTES:     10 GB                                            │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  I/O Backends:                                                              │
│    • FD Backend (default): pread/pwrite, io_uring for batches             │
│    • Mmap Backend:         memory-mapped files (fallback)                  │
│                                                                             │
│  Fsync Schedule:                                                            │
│    • Milliseconds(200)  ─── default, fsync every 200ms                     │
│    • SyncEach           ─── O_SYNC on every write                          │
│    • NoFsync            ─── maximum throughput, no durability              │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

    Code: src/storage/wal/
```

### RocksDB Indexes

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                             RocksDB Indexes                                  │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────┐   ┌─────────────────────────────────┐ │
│  │         VectorIndex             │   │         BucketIndex             │ │
│  │                                 │   │                                 │ │
│  │  Key:   vector_id (u64, LE)     │   │  Key:   vector_id (u64, LE)     │ │
│  │  Value: rkyv-serialized Vector  │   │  Value: bucket_id (u64, LE)     │ │
│  │                                 │   │                                 │ │
│  │  Purpose:                       │   │  Purpose:                       │ │
│  │  • Fetch vector by ID           │   │  • Find which bucket contains   │ │
│  │  • Duplicate detection on       │   │    a given vector               │ │
│  │    ingest (first_existing)      │   │  • Used for delete operations   │ │
│  │                                 │   │                                 │ │
│  │  Memory: ~96 MB                 │   │  Memory: ~96 MB                 │ │
│  │    (64MB block cache +          │   │    (64MB block cache +          │ │
│  │     2×16MB write buffers)       │   │     2×16MB write buffers)       │ │
│  └─────────────────────────────────┘   └─────────────────────────────────┘ │
│                                                                             │
│  Code: src/vector_index.rs, src/bucket_index.rs                            │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

---

## Data Flow

### Insert Path

```
   ┌─────────────────────────────────────────────────────────────────────────┐
   │                              INSERT FLOW                                 │
   └─────────────────────────────────────────────────────────────────────────┘

   Client
      │  api.upsert(id=42, vector=[0.1, 0.2, ...])
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  1. ROUTE TO BUCKET                                                      │
   │                                                                          │
   │     RouterManager.RouteOrInit(vector)                                    │
   │       │                                                                  │
   │       ├── If no buckets exist: create bucket_0 with this vector as      │
   │       │   centroid                                                       │
   │       │                                                                  │
   │       └── Else: HNSW.query(vector, top_k=1) → nearest bucket_id         │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  2. DISPATCH TO WORKER                                                   │
   │                                                                          │
   │     shard = hash_ring.node_for(bucket_id)                               │
   │     worker_senders[shard].send(Upsert { bucket_id, vector })            │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  3. WORKER PERSISTS                                                      │
   │                                                                          │
   │     bucket_lock = bucket_locks.lock_for(bucket_id)                      │
   │     _guard = bucket_lock.lock()                                          │
   │                                                                          │
   │     Storage.put_chunk(bucket_id, vector)     ────▶  Walrus topic        │
   │     VectorIndex.put(id, vector)              ────▶  RocksDB             │
   │     BucketIndex.put(id, bucket_id)           ────▶  RocksDB             │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  4. UPDATE ROUTER STATE                                                  │
   │                                                                          │
   │     RouterManager.ApplyUpsert(bucket_id, vector)                        │
   │       │                                                                  │
   │       ├── Update running centroid mean                                  │
   │       ├── pending_updates++                                              │
   │       ├── persist_update() → Walrus "router_updates" topic              │
   │       │                                                                  │
   │       └── If pending_updates >= 1000:                                   │
   │             rebuild_router_and_persist()                                 │
   │             persist_snapshot() → Walrus "router_snapshot" topic         │
   │                                                                          │
   └─────────────────────────────────────────────────────────────────────────┘

   Code: src/service.rs:77-110, src/worker.rs:116-141, src/router_manager.rs:186-200
```

### Query Path

```
   ┌─────────────────────────────────────────────────────────────────────────┐
   │                               QUERY FLOW                                 │
   └─────────────────────────────────────────────────────────────────────────┘

   Client
      │  api.query(vector=[0.1, 0.2, ...], top_k=10, router_top_k=200)
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  1. ROUTER QUERY                                                         │
   │                                                                          │
   │     RouterManager.Query(vector, top_k=200)                              │
   │                                                                          │
   │     HNSW search:                                                         │
   │       • Quantize query vector (f32 → u8)                                │
   │       • Greedy descent through layers                                   │
   │       • Beam search at layer 0 with ef_search=1200-4000                 │
   │       • Return top-200 bucket IDs                                        │
   │                                                                          │
   │     → [bucket_12, bucket_47, bucket_89, bucket_103, ...]                │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  2. GROUP BY WORKER                                                      │
   │                                                                          │
   │     For each bucket_id:                                                  │
   │       shard = hash_ring.node_for(bucket_id)                             │
   │       requests[shard].push(bucket_id)                                   │
   │                                                                          │
   │     Worker 0: [12, 89, 201]                                             │
   │     Worker 1: [47, 156]                                                 │
   │     Worker 2: [103]                                                     │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  3. PARALLEL WORKER QUERIES                                              │
   │                                                                          │
   │     Send QueryRequest to each shard (parallel via join_all)             │
   │                                                                          │
   │     Each worker:                                                         │
   │     ┌───────────────────────────────────────────────────────────────┐   │
   │     │  for bucket_id in bucket_ids:                                  │   │
   │     │      if cache.get(bucket_id):                                  │   │
   │     │          scan_bucket_slice(cached_data, query_vec)             │   │
   │     │      else:                                                     │   │
   │     │          chunks = Storage.get_chunks(bucket_id)  ◄── Walrus   │   │
   │     │          cache.put_from_chunks(bucket_id, chunks)              │   │
   │     │          scan_bucket_chunks(chunks, query_vec)                 │   │
   │     │                                                                │   │
   │     │      // L2 distance computation (SIMD accelerated)            │   │
   │     │      for each vector in bucket:                                │   │
   │     │          dist = sqrt(sum((v[i] - q[i])^2))                    │   │
   │     │          candidates.push((id, dist))                           │   │
   │     │                                                                │   │
   │     │  candidates.sort_by_distance()                                 │   │
   │     │  return candidates.truncate(100)                               │   │
   │     └───────────────────────────────────────────────────────────────┘   │
   │                                                                          │
   └──────────────────────────────────┬──────────────────────────────────────┘
   ┌─────────────────────────────────────────────────────────────────────────┐
   │  4. MERGE RESULTS                                                        │
   │                                                                          │
   │     all_results = flatten(worker_responses)                             │
   │     all_results.sort_by(|a, b| a.distance.cmp(&b.distance))             │
   │     return all_results.truncate(top_k)                                  │
   │                                                                          │
   │     → [(id: 1001, dist: 0.023), (id: 4521, dist: 0.031), ...]           │
   │                                                                          │
   └─────────────────────────────────────────────────────────────────────────┘

   Code: src/service.rs:44-56, 244-300
```

---

## Rebalancer

Autonomous background worker that splits oversized buckets:

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              REBALANCER LOOP                                 │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│     Every 500ms:                                                            │
│                                                                             │
│     1. refresh_sizes()                                                      │
│        └── For each bucket: count = wal.get_topic_entry_count(topic)       │
│                                                                             │
│     2. Find largest bucket                                                  │
│        └── max_bucket = argmax(bucket_sizes)                               │
│                                                                             │
│     3. If max_size > threshold (default: 2000):                            │
│                                                                             │
│        ┌─────────────────────────────────────────────────────────────┐     │
│        │                    SPLIT PROCESS                             │     │
│        ├─────────────────────────────────────────────────────────────┤     │
│        │                                                              │     │
│        │  a) Acquire bucket lock (serialize with workers)            │     │
│        │                                                              │     │
│        │  b) Load vectors from Walrus                                 │     │
│        │     chunks = Storage.get_chunks(bucket_id)                  │     │
│        │     vectors = decode_all_chunks(chunks)                     │     │
│        │                                                              │     │
│        │  c) K-means split (k=2)                                      │     │
│        │     ┌──────────────────────────────────────────────────┐    │     │
│        │     │  Init: farthest-pair centroids                   │    │     │
│        │     │  Iterate: max 8 iterations                       │    │     │
│        │     │    • Assign vectors to nearest centroid          │    │     │
│        │     │    • Recompute centroids                          │    │     │
│        │     │  Output: 2 new buckets with vectors               │    │     │
│        │     └──────────────────────────────────────────────────┘    │     │
│        │                                                              │     │
│        │  d) Persist new buckets                                      │     │
│        │     new_id_1 = allocate_bucket_id()                         │     │
│        │     new_id_2 = allocate_bucket_id()                         │     │
│        │     Storage.put_chunk(new_id_1, vectors_1)                  │     │
│        │     Storage.put_chunk(new_id_2, vectors_2)                  │     │
│        │                                                              │     │
│        │  e) Update BucketIndex for all moved vectors                │     │
│        │     bucket_index.put_batch(new_id_1, vector_ids_1)          │     │
│        │     bucket_index.put_batch(new_id_2, vector_ids_2)          │     │
│        │                                                              │     │
│        │  f) Retire old bucket                                        │     │
│        │     centroids.remove(old_bucket_id)                         │     │
│        │     Storage.put_bucket_meta(Retired)                        │     │
│        │                                                              │     │
│        │  g) Rebuild router with new centroids                       │     │
│        │     rebuild_router([old_id, new_id_1, new_id_2])            │     │
│        │                                                              │     │
│        │  h) Checkpoint old bucket (mark blocks as reclaimable)      │     │
│        │                                                              │     │
│        └─────────────────────────────────────────────────────────────┘     │
│                                                                             │
│     Else: sleep 500ms                                                       │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

   Threshold: SATORI_REBALANCE_THRESHOLD env var (default: 2000)

   Code: src/rebalancer.rs:497-545 (loop), 259-359 (split)
```

---

## Algorithms

### HNSW Router Index

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                               HNSW Structure                                 │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Layer 3:          ●───────────────────────────────●                       │
│                     │                               │                        │
│   Layer 2:          ●───────────●───────────────────●                       │
│                     │           │                   │                        │
│   Layer 1:          ●───●───────●───────●───────────●───●                   │
│                     │   │       │       │           │   │                    │
│   Layer 0:    ●─────●───●───●───●───●───●───●───●───●───●───●───●           │
│               ↑                                                             │
│             entry                                                           │
│             point                                                           │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Parameters (src/router.rs:30):                                            │
│     M = 24                  (max neighbors per node, layers > 0)           │
│     M0 = 48                 (max neighbors at layer 0)                      │
│     ef_construction = 180   (beam width during insertion)                   │
│                                                                             │
│   Query Parameters (src/router.rs:102-124):                                 │
│     ef_search scales with index size and top_k:                            │
│       • top_k=1:   64-128 (fast path for insert routing)                   │
│       • top_k>1:   1200-4000 (quality-focused for queries)                 │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Quantization (src/quantizer.rs):                                          │
│                                                                             │
│     f32 → u8 scalar quantization:                                          │
│       quantized = clamp((value - min) * 255 / (max - min), 0, 255)         │
│                                                                             │
│     Bounds computed with 0.1% padding to handle edge values                 │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Distance (src/router_hnsw.rs):                                            │
│                                                                             │
│     Cosine similarity over centered i8 vectors:                            │
│       centered[i] = u8[i] ^ 0x80   (convert u8 to signed)                  │
│       dist = 1 - dot(a, b) / (norm(a) * norm(b))                           │
│                                                                             │
│     SIMD: AVX2, AVX-512 kernels (src/router_hnsw.rs:722-871)               │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

### K-Means Clustering (Indexer)

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                            K-MEANS CLUSTERING                                │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Used for:                                                                  │
│     • Initial cluster formation: build_clusters(vectors, k)                │
│     • Bucket splitting: split_bucket_once(bucket) → 2 buckets              │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Initialization (for k=2 splits):                                          │
│                                                                             │
│     Farthest-pair method:                                                   │
│       1. c1 = vectors[0]                                                   │
│       2. c2 = argmax_v( distance(v, c1) )                                  │
│       3. c1 = argmax_v( distance(v, c2) )     (refine)                     │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Iteration (max 8 for splits, max 20 for initial build):                  │
│                                                                             │
│     for iter in 0..max_iters:                                               │
│       1. Assign each vector to nearest centroid                            │
│       2. Recompute centroids as mean of assigned vectors                   │
│       3. If no assignments changed → converged, break                      │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   SIMD Acceleration (src/indexer.rs:392-586):                              │
│                                                                             │
│     • k=2 special kernel: compute both distances in single pass            │
│     • k>=8 block kernel: process 8 centroids at once (SoA layout)          │
│     • AVX2 + FMA for L2 distance computation                               │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Fallback (src/indexer.rs:217-243):                                        │
│                                                                             │
│     If k=2 and one cluster ends up empty → split vectors in half           │
│     (guarantees split always produces 2 non-empty buckets)                 │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

---

## Recovery

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              STARTUP RECOVERY                                │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   1. Router Manager Recovery (src/router_manager.rs:364-391):              │
│                                                                             │
│      ┌────────────────────────────────────────────────────────────────┐    │
│      │  a) Load latest router_snapshot from Walrus                    │    │
│      │     → Recovers: bucket metadata (id, centroid, count)         │    │
│      │     → Also stores: updates_offset (how far updates were read) │    │
│      │                                                                │    │
│      │  b) Replay router_updates from updates_offset                  │    │
│      │     → Applies incremental centroid changes since snapshot     │    │
│      │                                                                │    │
│      │  c) Rebuild HNSW router from recovered centroids               │    │
│      └────────────────────────────────────────────────────────────────┘    │
│                                                                             │
│   2. Workers: Start with empty caches                                       │
│      → Bucket data loaded on-demand from Walrus topics                     │
│                                                                             │
│   3. RocksDB indexes: Just reopen (already durable)                        │
│      → VectorIndex, BucketIndex                                            │
│                                                                             │
│   4. Rebalancer: Refresh sizes from Walrus topic entry counts              │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

---

## Thread Model

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              THREAD ARCHITECTURE                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   ┌─────────────────────────────────────────────────────────────────────┐  │
│   │  Main Thread                                                         │  │
│   │    • Creates SatoriDb                                                │  │
│   │    • Holds SatoriHandle for API calls                               │  │
│   └─────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
│   ┌─────────────────────────────────────────────────────────────────────┐  │
│   │  Router Manager Thread ("router-mgr")                               │  │
│   │    • Single thread, blocking channel receive                        │  │
│   │    • Handles: Query, RouteOrInit, ApplyUpsert, Flush, Stats        │  │
│   │    • Channel: crossbeam unbounded                                   │  │
│   └─────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
│   ┌─────────────────────────────────────────────────────────────────────┐  │
│   │  Worker Threads ("worker-0", "worker-1", ..., "worker-N")          │  │
│   │    • N = num_cpus (configurable via SatoriDbConfig.workers)        │  │
│   │    • Each runs Glommio LocalExecutor, CPU-pinned                   │  │
│   │    • Channel: async_channel bounded(1000)                          │  │
│   │    • Concurrency limit: 32 parallel query tasks per worker         │  │
│   └─────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
│   ┌─────────────────────────────────────────────────────────────────────┐  │
│   │  Rebalancer Thread ("rebalance-loop")                               │  │
│   │    • Glommio LocalExecutor (optional CPU pin)                      │  │
│   │    • Autonomous monitoring loop                                     │  │
│   │    • Delete command channel: async_channel bounded(1024)           │  │
│   └─────────────────────────────────────────────────────────────────────┘  │
│                                                                             │
│   Synchronization:                                                          │
│     • BucketLocks: per-bucket async Mutex (workers ↔ rebalancer)          │
│     • RoutingTable: Arc<RwLock> for sharing router snapshots              │
│     • Channels: message passing for all commands                          │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
```

---

## Configuration

| Variable | Default | Description |
|----------|---------|-------------|
| `SATORI_REBALANCE_THRESHOLD` | 2000 | Split bucket when vector count exceeds |
| `SATORI_ROUTER_REBUILD_EVERY` | 1000 | Rebuild HNSW after N updates |
| `SATORI_WORKER_CACHE_BUCKETS` | 64 | Max buckets in worker LRU cache |
| `SATORI_WORKER_CACHE_BUCKET_MB` | 64 | Max MB per cached bucket |
| `SATORI_VECTOR_INDEX_PATH` | temp dir | Path for VectorIndex RocksDB |
| `SATORI_BUCKET_INDEX_PATH` | temp dir | Path for BucketIndex RocksDB |
| `WALRUS_DATA_DIR` | `./wal_files` | Walrus storage directory |
| `WALRUS_QUIET` | unset | Suppress Walrus debug output |