Building a Tenant-Isolated Vault for Confidential PDF Review
System DesignModel answerLast updated
Design a backend for sharing sensitive PDFs across organizations with fast permission revocation, per-viewer watermarks, and a tamper-evident record of every access.
Context
Law firms and corporate teams exchange large numbers of confidential PDFs during acquisitions, lawsuits, and regulatory reviews. They need a hosted vault where outside parties can read documents but cannot freely copy them. Design the backend for such a service.
What the system must do
- Hierarchy and tenancy. Organizations own rooms, rooms contain folders, and folders contain PDF documents. Many organizations share the platform.
- Fine-grained permissions. Support role-based and attribute-based rules (for example Admin, Collaborator, View-Only, Watermarked-Only). Removing someone's access must take effect right away.
- Protected delivery. Viewers must never obtain the raw file unless their role allows it. Every view carries a user-specific watermark (user ID, timestamp, IP address) and pages are displayed in the browser one at a time.
- Audit trail. Record every access event (document opened, page viewed, download attempted, permission changed) in an append-only log that makes tampering detectable.
Non-functional targets
- First page visible in under a second, even for files up to 500 MB.
- No data leakage between tenants, with cryptographic isolation at rest and in transit.
- Permission changes and revocations effective globally within 1 second.
- 99.99% read availability for rooms with active reviews.
Scale assumptions
- 5,000 organizations and 50,000 active rooms.
- 50 million PDFs, averaging 25 MB, with a maximum of 500 MB.
- Peak of 10,000 concurrent reviewers producing about 2,000 document/page requests per second and about 500 audit events per second.
What to cover in your answer
- How permissions are evaluated, cached, and invalidated.
- How PDFs are ingested, converted into protected page images, watermarked, and streamed.
- The data model for tenants, rooms, grants, documents, and audit records.
- The pipeline that stores high-volume audit events in a tamper-evident way.
- The trade-offs between direct storage download and proxied delivery, and between client-side and server-side watermarking.
Hints
Hint 1
Work out the numbers first: total storage, rendered page volume, egress bandwidth, and audit bytes per day. Then identify which two requirements (1-second revocation, sub-second first page on huge files) rule out the obvious designs.
Hint 2
Think about why a long-lived pre-signed storage URL conflicts with instant revocation. Consider a short-lived ticket that proves identity, plus a per-request authorization check against a versioned policy that is pushed to every serving node.
Hint 3
Separate the expensive work from the per-user work: render pages once at ingest and store them encrypted, then apply only the cheap watermark overlay per request. For the audit log, consider hash chaining plus write-once storage.
Hint 4
Prefix every storage key and cache key with the tenant ID, and use per-tenant encryption keys, so a single bug cannot expose another tenant's data.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Scope and sizing
Assume browser-only viewing, PDF as the only format, and download as a role-gated permission rather than a default.
| Quantity | Estimate |
|---|---|
| Raw storage | 50M x 25 MB = about 1.25 PB |
| Rendered pages | about 40 pages per doc, 2B pages, roughly 150 KB each at two resolutions, about 0.6 PB |
| Page egress | 2,000 req/s x 150 KB = about 300 MB/s (~2.4 Gbps) |
| Audit data | 500/s x 1 KB = about 43 GB/day, ~16 TB/year uncompressed |
| Authz checks | 2,000+/s, each budgeted under 5 ms |
Two constraints shape everything:
- Revocation within 1 s means no long-lived direct links to objects.
- Sub-second first page on 500 MB means the browser never downloads the whole file.
2. Data model
Relational metadata in Postgres, sharded by tenant_id, with row-level security on tenant_id.
tenants(tenant_id, name, kms_key_id, region)rooms(room_id, tenant_id, name, policy_epoch, wrapped_dek, status)folders(folder_id, room_id, parent_id, path)documents(doc_id, room_id, folder_id, sha256, page_count, status, storage_prefix, version)grants(grant_id, room_id, resource_type, resource_id, principal_type, principal_id, role, attrs JSONB, expires_at, revoked_at). Theattrscolumn holds IP ranges, a download flag, and time windows.audit_events(tenant_id, room_id, seq, ts, actor, ip, action, resource, meta, prev_hash, hash)in a columnar store such as ClickHouse.
Object storage: raw PDFs in a private bucket with no client access, and encrypted rendered pages under pages/{tenant}/{room}/{doc}/{page}_{res}. Prefix every object key and cache key with the tenant ID. It is the cheapest protection against cross-tenant bugs.
Encryption: a per-tenant KMS key wraps a per-room data key (envelope encryption). TLS everywhere, mTLS between services.
3. Architecture
- Ingest. Uploads land in a quarantine bucket. A queue feeds workers that scan for malware, sanitize the PDF (strip scripts and attachments), rasterize pages, extract text, encrypt, and store results.
- Authz service. Grants are the source of truth in Postgres. Compiled per-room policies are cached in memory on the serving nodes.
- Page gateway (reverse proxy). The only path to page bytes. It validates the ticket, checks policy, fetches the encrypted page, decrypts it, composites the watermark, and responds with
Cache-Control: no-store. - Audit pipeline. Services emit events to Kafka, which feeds ClickHouse for queries and a write-once (object-lock) bucket for retention.
- Viewer. A canvas-based client loads a manifest and fetches pages lazily, prefetching a couple of pages ahead.
4. Authorization with 1-second revocation
Evaluation. Grants inherit Room, then Folder, then Document. The most specific grant wins and an explicit deny beats any allow. Attribute checks (IP, expiry, download flag) run after the role check.
Versioned cache. Each room has a policy_epoch. A grant change is one transaction that updates the grant, increments the epoch, and writes an outbox row. Gateways cache compiled policies keyed by (tenant, room, epoch), so a decision is a local lookup well under 1 ms.
Invalidation. The outbox relay publishes {room, epoch} over pub/sub to all gateways in all regions, typically in 100-300 ms. Gateways also track a heartbeat on that channel. If it goes silent for more than about 1 s, the gateway fails closed and synchronously re-reads the epoch before serving. This converts an eventually consistent cache into bounded staleness under 1 s.
Tickets, not pre-signed URLs. A pre-signed URL cannot be revoked and exposes the object. Instead issue a short-lived signed ticket (about 60 s, bound to session, document, and client IP). The ticket proves authentication only. The gateway checks the current policy on every request, so a revoked user is rejected immediately even with a valid ticket.
5. Processing and serving
Ingest. Split and rasterize in parallel across a worker fleet at two resolutions (about 1200 px for normal view, 2400 px for zoom). For a huge file, render the first ~20 pages first and mark the document viewable while the rest finish.
First-page latency budget:
| Step | Time |
|---|---|
| Manifest | ~50 ms |
| Ticket + authz | ~5 ms |
| Encrypted page fetch (hot cache or storage) | 30-80 ms |
| Decrypt | ~1 ms |
| Watermark composite | 10-20 ms |
| Network | ~100 ms |
| Total | about 300 ms, independent of file size |
Watermarking. Base pages are rendered once and cached encrypted. Only the overlay is per user, so nothing is re-rendered per viewer.
- Visible: tiled semi-transparent text with user, timestamp, and IP, burned into pixels on the server.
- Forensic: an invisible per-user, per-session mark (frequency-domain perturbation) so leaked screenshots can be traced.
6. Audit and compliance
- Each event carries
hash = H(prev_hash || canonical_event), chained per room. Periodically sign the chain head with a KMS key and anchor it in write-once storage. Any edit or deletion breaks the chain. - Kafka gives durable buffering at 500 events/s (trivial load), partitioned by room to preserve order.
- Query side: ClickHouse ordered by
(tenant, room, ts). Compliance copies live in object-lock storage. - Page-view events are emitted by the gateway, since it is the only path to pages, so no view can go unlogged.
7. Trade-offs
Proxy vs direct storage download. The proxy costs about 2.4 Gbps of egress (roughly 10 instances per region with headroom). In return it gives instant revocation, per-page audit, server-side watermarking, and no direct object exposure. Reserve direct delivery for an explicit Download action, which should still produce a watermarked, audited, short-lived copy.
Server vs client watermarking. Client overlays are cheap but removable with browser devtools. Use them only as UX polish for trusted roles. Watermarked-Only must be burned in on the server.
8. Availability
Multi-AZ gateways and storage with regional replication, stateless gateways behind a load balancer, and hot-page caches per region. If the authz push channel degrades, fail closed for writes and sensitive roles. Keep the audit path asynchronous but durable, and buffer locally if Kafka is briefly unavailable.
Common mistakes
- Handing out pre-signed URLs and then claiming instant revocation.
- Relying on a plain TTL cache for permissions.
- Rendering a watermarked copy per user instead of compositing over a shared base page.
- Watermarking only in the browser.
- Logging audit events from clients rather than from the gateway.
- Forgetting tenant prefixes in cache keys.