Content-Addressed Storage (CAS) for Artifacts
1. Overview
This draft specifies a Content-Addressed Storage (CAS) system designed for storing immutable artifacts (e.g., build outputs, container images, datasets, binaries) in a distributed, scalable manner. Data is identified and addressed solely by its cryptographic hash (SHA-256), enabling:
- Deduplication: Global uniqueness across all users/tenants.
- Integrity: Tamper-proof storage with verifiable hashes.
- Migration: Backend-agnostic portability.
- Cost Optimization: Minimal storage footprint and efficient retrieval.
The system uses a multi-tier architecture with an abstraction layer over pluggable backends (e.g., S3, GCS, MinIO, filesystem). Artifacts are stored as immutable blobs; metadata (e.g., size, MIME type) is stored separately in a key-value store (e.g., DynamoDB, Redis).
Key Invariants:
- No data is ever modified in-place.
- Retrieval always verifies hash integrity.
- Storage is append-only.
2. Core Data Model
- Content ID (CID):
sha256-<64-hex-digits> (e.g., sha256-2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824).
- Blob: Raw bytes of the artifact.
- Metadata Record: JSON stored under key
cid:metadata:
{
"size": 1048576,
"mime": "application/octet-stream",
"created": "2023-10-01T12:00:00Z",
"tags": ["build/123", "user/abc"],
"refs": 1 // Reference count for GC
}
3. Deduplication (Dedup)
- Global Dedup: Before storing, compute SHA-256 hash. Query metadata store for existence:
- If exists and
refs > 0: Increment refs, return CID (zero-copy).
- Else: Store blob, create metadata with
refs=1.
- Chunking for Large Artifacts: For files > 1MB, use content-defined chunking (e.g., Rabin fingerprinting with avg 256KB chunks):
- Each chunk gets its own CID.
- Root CID is Merkle DAG (using
dag-pb format like IPFS).
- Enables partial dedup and repair.
- Cross-Tenant: Single namespace; isolates via tags/ACLs, not storage.
- Metrics: Track dedup ratio (e.g., 70-90% savings on CI/CD artifacts).
Pseudocode:
def store_artifact(data: bytes, tags: list[str]) -> str:
cid = sha256(data).hexdigest()
if metadata_exists(f"sha256-{cid}"):
increment_refs(cid)
return f"sha256-{cid}"
store_blob(f"blobs/sha256-{cid}", data)
store_metadata(f"sha256-{cid}", {"size": len(data), "tags": tags, "refs": 1})
return f"sha256-{cid}"
4. Integrity
- Hash-on-Write: Always compute+verify SHA-256 before/after storage.
- Hash-on-Read: Fetch → hash verify → serve (fail-fast on mismatch).
- Erasure Coding (Optional): For high-availability backends, use Reed-Solomon (e.g., via ZFS or libec) to tolerate bit-flips.
- Tamper Evidence: Signed metadata (Ed25519) with root key per tenant.
- Garbage Collection: Periodically decrement
refs on unreferenced CIDs; delete if refs == 0 (after integrity scan).
- Audit Logs: Log all store/fetch/delete with CID, timestamp, actor.
Error Handling:
- Corrupt blob: Quarantine, trigger repair from replicas or rebuild.
- Replication Factor: 3x by default (configurable).
5. Migration
- Backend Abstraction: Pluggable
StorageDriver interface:
type StorageDriver interface {
Put(ctx context.Context, key string, data []byte) error
Get(ctx context.Context, key string) ([]byte, error)
List(prefix string) ([]string, error)
Delete(key string) error
}
- Drivers: S3Driver, GCSDriver, FileDriver, AzureDriver.
- Zero-Downtime Migration:
- Dual-write: New puts go to both old+new backend.
- Background copy: Replay
List("blobs/") → put to new.
- Cutover: Update config to single new backend.
- Verify: Sample 1% blobs for hash match.
- Namespace Migration: Prefix CIDs (e.g.,
v2/sha256-...) without rehashing data.
- Sharding: Horizontal scale via consistent hashing (e.g., CID % num_shards → backend).
Migration Cost: ~1x data volume (copy) + temp dual-storage.
6. Costs
-
Storage Optimization:
| Technique | Savings | Implementation |
|---|
| Dedup | 50-90% | Global CID lookup |
| Compression | 20-60% | Zstd (level 3) on store |
| Chunking | 10-30% | For >1MB files |
| Tiering | 40-70% | Hot (SSD/S3 Std) → Cold (S3 Glacier) based on access age |
-
Cost Model (AWS S3 Example, per TiB/month):
| Tier | Cost | Trigger | GC Policy |
|---|
| Hot | $0.023 | refs > 10 or accessed <30d | None |
| Warm | $0.01 | refs 1-10, age 30-90d | Lazy |
| Cold | $0.004 | age >90d | Aggressive (refs==1) |
-
Retrieval Costs: Prefix-cached metadata ($0 egress); blob GET minimized via TTL cache (e.g., Redis + CDN).
-
Total Ownership: Target < $0.005/TiB/month post-dedup/compress.
-
Monitoring: Prometheus metrics for bytes_stored, dedup_ratio, cost_per_tidb.
Billing: Per-tenant quotas (e.g., 10TiB logical); charge on unique bytes (post-dedup).
7. Architecture Diagram
[Client] --> [API Gateway] --> [CAS Service]
|
+--> [Metadata KV] (DynamoDB)
|
+--> [Storage Abstraction]
|
+--> S3/GCS (Tiered)
|
+--> Cache (Redis)
8. Implementation Roadmap
- Phase 1: Core store/fetch with S3 driver (1 week).
- Phase 2: Dedup + metadata (1 week).
- Phase 3: Integrity + chunking (2 weeks).
- Phase 4: Migration drivers + tiering (2 weeks).
- Tech Stack: Go (server), Protocol Buffers (API), Zstd (compress).
9. Risks & Mitigations
- Hash Collisions: SHA-256 (2^-128 negligible).
- Backend Outages: Multi-region replication.
- Abuse: Rate limits, tenant isolation.
This draft is ready for review/iteration. Questions?