allframe 0.1.28

Complete Rust web framework with built-in HTTP/2 server, REST/GraphQL/gRPC, compile-time DI, CQRS - TDD from day zero
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# PRD: AllSource Cloud-Ready Evolution

**Title**: AllSource - Cloud-Native Event Store for Serverless and Multi-Tenant Architectures
**Version**: 1.0
**Status**: Draft
**Priority**: P0
**Author**: AllFrame Team
**Created**: 2025-11-27
**Last Updated**: 2025-11-27

---

## Table of Contents

1. [Executive Summary]#1-executive-summary
2. [Current State Analysis]#2-current-state-analysis
3. [Goals & Non-Goals]#3-goals--non-goals
4. [Market Context]#4-market-context
5. [Technical Requirements]#5-technical-requirements
6. [Architecture]#6-architecture
7. [Cloud Backend Implementations]#7-cloud-backend-implementations
8. [Multi-Tenancy Design]#8-multi-tenancy-design
9. [Serverless Optimization]#9-serverless-optimization
10. [API Design]#10-api-design
11. [Migration Path]#11-migration-path
12. [Timeline & Milestones]#12-timeline--milestones
13. [Success Criteria]#13-success-criteria
14. [Risks & Mitigations]#14-risks--mitigations
15. [Appendix]#appendix

---

## 1. Executive Summary

AllSource is the embedded event store powering AllFrame's CQRS+ES infrastructure. This PRD defines the evolution from a local-first embedded database to a **cloud-native, serverless-ready, multi-tenant event store** that can run on AWS, GCP, and self-hosted environments.

### Vision

**AllSource becomes the unified event store abstraction** that:
- Runs embedded for local development (current capability)
- Deploys to DynamoDB/Firestore for serverless workloads
- Scales horizontally for multi-tenant SaaS
- Maintains the same API regardless of backend

### Key Outcomes

| Outcome | Metric |
|---------|--------|
| Cloud-native deployment | Works on Lambda + DynamoDB |
| Multi-tenant support | Row-level isolation, per-tenant queries |
| Zero code changes | Same EventStore API across all backends |
| Sub-10ms latency | Matches current AllSource performance |
| Cost efficiency | Pay-per-request pricing on serverless |

---

## 2. Current State Analysis

### 2.1 What AllSource Has Today

| Feature | Status | Notes |
|---------|--------|-------|
| Event ingestion || 469K events/sec |
| Event querying || 11.9μs p99 latency |
| Parquet storage || Columnar format |
| Write-Ahead Log || Durability guarantee |
| Snapshots || Aggregate state caching |
| PostgreSQL backend | ⚠️ | Compilation errors |
| RocksDB backend | ⚠️ | Compilation errors |
| Multi-tenant queries | ⚠️ | Trait defined, not implemented |

### 2.2 Current Issues (Blocking)

From `docs/current/ALLSOURCE_CORE_ISSUES.md`:

1. **Missing trait implementations**:
   - `get_streams_by_tenant()` not implemented
   - `count_streams_by_tenant()` not implemented

2. **API inconsistencies**:
   - `expected_version()` getter missing from EventStream

3. **Error handling gaps**:
   - `From<sqlx::Error>` not implemented for AllSourceError

### 2.3 What's Missing for Cloud

| Gap | Impact |
|-----|--------|
| DynamoDB backend | Cannot deploy on AWS serverless |
| Firestore backend | Cannot deploy on GCP serverless |
| S3/GCS archival | No cold storage tier |
| Multi-tenant isolation | SaaS platforms blocked |
| Connection pooling | Lambda cold start overhead |
| Serverless-optimized queries | Scan-heavy, expensive |

---

## 3. Goals & Non-Goals

### Goals

| Goal | Success Metric |
|------|----------------|
| Fix compilation errors | `cargo build --all-features` succeeds |
| DynamoDB backend | Full EventStoreBackend implementation |
| Firestore backend | Full EventStoreBackend implementation |
| Multi-tenant support | Tenant isolation in all backends |
| S3/GCS archival | Cold storage for old events |
| Serverless optimization | < 50ms cold start contribution |
| Connection reuse | Pool connections across invocations |

### Non-Goals

- **Global replication**: Multi-region is future phase
- **Real-time streaming**: EventBridge/Pub-Sub handles this
- **GraphQL API**: AllFrame Router provides this
- **Custom query language**: Use backend-native queries
- **ACID across aggregates**: Event sourcing is eventually consistent

---

## 4. Market Context

### 4.1 Event Store Landscape 2025

| Solution | Type | Serverless | Multi-Tenant | Rust SDK |
|----------|------|------------|--------------|----------|
| EventStoreDB | Dedicated ||||
| DynamoDB | Managed ||||
| Firestore | Managed ||| ⚠️ |
| Kafka | Streaming ||||
| **AllSource** | Embedded + Cloud ||||

Sources: [AWS CQRS Event Store](https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/), [EventStoreDB Cloud](https://aws.amazon.com/marketplace/pp/prodview-kxo6grvoovk2y)

### 4.2 Why DynamoDB for Serverless Event Sourcing

> "DynamoDB offers performance benefits associated with NoSQL technology. An event store design is not characterized by joins between tables or relations, which makes it an ideal candidate for DynamoDB." — [AWS Database Blog]https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/

> "MongoDB needs to keep its working set in RAM for acceptable performance, which makes it too expensive for event sourcing. In contrast, DynamoDB's great performance is not dependent on the amount of data stored." — [The Mill Adventure]https://aws.amazon.com/blogs/architecture/how-the-mill-adventure-implemented-event-sourcing-at-scale-using-dynamodb/

### 4.3 Multi-Tenant Patterns

> "For pooled or bridge models, the most battle-tested pattern is Postgres with Row Level Security and a strict tenant context." — [Multi-Tenant SaaS Architecture 2025]https://isitdev.com/multi-tenant-saas-architecture-cloud-2025/

AllSource should support:
- **Pooled**: Single table, tenant_id column, RLS
- **Siloed**: Separate tables per tenant
- **Bridge**: Shared infrastructure, isolated data

---

## 5. Technical Requirements

### TR-1: Fix Existing Compilation Errors

**Priority**: P0 (Blocking)

**Requirements**:
1. Implement `get_streams_by_tenant()` for PostgreSQL and RocksDB backends
2. Implement `count_streams_by_tenant()` for PostgreSQL and RocksDB backends
3. Add `expected_version()` getter to EventStream
4. Implement `From<sqlx::Error>` for AllSourceError

**Acceptance Criteria**:
- `cargo build --all-features` succeeds
- All existing tests pass
- No new warnings

### TR-2: DynamoDB Backend

**Priority**: P0

**Requirements**:
1. Implement `EventStoreBackend` for DynamoDB
2. Single-table design with PK (stream_id) and SK (version)
3. Optimistic concurrency via conditional writes
4. DynamoDB Streams integration for projections
5. Auto-scaling configuration
6. GSI for tenant-based queries

**Schema**:
```
Table: {prefix}_events
├── PK (String): {tenant_id}#{stream_id}
├── SK (String): {version:020}  # Zero-padded for sorting
├── event_type (String): Discriminator
├── payload (String): JSON event data
├── timestamp (String): ISO 8601
├── metadata (Map): Custom metadata
└── ttl (Number): Optional expiration

GSI: by_tenant
├── PK: tenant_id
├── SK: timestamp
└── Projects: stream_id, event_type
```

### TR-3: Firestore Backend

**Priority**: P1

**Requirements**:
1. Implement `EventStoreBackend` for Firestore
2. Collection hierarchy: tenants/{tenant}/streams/{stream}/events/{version}
3. Firestore transactions for optimistic concurrency
4. Firestore triggers for projections
5. Security rules for tenant isolation

**Schema**:
```
Collection: tenants
└── Document: {tenant_id}
    └── Collection: streams
        └── Document: {stream_id}
            ├── current_version: number
            ├── created_at: timestamp
            └── Collection: events
                └── Document: {version}
                    ├── event_type: string
                    ├── payload: map
                    ├── timestamp: timestamp
                    └── metadata: map
```

### TR-4: S3/GCS Archival Backend

**Priority**: P1

**Requirements**:
1. Archive old events to object storage
2. Configurable retention policies
3. Lazy loading for archived events
4. Parquet format for analytics
5. Lifecycle rules integration

**Archive Strategy**:
```
Hot tier (DynamoDB/Firestore): Last 30 days
Warm tier (S3/GCS): 30 days - 1 year
Cold tier (S3 Glacier/GCS Archive): > 1 year
```

### TR-5: Multi-Tenant Support

**Priority**: P0

**Requirements**:
1. Tenant context injection at request level
2. Automatic tenant_id in all queries
3. Cross-tenant query prevention
4. Per-tenant statistics
5. Tenant-based rate limiting (optional)

**API**:
```rust
// Tenant context
let store = EventStore::with_tenant("tenant-123");

// All operations scoped to tenant
store.append("order-1", events).await?;  // Stored as tenant-123#order-1
store.get_events("order-1").await?;      // Only returns tenant-123's events

// Admin operations (cross-tenant)
let admin_store = EventStore::admin();
admin_store.get_streams_by_tenant("tenant-123").await?;
admin_store.count_streams_by_tenant("tenant-123").await?;
```

### TR-6: Connection Pooling for Lambda

**Priority**: P0

**Requirements**:
1. Reuse connections across Lambda invocations
2. Lazy initialization (don't connect until needed)
3. Connection keep-alive configuration
4. Graceful connection recovery
5. Metrics for connection reuse rate

**Implementation**:
```rust
// Static connection pool (lives across invocations)
static POOL: OnceCell<DynamoDBPool> = OnceCell::new();

pub async fn get_connection() -> &'static DynamoDBPool {
    POOL.get_or_init(|| async {
        let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
        DynamoDBPool::new(&config)
    }).await
}
```

### TR-7: Serverless Query Optimization

**Priority**: P1

**Requirements**:
1. Avoid full table scans
2. Use GSI for common access patterns
3. Projection expressions (return only needed fields)
4. Pagination with consistent reads
5. Query cost estimation

**Access Patterns**:
| Pattern | Solution |
|---------|----------|
| Get events for stream | Query by PK |
| Get events after version | Query by PK + SK >= version |
| Get streams for tenant | GSI query by tenant_id |
| Count streams for tenant | GSI count (or cached) |
| Get recent events | GSI by timestamp |

---

## 6. Architecture

### 6.1 High-Level Architecture

```
┌─────────────────────────────────────────────────────────────────────────┐
│                         AllSource Cloud Architecture                    │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  ┌─────────────────┐                                                    │
│  │   AllFrame      │                                                    │
│  │   Application   │                                                    │
│  └────────┬────────┘                                                    │
│           │                                                              │
│           ▼                                                              │
│  ┌─────────────────┐                                                    │
│  │  EventStore<E>  │  Unified API                                       │
│  │  with_backend() │                                                    │
│  └────────┬────────┘                                                    │
│           │                                                              │
│           ▼                                                              │
│  ┌─────────────────────────────────────────────────────────────────┐   │
│  │                    EventStoreBackend Trait                       │   │
│  └─────────────────────────────────────────────────────────────────┘   │
│           │                                                              │
│     ┌─────┼─────┬─────────┬─────────┬─────────┬─────────┐              │
│     ▼     ▼     ▼         ▼         ▼         ▼         ▼              │
│  ┌─────┐┌─────┐┌───────┐┌───────┐┌───────┐┌───────┐┌───────┐          │
│  │InMem││AllSrc││DynamoDB││Firestore││Postgres││RocksDB││ S3/GCS │     │
│  │     ││Core ││       ││        ││       ││      ││Archive│          │
│  └─────┘└─────┘└───────┘└────────┘└───────┘└──────┘└───────┘          │
│     │      │       │         │         │        │        │              │
│     ▼      ▼       ▼         ▼         ▼        ▼        ▼              │
│  ┌─────┐┌─────┐┌───────┐┌───────┐┌───────┐┌──────┐┌───────┐           │
│  │ RAM ││Parquet││ AWS  ││  GCP  ││  SQL  ││ LSM  ││Object │           │
│  │     ││+ WAL ││      ││       ││       ││ Tree ││Storage│           │
│  └─────┘└─────┘└───────┘└───────┘└───────┘└──────┘└───────┘           │
│                                                                          │
│  Local Development  │  Serverless Cloud  │  Self-Hosted  │  Archival   │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘
```

### 6.2 Crate Structure

```
allsource/
├── allsource-core/                 # Current - Fix and enhance
│   ├── src/
│   │   ├── lib.rs
│   │   ├── domain/
│   │   │   ├── entities/
│   │   │   │   └── event_stream.rs  # Add expected_version() getter
│   │   │   └── value_objects/
│   │   │       └── tenant_id.rs
│   │   ├── infrastructure/
│   │   │   └── repositories/
│   │   │       ├── postgres_event_stream_repository.rs  # Fix implementations
│   │   │       └── rocksdb_event_stream_repository.rs   # Fix implementations
│   │   └── error.rs                 # Add From<sqlx::Error>
│   └── Cargo.toml
│
├── allsource-dynamodb/             # NEW - AWS DynamoDB backend
│   ├── src/
│   │   ├── lib.rs
│   │   ├── backend.rs              # EventStoreBackend impl
│   │   ├── schema.rs               # Table schema definitions
│   │   ├── queries.rs              # Optimized query builders
│   │   └── streams.rs              # DynamoDB Streams integration
│   └── Cargo.toml
│
├── allsource-firestore/            # NEW - GCP Firestore backend
│   ├── src/
│   │   ├── lib.rs
│   │   ├── backend.rs              # EventStoreBackend impl
│   │   ├── schema.rs               # Collection schema
│   │   └── triggers.rs             # Firestore triggers
│   └── Cargo.toml
│
├── allsource-archive/              # NEW - S3/GCS archival
│   ├── src/
│   │   ├── lib.rs
│   │   ├── s3.rs                   # S3 backend
│   │   ├── gcs.rs                  # GCS backend
│   │   ├── parquet.rs              # Parquet serialization
│   │   └── lifecycle.rs            # Retention policies
│   └── Cargo.toml
│
└── allsource-tenant/               # NEW - Multi-tenancy support
    ├── src/
    │   ├── lib.rs
    │   ├── context.rs              # Tenant context
    │   ├── isolation.rs            # Isolation strategies
    │   └── metrics.rs              # Per-tenant metrics
    └── Cargo.toml
```

### 6.3 Feature Flags

```toml
# allframe-core/Cargo.toml
[features]
# Current
cqrs = ["allframe-macros"]
cqrs-allsource = ["cqrs", "allsource-core"]
cqrs-postgres = ["cqrs-allsource", "allsource-core/postgres"]
cqrs-rocksdb = ["cqrs-allsource", "allsource-core/rocksdb"]

# New cloud backends
cqrs-dynamodb = ["cqrs", "allsource-dynamodb"]
cqrs-firestore = ["cqrs", "allsource-firestore"]

# Archival
cqrs-archive-s3 = ["cqrs", "allsource-archive/s3"]
cqrs-archive-gcs = ["cqrs", "allsource-archive/gcs"]

# Multi-tenancy
cqrs-tenant = ["cqrs", "allsource-tenant"]

# Convenience bundles
cqrs-aws = ["cqrs-dynamodb", "cqrs-archive-s3"]
cqrs-gcp = ["cqrs-firestore", "cqrs-archive-gcs"]
cqrs-cloud = ["cqrs-aws", "cqrs-gcp"]
```

---

## 7. Cloud Backend Implementations

### 7.1 DynamoDB Backend

```rust
// allsource-dynamodb/src/backend.rs

use allframe_core::cqrs::{Event, EventStoreBackend, BackendError, BackendStats};
use aws_sdk_dynamodb::{Client, types::AttributeValue};
use async_trait::async_trait;

pub struct DynamoDBBackend<E> {
    client: Client,
    table_name: String,
    tenant_id: Option<String>,
    _phantom: std::marker::PhantomData<E>,
}

impl<E> DynamoDBBackend<E> {
    /// Create backend for single-tenant use
    pub async fn new(table_name: impl Into<String>) -> Result<Self, BackendError> {
        let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
        Ok(Self {
            client: Client::new(&config),
            table_name: table_name.into(),
            tenant_id: None,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Create backend scoped to a specific tenant
    pub async fn with_tenant(
        table_name: impl Into<String>,
        tenant_id: impl Into<String>,
    ) -> Result<Self, BackendError> {
        let mut backend = Self::new(table_name).await?;
        backend.tenant_id = Some(tenant_id.into());
        Ok(backend)
    }

    /// Build partition key with optional tenant prefix
    fn build_pk(&self, stream_id: &str) -> String {
        match &self.tenant_id {
            Some(tenant) => format!("{}#{}", tenant, stream_id),
            None => stream_id.to_string(),
        }
    }

    /// Build sort key from version
    fn build_sk(version: u64) -> String {
        format!("{:020}", version)
    }
}

#[async_trait]
impl<E> EventStoreBackend<E> for DynamoDBBackend<E>
where
    E: Event + serde::Serialize + serde::de::DeserializeOwned + Send + Sync,
{
    async fn append(
        &self,
        stream_id: &str,
        events: Vec<E>,
        expected_version: Option<u64>,
    ) -> Result<u64, BackendError> {
        let pk = self.build_pk(stream_id);

        // Get current version
        let current_version = self.get_current_version(stream_id).await?;

        // Optimistic concurrency check
        if let Some(expected) = expected_version {
            if current_version != expected {
                return Err(BackendError::ConcurrencyConflict {
                    expected,
                    actual: current_version,
                });
            }
        }

        // Batch write events
        let mut version = current_version;
        for event in events {
            version += 1;
            let sk = Self::build_sk(version);

            let payload = serde_json::to_string(&event)
                .map_err(|e| BackendError::Serialization(e.to_string()))?;

            self.client
                .put_item()
                .table_name(&self.table_name)
                .item("PK", AttributeValue::S(pk.clone()))
                .item("SK", AttributeValue::S(sk))
                .item("payload", AttributeValue::S(payload))
                .item("timestamp", AttributeValue::S(chrono::Utc::now().to_rfc3339()))
                .item("event_type", AttributeValue::S(std::any::type_name::<E>().to_string()))
                // Conditional write for optimistic concurrency
                .condition_expression("attribute_not_exists(PK) AND attribute_not_exists(SK)")
                .send()
                .await
                .map_err(|e| BackendError::Storage(e.to_string()))?;
        }

        Ok(version)
    }

    async fn get_events(&self, stream_id: &str) -> Result<Vec<E>, BackendError> {
        let pk = self.build_pk(stream_id);

        let result = self
            .client
            .query()
            .table_name(&self.table_name)
            .key_condition_expression("PK = :pk")
            .expression_attribute_values(":pk", AttributeValue::S(pk))
            .send()
            .await
            .map_err(|e| BackendError::Storage(e.to_string()))?;

        let events = result
            .items
            .unwrap_or_default()
            .into_iter()
            .filter_map(|item| {
                item.get("payload")
                    .and_then(|v| v.as_s().ok())
                    .and_then(|s| serde_json::from_str(s).ok())
            })
            .collect();

        Ok(events)
    }

    async fn get_events_after(
        &self,
        stream_id: &str,
        after_version: u64,
    ) -> Result<Vec<E>, BackendError> {
        let pk = self.build_pk(stream_id);
        let sk_start = Self::build_sk(after_version + 1);

        let result = self
            .client
            .query()
            .table_name(&self.table_name)
            .key_condition_expression("PK = :pk AND SK >= :sk")
            .expression_attribute_values(":pk", AttributeValue::S(pk))
            .expression_attribute_values(":sk", AttributeValue::S(sk_start))
            .send()
            .await
            .map_err(|e| BackendError::Storage(e.to_string()))?;

        let events = result
            .items
            .unwrap_or_default()
            .into_iter()
            .filter_map(|item| {
                item.get("payload")
                    .and_then(|v| v.as_s().ok())
                    .and_then(|s| serde_json::from_str(s).ok())
            })
            .collect();

        Ok(events)
    }

    async fn get_all_events(&self) -> Result<Vec<E>, BackendError> {
        // For tenant-scoped backend, scan with tenant prefix
        // For global backend, full scan (expensive, use sparingly)
        let mut events = Vec::new();
        let mut last_key = None;

        loop {
            let mut scan = self.client.scan().table_name(&self.table_name);

            if let Some(tenant) = &self.tenant_id {
                scan = scan
                    .filter_expression("begins_with(PK, :tenant)")
                    .expression_attribute_values(":tenant", AttributeValue::S(format!("{}#", tenant)));
            }

            if let Some(key) = last_key {
                scan = scan.set_exclusive_start_key(Some(key));
            }

            let result = scan.send().await.map_err(|e| BackendError::Storage(e.to_string()))?;

            for item in result.items.unwrap_or_default() {
                if let Some(payload) = item.get("payload").and_then(|v| v.as_s().ok()) {
                    if let Ok(event) = serde_json::from_str(payload) {
                        events.push(event);
                    }
                }
            }

            last_key = result.last_evaluated_key;
            if last_key.is_none() {
                break;
            }
        }

        Ok(events)
    }

    async fn flush(&self) -> Result<(), BackendError> {
        // DynamoDB is immediately consistent, no flush needed
        Ok(())
    }

    async fn stats(&self) -> BackendStats {
        BackendStats {
            total_events: 0, // Would require scan
            total_aggregates: 0,
            total_snapshots: 0,
            backend_specific: [
                ("backend_type".to_string(), "dynamodb".to_string()),
                ("table_name".to_string(), self.table_name.clone()),
            ]
            .into(),
        }
    }
}
```

### 7.2 Multi-Tenant Queries

```rust
// allsource-dynamodb/src/tenant.rs

impl<E> DynamoDBBackend<E>
where
    E: Event + serde::Serialize + serde::de::DeserializeOwned + Send + Sync,
{
    /// Get all streams for a tenant (admin operation)
    pub async fn get_streams_by_tenant(&self, tenant_id: &str) -> Result<Vec<String>, BackendError> {
        let result = self
            .client
            .query()
            .table_name(&self.table_name)
            .index_name("by_tenant")
            .key_condition_expression("tenant_id = :tenant")
            .expression_attribute_values(":tenant", AttributeValue::S(tenant_id.to_string()))
            .projection_expression("stream_id")
            .send()
            .await
            .map_err(|e| BackendError::Storage(e.to_string()))?;

        let streams: Vec<String> = result
            .items
            .unwrap_or_default()
            .into_iter()
            .filter_map(|item| {
                item.get("stream_id")
                    .and_then(|v| v.as_s().ok())
                    .map(|s| s.to_string())
            })
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();

        Ok(streams)
    }

    /// Count streams for a tenant (admin operation)
    pub async fn count_streams_by_tenant(&self, tenant_id: &str) -> Result<usize, BackendError> {
        let streams = self.get_streams_by_tenant(tenant_id).await?;
        Ok(streams.len())
    }
}
```

---

## 8. Multi-Tenancy Design

### 8.1 Isolation Strategies

| Strategy | Use Case | Pros | Cons |
|----------|----------|------|------|
| **Pooled** | SaaS, cost-sensitive | Single table, easy ops | Noisy neighbor risk |
| **Siloed** | Enterprise, regulated | Full isolation | Higher ops cost |
| **Bridge** | Hybrid | Flexible | Complex |

### 8.2 Pooled Multi-Tenancy (Recommended)

```rust
// Partition key includes tenant
PK: "{tenant_id}#{stream_id}"
SK: "{version:020}"

// GSI for tenant queries
GSI: by_tenant
  PK: tenant_id
  SK: created_at

// Access pattern: Always scope by tenant
let store = EventStore::with_tenant("tenant-123");
store.get_events("order-1").await?;  // Queries "tenant-123#order-1"
```

### 8.3 Tenant Context Middleware

```rust
// allsource-tenant/src/context.rs

use std::sync::Arc;
use tokio::sync::RwLock;

/// Thread-local tenant context for request scoping
#[derive(Clone)]
pub struct TenantContext {
    tenant_id: Arc<RwLock<Option<String>>>,
}

impl TenantContext {
    pub fn new() -> Self {
        Self {
            tenant_id: Arc::new(RwLock::new(None)),
        }
    }

    /// Set tenant for current request
    pub async fn set_tenant(&self, tenant_id: impl Into<String>) {
        *self.tenant_id.write().await = Some(tenant_id.into());
    }

    /// Get current tenant
    pub async fn get_tenant(&self) -> Option<String> {
        self.tenant_id.read().await.clone()
    }

    /// Clear tenant (end of request)
    pub async fn clear(&self) {
        *self.tenant_id.write().await = None;
    }
}

/// Middleware for Lambda/Cloud Run
pub async fn tenant_middleware<F, Fut, R>(
    tenant_id: &str,
    ctx: TenantContext,
    f: F,
) -> R
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = R>,
{
    ctx.set_tenant(tenant_id).await;
    let result = f().await;
    ctx.clear().await;
    result
}
```

### 8.4 Row-Level Security (PostgreSQL)

```sql
-- Enable RLS
ALTER TABLE events ENABLE ROW LEVEL SECURITY;

-- Create policy
CREATE POLICY tenant_isolation ON events
    USING (tenant_id = current_setting('app.tenant_id')::uuid);

-- Set tenant context
SET app.tenant_id = 'tenant-123';

-- All queries now filtered by tenant
SELECT * FROM events WHERE stream_id = 'order-1';
-- Automatically adds: AND tenant_id = 'tenant-123'
```

---

## 9. Serverless Optimization

### 9.1 Cold Start Optimization

| Technique | Impact | Implementation |
|-----------|--------|----------------|
| Lazy init | -50ms | Don't connect until first query |
| Static pool | -30ms | Reuse connections across invocations |
| ARM64 | -20ms | Use Graviton processors |
| Minimal deps | -10ms | Feature flags, no unused code |

### 9.2 Connection Reuse Pattern

```rust
// Static connection pool for Lambda
use once_cell::sync::OnceCell;
use tokio::sync::OnceCell as AsyncOnceCell;

static CLIENT: AsyncOnceCell<Client> = AsyncOnceCell::const_new();

async fn get_client() -> &'static Client {
    CLIENT
        .get_or_init(|| async {
            let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
            Client::new(&config)
        })
        .await
}
```

### 9.3 Query Cost Optimization

```rust
// Projection expressions - return only needed fields
.projection_expression("payload, timestamp")

// Consistent vs eventual reads
.consistent_read(false)  // Faster, cheaper for most cases

// Pagination for large result sets
.limit(100)
.exclusive_start_key(last_key)
```

### 9.4 Binary Size Optimization

```toml
# Cargo.toml
[profile.release]
opt-level = "z"      # Size optimization
lto = true           # Link-time optimization
codegen-units = 1    # Better optimization
strip = true         # Remove symbols
panic = "abort"      # Smaller panic handling

[profile.release.package."*"]
opt-level = "z"
```

**Target**: < 5 MB for Lambda deployment

---

## 10. API Design

### 10.1 Unified EventStore API

```rust
/// EventStore with pluggable backends and multi-tenancy
pub struct EventStore<E: Event, B: EventStoreBackend<E>> {
    backend: Arc<B>,
    tenant_id: Option<String>,
}

impl<E: Event, B: EventStoreBackend<E>> EventStore<E, B> {
    /// Create with backend
    pub fn with_backend(backend: B) -> Self;

    /// Create with backend and tenant scope
    pub fn with_tenant(backend: B, tenant_id: impl Into<String>) -> Self;

    /// Append events to stream
    pub async fn append(&self, stream_id: &str, events: Vec<E>) -> Result<u64, StoreError>;

    /// Append with optimistic concurrency
    pub async fn append_with_version(
        &self,
        stream_id: &str,
        events: Vec<E>,
        expected_version: u64,
    ) -> Result<u64, StoreError>;

    /// Get all events for stream
    pub async fn get_events(&self, stream_id: &str) -> Result<Vec<E>, StoreError>;

    /// Get events after version
    pub async fn get_events_after(&self, stream_id: &str, version: u64) -> Result<Vec<E>, StoreError>;

    /// Save snapshot
    pub async fn save_snapshot<A: Aggregate<Event = E>>(
        &self,
        aggregate_id: &str,
        aggregate: &A,
        version: u64,
    ) -> Result<(), StoreError>;

    /// Load snapshot
    pub async fn get_snapshot<A: Aggregate<Event = E>>(
        &self,
        aggregate_id: &str,
    ) -> Result<Option<(A, u64)>, StoreError>;

    /// Get backend statistics
    pub async fn stats(&self) -> BackendStats;

    /// Flush pending writes (no-op for DynamoDB)
    pub async fn flush(&self) -> Result<(), StoreError>;
}
```

### 10.2 Backend Selection

```rust
// Local development - InMemory
let store = EventStore::new();

// Local development - AllSource embedded
let store = EventStore::with_backend(AllSourceBackend::new()?);

// Production - DynamoDB
let store = EventStore::with_backend(
    DynamoDBBackend::with_tenant("events-table", "tenant-123").await?
);

// Production - Firestore
let store = EventStore::with_backend(
    FirestoreBackend::with_tenant("my-project", "tenant-123").await?
);

// Self-hosted - PostgreSQL
let store = EventStore::with_backend(
    PostgresBackend::new("postgres://localhost/events").await?
);
```

---

## 11. Migration Path

### Phase 1: Fix Current Issues (Week 1)

1. Fix compilation errors in allsource-core
2. Implement missing trait methods
3. Add error conversions
4. All tests passing

### Phase 2: DynamoDB Backend (Weeks 2-4)

1. Create allsource-dynamodb crate
2. Implement EventStoreBackend
3. Add multi-tenant support
4. Integration tests with DynamoDB Local

### Phase 3: Multi-Tenancy (Weeks 5-6)

1. Create allsource-tenant crate
2. Tenant context middleware
3. Per-tenant metrics
4. Documentation

### Phase 4: Firestore Backend (Weeks 7-8)

1. Create allsource-firestore crate
2. Implement EventStoreBackend
3. Firestore triggers for projections
4. GCP integration tests

### Phase 5: Archival (Weeks 9-10)

1. Create allsource-archive crate
2. S3 backend
3. GCS backend
4. Lifecycle policies

### Phase 6: Documentation & Polish (Weeks 11-12)

1. Comprehensive documentation
2. Example applications
3. Performance benchmarks
4. Migration guides

---

## 12. Timeline & Milestones

| Phase | Duration | Deliverable |
|-------|----------|-------------|
| 1. Fix Issues | 1 week | Compilation succeeds |
| 2. DynamoDB | 3 weeks | Full EventStoreBackend |
| 3. Multi-Tenancy | 2 weeks | Tenant isolation |
| 4. Firestore | 2 weeks | GCP support |
| 5. Archival | 2 weeks | Cold storage |
| 6. Polish | 2 weeks | Docs, examples |
| **Total** | **12 weeks** | |

---

## 13. Success Criteria

### Functional

- [ ] `cargo build --all-features` succeeds
- [ ] DynamoDB backend passes all EventStoreBackend tests
- [ ] Multi-tenant queries work with isolation
- [ ] Firestore backend works on GCP
- [ ] S3/GCS archival stores and retrieves events

### Performance

| Metric | Target |
|--------|--------|
| DynamoDB append latency | < 10ms p99 |
| DynamoDB query latency | < 20ms p99 |
| Lambda cold start contribution | < 50ms |
| Binary size | < 5 MB |

### Quality

- [ ] 80%+ code coverage
- [ ] Zero unsafe code in new backends
- [ ] All public APIs documented
- [ ] No new Clippy warnings

---

## 14. Risks & Mitigations

### Risk 1: DynamoDB Costs

**Risk**: High-volume event sourcing may be expensive.

**Mitigation**:
- Use on-demand billing
- Implement archival to S3
- Document cost estimation
- Consider reserved capacity for predictable workloads

### Risk 2: Cross-Tenant Data Leak

**Risk**: Multi-tenant isolation failure.

**Mitigation**:
- Tenant ID in partition key (can't query without it)
- Integration tests for isolation
- Security review before release
- Documentation for proper usage

### Risk 3: Eventual Consistency

**Risk**: DynamoDB eventual consistency surprises users.

**Mitigation**:
- Document consistency model
- Provide consistent read option
- Use DynamoDB Streams for projections
- Strong consistency for critical reads

### Risk 4: Migration Complexity

**Risk**: Migrating from AllSource embedded to DynamoDB is hard.

**Mitigation**:
- Provide migration tool
- Document migration path
- Support dual-write during migration
- Rollback procedures

---

## Appendix

### A. DynamoDB Table Terraform

```hcl
resource "aws_dynamodb_table" "events" {
  name         = "${var.prefix}_events"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "PK"
  range_key    = "SK"

  attribute {
    name = "PK"
    type = "S"
  }

  attribute {
    name = "SK"
    type = "S"
  }

  attribute {
    name = "tenant_id"
    type = "S"
  }

  attribute {
    name = "created_at"
    type = "S"
  }

  global_secondary_index {
    name            = "by_tenant"
    hash_key        = "tenant_id"
    range_key       = "created_at"
    projection_type = "KEYS_ONLY"
  }

  stream_enabled   = true
  stream_view_type = "NEW_AND_OLD_IMAGES"

  ttl {
    attribute_name = "ttl"
    enabled        = true
  }

  tags = {
    Environment = var.environment
    Application = "allsource"
  }
}
```

### B. Firestore Security Rules

```javascript
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Tenant isolation
    match /tenants/{tenantId}/{document=**} {
      allow read, write: if request.auth != null
        && request.auth.token.tenant_id == tenantId;
    }

    // Admin access
    match /{document=**} {
      allow read, write: if request.auth != null
        && request.auth.token.admin == true;
    }
  }
}
```

### C. Dependencies

| Dependency | Version | Purpose |
|------------|---------|---------|
| aws-sdk-dynamodb | 1.55+ | DynamoDB client |
| aws-config | 1.5+ | AWS configuration |
| google-cloud-firestore | 0.4+ | Firestore client |
| aws-sdk-s3 | 1.65+ | S3 archival |
| google-cloud-storage | 0.20+ | GCS archival |
| chrono | 0.4+ | Timestamps |
| serde | 1.0+ | Serialization |
| tokio | 1.0+ | Async runtime |

### D. References

- [AWS CQRS Event Store with DynamoDB]https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/
- [The Mill Adventure Event Sourcing]https://aws.amazon.com/blogs/architecture/how-the-mill-adventure-implemented-event-sourcing-at-scale-using-dynamodb/
- [Multi-Tenant SaaS Architecture 2025]https://isitdev.com/multi-tenant-saas-architecture-cloud-2025/
- [Serverless Event Sourcing with AWS]https://dev.to/slsbytheodo/serverless-event-sourcing-with-aws-state-of-the-art-data-synchronization-4mog
- [Event Sourcing Pattern - AWS]https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/event-sourcing.html
- [redb - Embedded Database]https://github.com/cberner/redb

---

**AllSource. Events without boundaries.**

*Cloud-native. Multi-tenant. Serverless-ready.*