[파이썬] SQLAlchemy Multi-tenancy 및 Sharding

In today’s ever-growing digital landscape, multi-tenancy and sharding have become crucial aspects of building scalable and efficient applications. SQLAlchemy, a powerful Python ORM (Object Relational Mapper), provides excellent support for implementing both multi-tenancy and sharding in Python-based applications.

What is Multi-tenancy?

Multi-tenancy is an architectural pattern where a single instance of an application serves multiple tenants or clients, who share the same resources but have separate data and configurations. In other words, multi-tenancy allows multiple users or organizations to use the same application while maintaining isolation and separation of data.

What is Sharding?

Sharding is a technique where large databases are divided into smaller, more manageable parts called shards, usually based on certain criteria such as data ranges, hash values, or specific attributes. Each shard can be stored on a separate database server, enabling horizontal scaling and improving performance by distributing the workload across multiple servers.

SQLAlchemy and Multi-tenancy

SQLAlchemy provides various tools and techniques to implement multi-tenancy in Python applications. Let’s explore some of the commonly used approaches:

1. Separate Databases for Each Tenant

One of the straightforward ways to implement multi-tenancy in SQLAlchemy is to use separate databases for each tenant. Each tenant’s data is stored in a dedicated database, ensuring complete isolation. This approach can be achieved by creating a separate Engine for each tenant, and associating the appropriate engine with each tenant’s session.

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Create separate engines for each tenant
engine_tenant1 = create_engine('postgresql://user:password@tenant1_db_host/tenant1_db')
engine_tenant2 = create_engine('postgresql://user:password@tenant2_db_host/tenant2_db')

# Create session factories associated with respective engines
SessionTenant1 = sessionmaker(bind=engine_tenant1)
SessionTenant2 = sessionmaker(bind=engine_tenant2)

# Create session for Tenant 1
session_tenant1 = SessionTenant1()

# Create session for Tenant 2
session_tenant2 = SessionTenant2()

2. Shared Database with Tenant Identification

In some cases, it might be desirable to store all tenant data in a shared database while identifying the tenant using a unique identifier. This approach allows for more efficient resource utilization and reduces the management overhead of maintaining multiple databases.

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session

# Declare base model
Base = declarative_base()

# Define Tenant model
class Tenant(Base):
    __tablename__ = 'tenants'
    
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)

# Create shared engine and session factory
engine = create_engine('postgresql://user:password@shared_db_host/shared_db')
Session = sessionmaker(bind=engine)
session = scoped_session(Session)

# Set up tenant identification
tenant = session.query(Tenant).filter_by(name='tenant1').first()
session.execute(f"SET myapp.tenant_id = {tenant.id}")

# Use the session to interact with tenant-specific data

SQLAlchemy and Sharding

SQLAlchemy also supports sharding to effectively distribute data across multiple databases. Here’s an example of how sharding can be implemented using SQLAlchemy:

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import create_database, drop_database

# Create engine for master database
engine_master = create_engine('postgresql://user:password@master_db_host/master_db')

# Create session factory for the master database
SessionMaster = sessionmaker(bind=engine_master)
session_master = SessionMaster()

# Create shard-specific engines
shard1_uri = 'postgresql://user:password@shard1_db_host/shard1_db'
shard2_uri = 'postgresql://user:password@shard2_db_host/shard2_db'
engine_shard1 = create_engine(shard1_uri)
engine_shard2 = create_engine(shard2_uri)

# Create session factories for shards
SessionShard1 = sessionmaker(bind=engine_shard1)
SessionShard2 = sessionmaker(bind=engine_shard2)

# Create session for shard 1
session_shard1 = SessionShard1()

# Create session for shard 2
session_shard2 = SessionShard2()

In this example, we have a master database that manages the sharding metadata and separate engines for each shard. Each shard can be accessed through its respective engine and session factory.

Conclusion

SQLAlchemy provides a rich set of tools and features to implement multi-tenancy and sharding in Python applications. With its flexibility and extensive support for various databases, SQLAlchemy enables developers to build scalable and efficient applications that can handle multiple tenants and efficiently distribute data across multiple database servers.