Tenant-Safe Next-File Suggestions with Live Permission Checks
Machine LearningModel answerLast updated
Design a low-latency recommender that proposes the next file a user will open while keeping each company's data isolated and respecting constantly changing file permissions.
Scenario
A cloud document platform serves thousands of customer companies (tenants). It wants a "you might open this next" feature that uses each user's own activity and broader usage patterns to suggest files.
The hard rule is tenant isolation: a user at one company must never see a suggestion that comes from another company, even if that file would otherwise look popular or relevant. Within a company, a user may only be shown files they are currently allowed to view, and those permissions change often as people join or leave folders and groups.
Requirements
- Data isolation. Training data and the context used at inference time must not cross tenant boundaries.
- Permission enforcement. Only files the user can currently view may be suggested.
- Latency. Top-k results must come back in under 100 ms.
- Scale. Millions of users across thousands of tenants, with very uneven tenant sizes.
Constraints
- No information may leak between tenants through model gradients or shared embeddings.
- Permission checks should happen as close to the ranking stage as practical, so that few invalid suggestions are produced.
What to deliver
- A high-level architecture for training and for serving.
- A strategy for enforcing permissions inside the recommendation pipeline.
- A plan for cold start (new files, and optionally new users and tenants) that keeps tenant isolation intact.
State your scale assumptions and discuss the trade-offs of the choices you make.
Hints
Hint 1
Write down the invariants first (no cross-tenant candidates, no cross-tenant learned parameters, every returned file is permitted at serve time), then ask which components could break each one.
Hint 2
Consider a hybrid model: a shared ranker that only sees behavior-style features with no IDs or content, plus per-tenant embeddings and indexes for anything that carries identity.
Hint 3
Compare filtering permissions before retrieval, after ranking, and between retrieval and ranking. Think about what each costs in latency and in wasted ranking work, and about a final cheap revalidation of the top-k.
Hint 4
For cold start, look for signals available at creation time: folder, creator, who it was shared with, and content vectors from a frozen encoder that was never trained on tenant data.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Assumptions and invariants
Assumptions to state aloud:
- About 10M users, 5,000 tenants, heavily skewed (10 seats to 200k seats).
- About 2B files. A user can reach roughly 10k to 1M of them.
- Around 10k QPS at peak, p99 under 100 ms end to end.
- Permission changes must take effect within seconds. Behavioral signals can lag by minutes.
Invariants:
- No cross-tenant candidates.
- No learned parameter encodes another tenant's data.
- Every returned file passes a permission check at serve time.
2. Data model and isolation
The tenant ID comes from the auth token, never from the request body. Every table, stream partition, cache key and index carries tenant_id as the leading key, so a query physically cannot read another tenant's partition. Permissions are stored as relationship tuples (file to folder to group to user), in the style of Google's Zanzibar.
3. Architecture
Training
- Events flow through a stream partitioned by tenant into a feature store with per-tenant namespaces.
- Shared ranker. A GBDT or small MLP trained on tenant-agnostic behavioral features: recency of the user's last open, open counts, co-access strength, same-folder-as-current-file, shared-by-collaborator flag, file type and age, and the file's popularity percentile within its tenant. It takes no user IDs, file IDs, filename tokens or content. It learns how people behave with files, not what any company's files are. For the gradient-leakage constraint, add per-tenant gradient clipping plus noise (DP-SGD style) so no single tenant measurably shapes the weights.
- Per-tenant models. Each tenant gets its own item2vec or two-tower embeddings trained on its own co-access sequences, in isolated jobs. Tiny tenants skip this and use graph and heuristic retrievers.
| Option | Strength | Weakness |
|---|---|---|
| Global model with ID embeddings | Best raw quality | Mixes tenants in embeddings and gradients |
| Per-tenant only | Clean isolation | Weak for small tenants, 5,000 models to operate |
| Hybrid (chosen) | Isolation where it matters, good ranking for small tenants | Needs strict control over which features the shared model sees |
Serving
auth (tenant, user)
-> tenant-scoped candidate generation (~500) ~20 ms
-> batched permission check + cache ~15 ms
-> feature fetch (tenant namespace) ~15 ms
-> ranking (shared ranker + tenant embeddings) ~20 ms
-> final revalidation of top-k, diversity ~5 ms
About 75 ms, leaving headroom.
4. Permission enforcement
Candidate sources are scoped by construction: the user's recent files, files in their folders and groups, co-access neighbors of the current file, files recently shared with them, and results from a per-tenant ANN index. Most candidates are therefore already valid.
Where to filter:
- Only after ranking: cheap, but you may lose most of the top 10 and need to backfill, wasting latency and hurting quality.
- Pre-filter the whole corpus: infeasible, since you cannot enumerate a million accessible files per request.
- Between retrieval and ranking (chosen): one batched
Check(user, files[])call for roughly 500 candidates, backed by a per-user cache (resolved group set plus a bitmap or bloom filter of accessible folders, short TTL). Ranking compute is spent only on valid files. - Final step: revalidate the top-k with a consistency token newer than the user's last permission change, so a stale grant cannot slip through.
Frequent changes:
- Publish ACL change events on a stream. They invalidate the user's cache entries and evict revoked files from any precomputed candidate lists.
- Fail closed. If the permission service times out, drop unverified candidates and fall back to the user's own recent or owned files.
- Store folder or group IDs as filterable attributes in the per-tenant ANN index so filtered search skips inaccessible regions.
5. Cold start under isolation
- New file: use creation-time signals: folder, creator, and who it was shared with ("shared with you" is a strong predictor). Add a content vector from a frozen, publicly pretrained encoder that is never fine-tuned on tenant data, stored only in that tenant's index. Add a small recency boost and an exploration slot (epsilon-greedy or Thompson sampling) to gather feedback.
- New user: use files in their team folders and within-tenant popular files they can access. The shared ranker works immediately because its features are tenant-agnostic.
- New tenant: shared ranker plus graph and heuristic retrievers from day one. Train per-tenant embeddings once enough activity exists.
6. Trade-offs, monitoring and common mistakes
- Differential privacy noise costs some ranker accuracy. Because the ranker uses few, coarse features, the loss is usually small.
- Larger permission caches cut latency but widen the staleness window. The final revalidation bounds that risk.
- Monitor: rate of candidates dropped by permission checks (a rising rate signals poor candidate scoping), cache hit rate, p99 per stage, and an automated test that injects cross-tenant IDs and asserts they never appear.
Common mistakes:
- Training one global model with file or user ID embeddings and calling it isolated.
- Filtering permissions only after ranking, leaving too few results.
- Taking the tenant from the request instead of the auth token.
- Failing open when the permission service is slow.
- Fine-tuning a text encoder on tenant documents and then sharing it.