#247 Enterprise architecture and AI agents 5.6 min
Multi-Tenant AI Agent Platform: Four Leaks and How to Test
I gave every tenant its own database, and one tenant's data still leaked.
Predict the leaks, then run
Copy the code folder first. Before you run anything, write down how many of the four mistakes you expect to leak for each of the three designs, and which mistake you expect a database for each tenant to still leak. Then run python compare.py and compare. You should see 4 of 4, 0 of 4 and 1 of 4.
A hint
Read the DatabasePerTenant class in tenants.py and look at what it still shares.
Break the scoped layer's cache
In tenants.py, find cached_list in the ScopedLayer class. Change the line key = ("list", session) to key = "list" so the cache key is only the question. Predict which design results change, then run python compare.py. The one scoped layer line should now show 1 of 4, leaked through the cache. Put the line back afterwards.
A hint
Another tenant calls cached_list first in compare.py, and its answer is stored under the key.
Fix the cache in the per-tenant database design
In the DatabasePerTenant class, change cached_list so that its cache key includes the session, for example ("list", session), in all three places where the key is used. Run python compare.py. The database for each tenant line should now show 0 of 4 mistakes leaked. Put the three lines back afterwards.
A hint
The three places are the if test, the line that stores the result, and the return line.
Add a fifth leak scenario
In compare.py, add one more scenario line after the search index one: yield "another tenant name", leaked(design.search("acme", "initech", "")). Predict which designs leak it, then run python compare.py. You should see 5 of 5, 0 of 5 and 1 of 5.
A hint
The scoped layer ignores the tenant argument, and the per-tenant database cannot see other tenants.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Three made-up tenants and four mistakes I chose, in SQLite; no agent or model is run, the session is assumed trusted, the index is a plain query, and the script prints leaks but does not fail a build.
Up next in Enterprise architecture and AI agents
More in Enterprise architecture and AI agents
- #229 A Reference Architecture for Enterprise AI Agents, as Code
- #230 Agent Gateway: One Front Door for Models, Tools, Agents
- #231 MCP Gateway: Per-Tool Policy for Agent Tools (Python)
- #232 Give Every AI Agent Its Own Identity
- #233 Delegated Authority: Let an AI Agent Act for a User, Safely
- #234 Authorisation for AI Agents With Policy as Code (Cedar)
Related videos
- #236 Agent Orchestration Patterns: Supervisor, Pipeline, Swarm Enterprise architecture and AI agents
- #237 Where Should AI Agent State and Memory Live? A Crash Test Enterprise architecture and AI agents
- #238 Event-Driven AI Agents: Queues, Crashes and Duplicates Enterprise architecture and AI agents
- #239 Async AI Agents: Design for Timeouts With Job Polling Enterprise architecture and AI agents
