Building a Multi-Tenant SaaS Platform from Scratch
The Challenge
When I set out to build ChoGiaKiem.vn, the goal was ambitious: create a multi-tenant marketplace platform where each vendor gets their own storefront, subdomain, POS system, and business tools — all from a single codebase.
The platform needed to handle:
Architecture Decisions
Why Next.js + Supabase?
I chose Next.js 15 App Router for its hybrid rendering model — most dashboard pages are client-rendered for interactivity, while public storefronts use SSR for SEO. Supabase (PostgreSQL) was the natural choice for its Row-Level Security (RLS), real-time subscriptions, and generous free tier during development.
Multi-Tenant Strategy: Shared DB, RLS Isolation
Instead of separate databases per tenant, I used a shared database with RLS policies. Every table includes a vendor_id column, and Supabase RLS ensures vendors can only see their own data.
-- Example RLS policy
CREATE POLICY "Vendors see own products" ON products
FOR ALL USING (vendor_id = auth.jwt() ->> 'vendor_id');This approach keeps infrastructure costs low while maintaining strict data isolation.
Subdomain Routing
Next.js middleware handles subdomain detection, resolving vendorname.chogiakiem.vn to the correct vendor context before any page renders.
Key Technical Achievements
| Metric | Value |
|---|---|
| API Routes | 630+ |
| React Components | 397 |
| Database Tables | 50+ |
| App Modules | 15+ |
The POS System
The POS module was the most complex — it needed to:
I built a service worker that caches critical POS data locally, so cashiers can continue selling even during network interruptions.
E-Invoicing Integration
Vietnam's tax authority requires electronic invoicing (hóa đơn điện tử). I built a queue-based system that:
Results
Lessons Learned
The platform continues to grow, and the architecture has proven scalable. Each new vendor can be onboarded in under 5 minutes, with full POS, inventory, and accounting ready to go.