MATIH Platform is in active MVP development. Documentation reflects current implementation status.
2. Architecture
Database Isolation

Database Isolation

The MATIH Platform uses Hibernate's schema-based multi-tenancy to isolate tenant data at the database level. Each tenant's data resides in a separate PostgreSQL schema within a shared database, providing strong data isolation without the operational overhead of separate database instances.


Schema-Per-Tenant Model

PostgreSQL Database: matih_ai
  +-- Schema: system       (platform metadata)
  +-- Schema: acme_corp    (Tenant: ACME Corporation)
  +-- Schema: globex       (Tenant: Globex Inc.)
  +-- Schema: initech      (Tenant: Initech LLC)

Each schema contains identical table structures. Hibernate routes queries to the correct schema based on the current tenant context.


TenantIdentifierResolver

The TenantIdentifierResolver integrates with Hibernate to resolve the current tenant:

@Component
public class TenantIdentifierResolver
    implements CurrentTenantIdentifierResolver<String> {
 
    @Override
    public String resolveCurrentTenantIdentifier() {
        return TenantContext.getCurrentTenantIdOrDefault("system");
    }
 
    @Override
    public boolean validateExistingCurrentSessions() {
        return true;
    }
}

Before each query, Hibernate sets the PostgreSQL search path:

SET search_path TO 'acme_corp';
SELECT * FROM dashboards WHERE created_by = 'user-123';

Hibernate Configuration

spring:
  jpa:
    properties:
      hibernate:
        multiTenancy: SCHEMA
        tenant_identifier_resolver:
          com.matih.commons.persistence.multitenancy.TenantIdentifierResolver

Connection Pool Management

Each service maintains a single HikariCP connection pool shared across all tenants:

ParameterDevelopmentProduction
Pool size1030
Min idle25
Connection timeout30s10s

The TenantIdentifierResolver sets the schema on each connection before use, ensuring correct routing without per-tenant pools.


Schema Provisioning

When a new tenant is provisioned, the Tenant Service creates schemas:

StepAction
1Create schema: CREATE SCHEMA IF NOT EXISTS {tenant_slug}
2Run Flyway/Alembic migrations against the new schema
3Seed initial data (default dashboards, configurations)
4Verify schema structure matches system schema

Backup and Migration

OperationMethod
Per-tenant backuppg_dump --schema={tenant_slug}
Tenant migrationExport schema from source, import to target database
Schema comparisonCompare tenant schema against system schema
Data purgeDrop tenant schema on decommission

Isolation Guarantees

GuaranteeMechanism
No cross-tenant queriesHibernate sets search_path per request
No missing tenant filterrequireTenantId() throws if context is absent
Schema independenceEach schema is a complete, isolated namespace
Connection isolationSchema set on connection checkout, cleared on return

Related Pages