Spring Boot Multitenancy – Day 1: Wiring the Skeleton

Rahul BhattRahul Bhatt
2 min read

Summary:

Thread-safety, routing, and resolver strategy — Day 1 of building a Spring Boot multitenancy starter from scratch. A lot of thinking, a lot of tests, and the first structure is now alive.

After Day 0’s fire, Day 1 was all about grounding the idea into structure. This was the first time the code felt “real.”

My focus: building it the way Spring Boot would — if it handled multitenancy natively.

Day 1 of My Spring Boot Multitenancy Starter: Contexts, Filters, and Auto-Config


🎯 Goal

Scaffold the foundational skeleton of a plug-and-play Spring Boot multitenancy starter — just like how Spring Boot itself feels.


🧱 What I Built Today

🧠 Core Concepts Implemented

  • Thread-safe TenantContextHolder using ThreadLocal

  • Pluggable TenantResolver interface (subdomain strategy default)

  • TenantFilter to set/clear context per request

  • TenantDefinition (record) to hold metadata like tenantId, creds, etc.

  • TenantRegistry + InMemoryTenantRegistry to register and fetch tenants

  • TenantRoutingDataSource to delegate dynamically

  • DataSourceFactory to build isolated HikariDataSources per tenant

  • Custom YAML props via HikariTenantPoolProperties

  • Spring Boot AutoConfig:

    • MultitenancyAutoConfiguration

    • TenantRegistryAutoConfig

    • TenantDataSourceAutoConfig

  • @EnableMultitenancy annotation to wire everything

🧪 Tests Added

  • TenantContextTest — validate isolation

  • TenantContextLeakTest — simulate misuse

  • TenantContextConcurrencyTest — 100-thread stress test


🧠 What I Learned Today

🔹 Java Concurrency

Built a cheat-sheet on:

  • ThreadLocal

  • ExecutorService, CountDownLatch, AtomicBoolean

🔹 Maven Structuring

  • Local publish using mvn install

  • Clean starter module structure

  • IDE tricks for IntelliJ Maven + .iml

🔹 Spring Boot Internals

  • @ImportAutoConfiguration flow

  • AutoConfiguration.imports mechanism

  • Clean starter design, Spring-style


📁 Folder Structure

multi-tenant-springboot-starter/
├── pom.xml
├── starter/
│ ├── pom.xml
│ └── src/main/java/org/nirvikalpa/...
│ └── src/test/java/org/nirvikalpa/...
├── demo/ # coming tomorrow


🧭 Next Steps (Day 2 Plan)

  • Build a tenant onboarding workflow

  • Wire demo app to consume the starter

  • Expose /health per-tenant endpoint

  • Reflect in Ego Log #002


🔗 Repo: Github Link

Thanks for following my journey.
This is more than code — it's my architectural diary.

0
Subscribe to my newsletter

Read articles from Rahul Bhatt directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Bhatt
Rahul Bhatt