Skip to content
··閱讀時間4分鐘

我知Agency需要咩——所以我第2日就Build咗Multi-Tenancy

做咗20年agency之後,我知multi-tenant architecture唔等得——所以第2日,得一個working AI agent,我將dev complexity三倍化嚟避免未來嘅rewrite。

2025年8月21日。Build STRAŦUM嘅第2日。

我坐喺枱前,有一個working AI agent(Business Strategy)、基本authentication、同一個好簡單嘅data model。咖啡涼緊。我好清楚知道我想build for邊個:需要AI marketing help嘅SMEs,同可以across多個clients用嘅agencies。

兩個audience。由第1日起。

做咗20年agency side之後,我理解agencies actually需要咩——唔係glossy marketing版本,而係manage 10-15個clients嘅messy reality,每個都有唔同strategies、唔同brand guidelines、唔同everything。我亦知SMEs desperately需要affordable strategic help。

但第2日,盯住我嘅simple data model,我意識到一件uncomfortable嘅事:build for兩個audiences代表要build multi-tenant architecture。而家。唔係之後。

到日尾,我commit咗一個會將development complexity三倍化同延長timeline幾個禮拜嘅decision。

我由第2日起build咗multi-tenant architecture。

呢個係ambitious定insane?老實講,我唔肯定。我而家都唔係100%肯定。但以下係點解呢個decision——幾乎冇working product嘅情況下做嘅——點解turn out to be我做過最重要嘅technical choices之一嘅故事。

點解兩個Audience?20年Agency經驗

Vision從來唔係SME-only。由一開始,我就想build for兩個distinct audiences:

SMEs(Small businesses,1-30 employees)——你Afford得起嘅Strategic Co-Founder:

- 請唔起marketing agencies或consultants

- 冇strategic marketing expertise

- 需要quick wins冇learning curve

Agencies(Managing 5-15+個clients)——Scale你嘅Strategic Capacity,唔係你嘅Headcount:

- 溺喺across多個clients嘅strategy work

- 需要consistent frameworks可以scale

- 需要clients之間完整data isolation

點解agencies?因為我喺agency side做咗20年。我知嗰種pain。

我見過strategists juggle 12個clients有12套唔同brand guidelines,copy and paste context between tools,祈禱唔好send錯strategy俾錯client。我睇住agencies因為冇方法scale strategic thinking而唔scale headcount而流失好人。

同老實講?我想challenge自己。淨係build for SMEs會更簡單——一個user、一個organization、straightforward routing。Build for agencies代表multi-client dashboards、data isolation、role-based permissions、client-scoped everything。

我想build一啲complex嘅嘢。一啲actually解決我見咗二十年嘅hard problems。

第2日嘅Realization:你唔可以Retrofit Multi-Tenancy

以下係我第2日盯住simple data model時理解到嘅:

SME architecture(我有嘅):

```
users → campaigns → agent_outputs
```

Multi-tenant architecture(我for agencies需要嘅):

```
organizations (SME or AGENCY)
  ↓
clients (agencies only)
  ↓
campaigns
  ↓
agent_outputs (with org_id AND client_id)
```

分別唔淨止「加幾個columns。」係fundamentally唔同嘅data model。

如果我先build for SMEs再之後加agency support,我要面對:

- Migrate每個existing table加org_id

- Retrofit RLS policies across成個database

- Rewrite frontend routing for client context

- Re-test所有嘢有multi-tenant scenarios

嗰個唔係feature addition。嗰個係rewrite。

我等得越耐,retrofit越貴。第2日便宜。第60日痛苦。第200日就「maybe我哋for agencies build一個separate product。」

所以我做咗決定:由第2日起build multi-tenant,或者接受agencies永遠唔會係first-class citizens。

Trade-Offs:Multi-Tenancy Actually Cost咩

以下係我應該pause同問自己「你肯定?你有一個working agent。一個。」嘅部分。

但我冇pause。相反,我開始好似madman咁寫SQL schemas。第2日嘅Multi-tenancy代表接受令任何reasonable嘅人reconsider嘅costs:

Cost 1:Development Complexity(3倍)

每個table需要org_id。Agency tables需要client_id。RLS policies需要兩個filters。每個query、每個API endpoint、每個frontend route——全部要multi-tenant aware。

估計complexity increase:同一啲features 3倍development time。

(劇透:Actually closer to 4倍。我underestimate咗。Classic me。:P)

Cost 2:Extended Development Timeline

由第1日起build for兩個audiences代表更長嘅path to launch。以下係佢嘅樣:

Timeline

- 8月20日:開始build(由第1日起multi-tenant)

- 9月15日:4個禮拜入面,3個agents working,target 10月

- 10月:病咗10日,寫唔到code

- 11月5日:Private Alpha launch(共75日)

Multi-tenancy加咗咩

- Public同agency tables之間嘅schema routing

- 每個table都有RLS policies(最後有83個across 26個tables)

- Client context propagation through every agent

- Dual routing patterns(SME vs agency URLs)

淨係build SME-only architecture會唔會launch得更快?大概快3-4個禮拜。但到時agencies來敲門我就要stare at rewrite。

Cost 3:Security Requirements(更高Risk)

SME data isolation係table-stakes。Agency data isolation係mission-critical。

加咗嘅Requirements:

- 83個RLS policies across 26個tables

- Schema-level isolation(public vs agency schemas)

- 每個CRUD operation嘅database router functions

- Comprehensive audit logging

- Multi factor authenticator option

Security complexity:比SME-only高5倍。

有啲禮拜我花更多時間喺Row-Level Security policies上面而唔係actual AI agent development。Let that sink in。

Cost 4:Testing Complexity(2倍Test Cases)

每個feature需要test:

- ✅ SME flow(較簡單)

- ✅ Agency flow(client-scoped)

- ✅ Data isolation(Nike ≠ Adidas)

- ✅ Permission boundaries(agencies可以睇SME data嗎?No。)

Test maintenance:Double咗。

你知咩fun?寫同一個test兩次有slightly唔同contexts。Said no one ever。

---

點解我仲係做咗呢個Trade-Off

所以等我搞清楚:3倍complexity、extended timeline、5倍security overhead、double testing...然後我仲係做咗?

係。以下係thinking(such as it was):

原因1:Retrofitting係噩夢

第2日:幾乎冇code寫。Data model仲flexible。冇users要migrate。

第60日:45個tables、200k行code、15個alpha users。加multi-tenancy需要rewrite一半codebase。

Lesson:Architectural decisions隨時間exponentially harder to change。

第2日嘅Cost:~5個鐘redesign data model。

第60日嘅Cost:~200個鐘refactor所有嘢。

我見過公司試retrofit multi-tenancy。好brutal。你基本上係一邊飛一邊rebuild飛機。有乘客。佢哋喺pay你。

原因2:我Actually理解Agencies

呢個唔係theoretical market research。我花咗20年睇住agencies struggle exactly呢個problem。

我知佢哋需要:

- 完整data isolation(client 1睇唔到client 2嘅strategy)

- Client-scoped everything(brand guidelines、personas、campaigns)

- Role-based access(account managers vs strategists)

- White-label potential for client presentations

我可以build呢個啱因為我lived嗰個pain。呢個唔係我想浪費嘅advantage,淨係ship SME-only然後「之後加agencies」。

原因3:Challenge本身就係Point

有啲嘢我唔常講:我_想要_complexity。

我係middle-aged founder有family,唔係22歲可以靠energy drinks同唔瞓覺survive。如果我要花一年build一啲嘢,佢最好challenge我。一啲我technically上proud嘅嘢。

Multi-tenant architecture有proper data isolation、RLS policies、schema routing——呢個好難。呢個interesting。呢個係令我2 AM都仲engaged嘅engineering problem。

Build SME-only會更快。但我會唔會一樣motivated push through hard parts?大概唔會。

原因4:兩個Audiences令對方更好

SMEs同agencies唔係competing use cases。佢哋係complementary:

- Agencies validate product:如果agencies trust佢處理client data,SMEs都會

- SMEs provide volume:更多users代表更多feedback、更快iteration

- Architecture serve兩邊:Team accounts、role permissions、white-label兩個audiences都work

Build for兩個代表build _完整_solution,唔係一個我會outgrow嘅simplified版本。

原因5:Future-Proofing

除咗agencies之外,multi-tenancy architecture enable咗:

- Team accounts(SMEs有multiple users)

- Reseller partnerships

- White-label opportunities

- Enterprise sales(如果我哋ever想go there)

Option value:Multi-tenancy unlock咗我第2日都未能envision嘅business models。

Implementation:第2日Architecture Decisions嘅樣

以下係我8月21日actually build嘅:

Database Schema(Core Decision)

```sql
-- Organizations table (SME or AGENCY)
CREATE TABLE organizations (
  id UUID PRIMARY KEY,
  name TEXT,
  type TEXT CHECK (type IN ('SME', 'AGENCY')),  -- Critical distinction
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Clients table (agencies only)
CREATE TABLE clients (
  id UUID PRIMARY KEY,
  org_id UUID REFERENCES organizations(id),  -- Which agency owns this
  company_name TEXT,
  slug TEXT UNIQUE,  -- For URL routing
  created_at TIMESTAMPTZ DEFAULT NOW(),

  -- Constraint: Only agencies can have clients
  CONSTRAINT clients_agency_only CHECK (
    (SELECT type FROM organizations WHERE id = org_id) = 'AGENCY'
  )
);

-- Campaigns table (multi-tenant aware)
CREATE TABLE campaigns (
  id UUID PRIMARY KEY,
  org_id UUID REFERENCES organizations(id),  -- Always required
  client_id UUID REFERENCES clients(id),      -- Required for agencies, null for SMEs
  name TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Agent outputs (multi-tenant + client-scoped)
CREATE TABLE agent_outputs (
  id UUID PRIMARY KEY,
  org_id UUID REFERENCES organizations(id),
  client_id UUID REFERENCES clients(id),
  campaign_id UUID REFERENCES campaigns(id),
  agent_type TEXT,
  content JSONB,
  created_at TIMESTAMPTZ DEFAULT NOW()
);
```

第2日嘅Key decisions

- 每個table都有org_id(enable RLS)

- client_id nullable(SMEs冇clients)

- Organizations上嘅type discriminator(SME vs AGENCY)

- Constraint:只有agencies可以create clients

Row-Level Security(第2日Planning)

```sql
-- RLS policy for campaigns (multi-tenant isolation)
CREATE POLICY campaigns_org_isolation ON campaigns
  FOR ALL TO authenticated
  USING (
    org_id = get_user_org_id() AND
    (client_id IS NULL OR client_id IN (SELECT id FROM clients WHERE org_id = get_user_org_id()))
  );
```

呢個policy確保:

- ✅ Users只睇到自己organization嘅campaigns

- ✅ Agency users睇唔到SME campaigns

- ✅ Agencies睇唔到彼此嘅clients

Cost:第2日寫咗5個initial RLS policies。最終到launch有83個。

URL Routing(由第1日起Multi-Tenant)

```typescript
// Frontend routing structure (planned Day 2)

// SME routes (simple)
/campaigns/:campaignId
/agents/strategy/:campaignId

// Agency routes (client-scoped)
/clients/:clientSlug/campaigns/:campaignId
/clients/:clientSlug/agents/strategy/:campaignId
```

呢個URL structure enable:

- ✅ 每個URL都有client context(agencies)

- ✅ SME同Agency flows清晰separation

- ✅ Bookmarkable、shareable URLs

- ✅ 每個client嘅analytics tracking

結果:值唔值得?

Launch date:2025年11月5日(由start起75日)

Multi-tenancy嘅development overhead

- Schema design同migrations

- 83個RLS policies across 26個tables

- Database router functions

- Dual routing patterns(SME vs agency URLs)

- Client context propagation

有Claude Code同Gemini 2.5 Pro,actual coding比solo做快。但debug 應該 work但唔work嘅RLS policies?2 AM嘅時候無論有冇AI assistance都仲係painful。

我而家有嘅

- Organizations之間完整data isolation

- Agency-ready architecture有client scoping

- 83個RLS policies across 26個tables

- Schema-level routing(public vs agency schemas)

- 由第1日起built in嘅white-label potential

- 一個可以genuinely serve SMEs同agencies嘅product

值得嗎?

6個月後當我有actual customers pay actual money先問我。而家我喺private alpha有15+用戶,仲喺學咩work咩唔work。

但以下係我知嘅:我有architecture properly serve兩個audiences。如果我build咗SME-only然後試加agencies,我而家就要stare at 3個月嘅rewrite。相反,我focus喺product-market fit,唔係architectural debt。

呢個feel like正確嘅trade-off。

Framework:幾時Early Build Multi-Tenancy

唔係每個product都需要第2日嘅multi-tenancy。用呢個framework:

Early Build Multi-Tenancy如果:

Target market包括agencies/resellers(每個customer更高value)

Data isolation係critical(legal/compliance requirements)

B2B有team accounts likely(Slack、Figma model)

White-label opportunities exist(reseller GTM strategy)

Enterprise sales possible(由第1日起需要multi-client)

Skip Multi-Tenancy Early如果:

B2C product(individual users,冇organizational hierarchy)

MVP needs speed(先test product-market fit)

冇team/agency use case(single-player tools)

Simple data model(之後加org_id好容易)

Solo founders learning(complexity可能太多)

Lessons Learned:Build For你嘅Expertise

1. Audience Knowledge應該Drive Architecture

唔好為hypothetical users design。為你actually理解嘅users design。

2. Early Complexity勝過Late Refactoring

第2日architecture cost:5個鐘redesign data model。第60日architecture cost:200個鐘refactor所有嘢。40倍差異。

3. Technical Challenge可以係Motivation

呢個好personal:我_想_build一啲hard嘅嘢。Multi-tenant architecture有RLS policies、schema routing同proper data isolation——好interesting嘅engineering。

4. Architecture Enable Business Models

Multi-tenancy唔只係technical decision。佢enable咗agency pricing tiers、white-label opportunities、enterprise sales potential、SMEs嘅team accounts。Architecture = business optionality in code form。

5. Solo Founders可以Build Multi-Tenant

「Multi-tenancy對一個人嚟講太complex」→ False。有modern tools(Supabase RLS、database functions、AI-assisted development),solo founders可以build enterprise-grade multi-tenancy。

最後諗法

第2日太early知STRAŦUM會唔會成功。我有一個agent。冇users。冇validation。只有20年agency experience話我知agencies actually需要咩。

Extended timeline有時feel painful。睇住其他人launch而我仲喺debug RLS policies同寫database router functions?Frustrating都唔夠形容。有啲日我question過complexity值唔值得。

但以下係我而家知嘅:由第1日起build for兩個audiences係正確嘅call。唔係因為咩clever unit economics calculation——我仲唔知咩pricing會work。而係因為我有architecture actually properly serve兩個audiences。

第2日嘅Multi-tenancy唔只係technical decision。佢係bet on build for我actually理解嘅users,唔係我之後先figure out嘅hypothetical users。

呢個bet pay off咗未?

我仲喺Private Alpha有15+用戶。我仲冇答案。但我可以同你講:當agency reach out問「can we use this for multiple clients with proper data isolation?」我可以話yes。我唔使話「we're working on that。」

呢個feel like正確嘅foundation。

Time will tell我係ambitious定只係stubborn。(大概兩樣都有少少,老實講。):)

你有冇early做過一個architecture bet當時feel too ambitious——而佢pay off咗嗎?我好想聽你嘅故事。

祝好,

Chandler

STRAŦUM architecture系列: 呢個第2日嘅decision只係開始。第67日,我要rebuild成個multi-tenancy layer因為security audit揭示architectural flaws。然後嚟咗31個blank screens因為lost navigation context,同發現我嘅databasetechnically correct但296倍太慢

---

Build緊multi-tenant SaaS? STRAŦUM嘅architecture由第1日起support SME同Agency customers。申請alpha access:https://stratum.chandlernguyen.com/request-invitation

---

仲喺學architecture decisions係disguise咗嘅business decisions。仲喺debug RLS policies。仲喺question我第2日嘅choices(但少咗)。更多building adventures: https://www.chandlernguyen.com/

---

繼續閱讀

我嘅旅程
聯繫
語言
偏好設定