Java.Hibernate.Medium.What is multi tenant apps ?

Short Answer

A multi-tenant application is a single application instance that serves multiple customers (tenants), while keeping each tenant’s data isolated.
It allows you to share the same codebase and infrastructure while giving every tenant the feeling of their own private app.


🔎 Detailed Explanation

🔹 What is a tenant?
A tenant typically represents a customer, organization, or user group that uses your application independently of others.


🔹 Why multi-tenancy?
✅ Reduces costs → one app/server supports many customers.
✅ Simplifies deployment → single codebase for all tenants.
✅ Centralized maintenance → upgrades, bug fixes, and new features roll out to everyone instantly.

🔹 Tenant data isolation strategies
There are three main patterns to keep tenant data separate:

1️⃣ Shared Database, Shared Schema

  • All tenants share the same DB and tables.
  • Rows are tagged with a tenant_id column.
  • Cheapest, simplest.
  • Example table: idtenant_idname11001Alice21002BobCorp

2️⃣ Shared Database, Separate Schema

  • One DB with one schema per tenant → e.g., tenant_1001.orders, tenant_1002.orders.
  • Better isolation, but requires more DB objects.
  • Hibernate supports this using schema-based multi-tenancy.

3️⃣ Separate Database per Tenant

  • Each tenant has its own DB instance → strongest isolation.
  • More expensive → more complex to manage.

🔹 How does Hibernate support multi-tenancy?
Hibernate has built-in support via MultiTenantConnectionProvider and CurrentTenantIdentifierResolver:

  • Lets you dynamically switch schemas or connections based on the current tenant.
  • Example:
configuration.setProperty("hibernate.multiTenancy", "SCHEMA");

Hibernate will automatically prefix tables with the correct schema for each tenant.

🔹 Examples of multi-tenant apps
✅ SaaS platforms like Salesforce, Shopify, Jira Cloud → serve thousands of customers from a single codebase.
✅ Any app offering “workspaces” or “organizations” → Slack, Notion, GitHub Enterprise Cloud.

📌 Key Takeaways

✅ Multi-tenancy lets you run one app for many customers, with isolated data per tenant.
✅ It optimizes cost and maintenance, but requires careful design for security and data isolation.
✅ Hibernate supports multi-tenancy with flexible strategies for shared or separate schemas/databases.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.