server/priv/docs/en/guides/cache/architecture.md
[!NOTE] This page provides a technical overview of the Tuist cache service architecture. It is primarily intended for self-hosting users and contributors who need to understand the internal workings of the service. General users who only want to use the cache do not need to read this.
The Tuist cache service is a standalone service that provides Content Addressable Storage (CAS) for build artifacts and a key-value store for cache metadata.
The service uses a two-tier storage architecture plus local SQLite metadata:
flowchart LR
CLI[Tuist CLI] --> NGINX[Nginx]
NGINX --> APP[Cache service]
NGINX -->|X-Accel-Redirect| DISK[(Local Disk)]
APP --> S3[(S3)]
APP -->|auth| SERVER[Tuist Server]
Nginx serves as the entry point and handles efficient file delivery using X-Accel-Redirect:
X-Accel-Redirect header. Nginx serves the file directly from disk or proxies from S3.Artifacts are stored on local disk in a sharded directory structure:
{account}/{project}/cas/{shard1}/{shard2}/{artifact_id}ABCD1234 → AB/CD/ABCD1234)The cache service uses two SQLite databases:
cache_artifacts, orphan scan cursors, Oban jobs, and other service metadata.key_value_entries and key_value_entry_hashes in a dedicated SQLite file.The key-value store is split into its own database so it can use SQLite incremental auto-vacuum without affecting artifact metadata and orphan cleanup state.
S3 provides durable storage:
The service manages disk space using multiple background processes:
cache_artifactsThe service also runs an orphan cleanup worker for disk artifacts:
cache_artifacts row.The cache delegates authentication to the Tuist server by calling the /api/projects endpoint and caching results (10 minutes for success, 3 seconds for failure).
sequenceDiagram
participant CLI as Tuist CLI
participant N as Nginx
participant A as Cache service
participant D as Disk
participant S as S3
CLI->>N: GET /api/cache/cas/:id
N->>A: Proxy for auth
A-->>N: X-Accel-Redirect
alt On disk
N->>D: Serve file
else Not on disk
N->>S: Proxy from S3
end
N-->>CLI: File bytes
sequenceDiagram
participant CLI as Tuist CLI
participant N as Nginx
participant A as Cache service
participant D as Disk
participant S as S3
CLI->>N: POST /api/cache/cas/:id
N->>A: Proxy upload
A->>D: Stream to disk
A-->>CLI: 201 Created
A->>S: Background upload
| Endpoint | Method | Description |
|---|---|---|
/up | GET | Health check |
/metrics | GET | Prometheus metrics |
/api/cache/cas/:id | GET | Download CAS artifact |
/api/cache/cas/:id | POST | Upload CAS artifact |
/api/cache/keyvalue/:cas_id | GET | Get key-value entry |
/api/cache/keyvalue | PUT | Store key-value entry |
/api/cache/module/:id | HEAD | Check if module artifact exists |
/api/cache/module/:id | GET | Download module artifact |
/api/cache/module/start | POST | Start multipart upload |
/api/cache/module/part | POST | Upload part |
/api/cache/module/complete | POST | Complete multipart upload |