Sharding
Sharding
A technique that horizontally distributes large-scale data across multiple database servers. Splits data by a shard key to overcome the limits of a single server and improve scalability and performance.
Sharding
A technique that horizontally distributes large-scale data across multiple database servers. Splits data by a shard key to overcome the limits of a single server and improve scalability and performance.
Sharding is a horizontal scaling technique that splits one huge database into multiple smaller pieces (shards) and stores them distributed across multiple servers. Each shard operates as an independent database and is responsible for only a portion of the entire data.
The Shard Key is the criterion column that determines which shard data is stored in. For example, if user_id is used as the shard key, the user_id % 4 operation selects one of the 4 shards. A good shard key should distribute data evenly and prevent hotspots.
There are several sharding strategies. Range Sharding splits by value range (e.g., A-M, N-Z), Hash Sharding distributes evenly using a hash function, and Directory Sharding uses a separate mapping table. Each strategy has its pros and cons, so it should be chosen to fit the characteristics of the data.
The biggest advantage of sharding is theoretically unlimited scalability. Adding shards increases storage capacity and throughput linearly. Also, because each shard is independent, a failure in one shard does not affect the entire system.
On the other hand, there are also disadvantages. Joins across shards are difficult, transaction management becomes complex, and resharding requires large-scale data migration. Tools such as MongoDB, Vitess (MySQL), and Citus (PostgreSQL) reduce the complexity of sharding.
# 애플리케이션 레벨 샤딩 예제 - Python
import hashlib
from typing import Dict, Any, List
import psycopg2
class ShardRouter:
"""샤드 라우터 - 데이터를 적절한 샤드로 라우팅"""
def __init__(self, shard_configs: List[Dict[str, str]]):
"""
Args:
shard_configs: 샤드별 연결 정보 리스트
[{"host": "shard1.db.com", "dbname": "app_db"}, ...]
"""
self.shards = []
self.num_shards = len(shard_configs)
for config in shard_configs:
conn = psycopg2.connect(**config)
self.shards.append(conn)
def get_shard_id(self, shard_key: str) -> int:
"""
해시 기반 샤드 ID 결정
일관된 해싱으로 같은 키는 항상 같은 샤드로
"""
hash_value = int(hashlib.md5(str(shard_key).encode()).hexdigest(), 16)
return hash_value % self.num_shards
def get_connection(self, shard_key: str):
"""샤드 키에 해당하는 DB 커넥션 반환"""
shard_id = self.get_shard_id(shard_key)
return self.shards[shard_id]
def execute_on_shard(self, shard_key: str, query: str, params: tuple = None):
"""특정 샤드에서 쿼리 실행"""
conn = self.get_connection(shard_key)
with conn.cursor() as cur:
cur.execute(query, params)
conn.commit()
return cur.fetchall() if cur.description else None
def execute_on_all_shards(self, query: str, params: tuple = None) -> List:
"""모든 샤드에서 쿼리 실행 (집계 등)"""
results = []
for shard in self.shards:
with shard.cursor() as cur:
cur.execute(query, params)
if cur.description:
results.extend(cur.fetchall())
return results
class UserService:
"""사용자 서비스 - 샤딩 적용"""
def __init__(self, router: ShardRouter):
self.router = router
def create_user(self, user_id: str, name: str, email: str):
"""
사용자 생성 - user_id가 샤드 키
같은 user_id는 항상 같은 샤드에 저장됨
"""
query = """
INSERT INTO users (user_id, name, email, created_at)
VALUES (%s, %s, %s, NOW())
"""
self.router.execute_on_shard(user_id, query, (user_id, name, email))
print(f"User {user_id} created on shard {self.router.get_shard_id(user_id)}")
def get_user(self, user_id: str) -> Dict[str, Any]:
"""사용자 조회 - 샤드 키로 정확한 샤드 접근"""
query = "SELECT * FROM users WHERE user_id = %s"
result = self.router.execute_on_shard(user_id, query, (user_id,))
return result[0] if result else None
def get_user_count(self) -> int:
"""전체 사용자 수 - 모든 샤드에서 집계"""
query = "SELECT COUNT(*) FROM users"
results = self.router.execute_on_all_shards(query)
return sum(row[0] for row in results)
# 일관된 해싱 (Consistent Hashing) 구현
class ConsistentHashRing:
"""
일관된 해싱 - 샤드 추가/제거 시 최소한의 데이터 이동
"""
def __init__(self, nodes: List[str], virtual_nodes: int = 150):
self.ring = {}
self.sorted_keys = []
self.virtual_nodes = virtual_nodes
for node in nodes:
self.add_node(node)
def _hash(self, key: str) -> int:
return int(hashlib.md5(key.encode()).hexdigest(), 16)
def add_node(self, node: str):
"""노드 추가 - 가상 노드로 균등 분산"""
for i in range(self.virtual_nodes):
virtual_key = f"{node}:{i}"
hash_key = self._hash(virtual_key)
self.ring[hash_key] = node
self.sorted_keys.append(hash_key)
self.sorted_keys.sort()
def remove_node(self, node: str):
"""노드 제거"""
for i in range(self.virtual_nodes):
virtual_key = f"{node}:{i}"
hash_key = self._hash(virtual_key)
del self.ring[hash_key]
self.sorted_keys.remove(hash_key)
def get_node(self, key: str) -> str:
"""키에 해당하는 노드 찾기"""
if not self.ring:
return None
hash_key = self._hash(key)
# 이진 탐색으로 적절한 노드 찾기
for ring_key in self.sorted_keys:
if hash_key <= ring_key:
return self.ring[ring_key]
# 링의 처음으로 순환
return self.ring[self.sorted_keys[0]]
# 사용 예시
if __name__ == "__main__":
# 4개 샤드 설정
shard_configs = [
{"host": "shard1.db.com", "dbname": "app", "user": "app"},
{"host": "shard2.db.com", "dbname": "app", "user": "app"},
{"host": "shard3.db.com", "dbname": "app", "user": "app"},
{"host": "shard4.db.com", "dbname": "app", "user": "app"},
]
router = ShardRouter(shard_configs)
user_service = UserService(router)
# 사용자 생성 - 자동으로 적절한 샤드에 저장
user_service.create_user("user_001", "Alice", "alice@example.com")
user_service.create_user("user_002", "Bob", "bob@example.com")
// MongoDB 샤딩 예제 - Node.js
const { MongoClient } = require('mongodb');
/**
* MongoDB 샤드 클러스터 연결 및 사용
* mongos (라우터)에 연결하면 자동으로 샤딩 처리됨
*/
class ShardedMongoService {
constructor(mongosUri) {
// mongos 라우터에 연결
this.client = new MongoClient(mongosUri);
this.db = null;
}
async connect() {
await this.client.connect();
this.db = this.client.db('ecommerce');
console.log('Connected to sharded MongoDB cluster');
}
/**
* 샤드 컬렉션 설정
* 실제로는 mongosh에서 설정하지만, 예시로 포함
*/
async setupSharding() {
const adminDb = this.client.db('admin');
// 데이터베이스 샤딩 활성화
await adminDb.command({ enableSharding: 'ecommerce' });
// 컬렉션 샤딩 - user_id를 해시 샤드 키로
await adminDb.command({
shardCollection: 'ecommerce.orders',
key: { user_id: 'hashed' } // 해시 샤딩
});
// Range 샤딩 예시
await adminDb.command({
shardCollection: 'ecommerce.products',
key: { category: 1, product_id: 1 } // 복합 샤드 키
});
}
/**
* 주문 생성 - user_id 기반으로 자동 샤드 라우팅
*/
async createOrder(userId, items, totalAmount) {
const order = {
user_id: userId, // 샤드 키
items,
total_amount: totalAmount,
status: 'pending',
created_at: new Date()
};
// mongos가 user_id 해시값으로 적절한 샤드 선택
const result = await this.db.collection('orders').insertOne(order);
console.log(`Order created: ${result.insertedId}`);
return result.insertedId;
}
/**
* 사용자의 모든 주문 조회
* 샤드 키(user_id) 포함 쿼리 - 단일 샤드만 접근 (효율적)
*/
async getOrdersByUser(userId) {
// user_id가 샤드 키이므로 targeted query
const orders = await this.db.collection('orders')
.find({ user_id: userId })
.sort({ created_at: -1 })
.toArray();
console.log(`Found ${orders.length} orders for user ${userId}`);
return orders;
}
/**
* 날짜 범위 주문 조회
* 샤드 키 없는 쿼리 - 모든 샤드 스캔 (scatter-gather)
*/
async getOrdersByDateRange(startDate, endDate) {
// 샤드 키 없음 - 모든 샤드에서 검색 (비효율적)
const orders = await this.db.collection('orders')
.find({
created_at: {
$gte: startDate,
$lte: endDate
}
})
.toArray();
console.log(`Scatter-gather query: ${orders.length} orders found`);
return orders;
}
/**
* 샤드 분산 상태 확인
*/
async getShardDistribution() {
const stats = await this.db.collection('orders').stats();
console.log('Shard Distribution:');
console.log(` Total documents: ${stats.count}`);
console.log(` Total size: ${(stats.size / 1024 / 1024).toFixed(2)} MB`);
// 각 샤드별 청크 정보
const configDb = this.client.db('config');
const chunks = await configDb.collection('chunks')
.find({ ns: 'ecommerce.orders' })
.toArray();
const shardCounts = {};
chunks.forEach(chunk => {
shardCounts[chunk.shard] = (shardCounts[chunk.shard] || 0) + 1;
});
console.log(' Chunks per shard:', shardCounts);
return shardCounts;
}
/**
* 집계 쿼리 - mapReduce 또는 aggregation
*/
async getOrderStats() {
// Aggregation pipeline - 모든 샤드에서 실행 후 mongos가 병합
const stats = await this.db.collection('orders').aggregate([
{
$group: {
_id: '$status',
count: { $sum: 1 },
total_revenue: { $sum: '$total_amount' }
}
},
{ $sort: { count: -1 } }
]).toArray();
return stats;
}
}
// Zone 샤딩 (지역 기반 샤딩) 예시
async function setupZoneSharding(adminDb) {
// 샤드를 지역 존에 할당
await adminDb.command({
addShardToZone: 'shard-seoul',
zone: 'asia'
});
await adminDb.command({
addShardToZone: 'shard-virginia',
zone: 'americas'
});
// 존 범위 설정 - 지역별로 데이터 배치
await adminDb.command({
updateZoneKeyRange: 'ecommerce.users',
min: { region: 'asia', user_id: MinKey },
max: { region: 'asia', user_id: MaxKey },
zone: 'asia'
});
}
// 사용 예시
async function main() {
const service = new ShardedMongoService(
'mongodb://mongos1.example.com:27017,mongos2.example.com:27017'
);
await service.connect();
// 주문 생성 - 자동으로 적절한 샤드에 저장
await service.createOrder('user_123', [
{ product_id: 'prod_1', quantity: 2 }
], 50000);
// 샤드 분산 상태 확인
await service.getShardDistribution();
}
main().catch(console.error);
-- Citus (PostgreSQL 샤딩 확장) 예제
-- ============================================
-- 1. 분산 테이블 생성
-- ============================================
-- Citus 확장 활성화
CREATE EXTENSION citus;
-- 워커 노드 추가
SELECT citus_add_node('worker1.db.com', 5432);
SELECT citus_add_node('worker2.db.com', 5432);
SELECT citus_add_node('worker3.db.com', 5432);
-- 분산 테이블 생성 (해시 샤딩)
CREATE TABLE orders (
order_id BIGSERIAL,
user_id BIGINT NOT NULL, -- 샤드 키
product_id INT NOT NULL,
quantity INT NOT NULL,
total_amount DECIMAL(12,2),
status VARCHAR(20) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (user_id, order_id) -- 샤드 키 포함 필수
);
-- user_id를 샤드 키로 분산 (32개 샤드)
SELECT create_distributed_table('orders', 'user_id');
-- ============================================
-- 2. 레퍼런스 테이블 (모든 노드에 복제)
-- ============================================
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
name VARCHAR(200),
category VARCHAR(50),
price DECIMAL(10,2)
);
-- 모든 워커에 전체 복제 (조인 성능 향상)
SELECT create_reference_table('products');
-- ============================================
-- 3. 샤드 키 포함 쿼리 (단일 샤드 접근)
-- ============================================
-- 효율적: user_id(샤드 키)로 필터링
SELECT * FROM orders
WHERE user_id = 12345
ORDER BY created_at DESC;
-- 효율적: 샤드 키 포함 조인
SELECT o.*, p.name as product_name
FROM orders o
JOIN products p ON o.product_id = p.product_id
WHERE o.user_id = 12345;
-- ============================================
-- 4. 크로스 샤드 쿼리 (모든 샤드 스캔)
-- ============================================
-- 비효율적: 샤드 키 없는 쿼리 (scatter-gather)
SELECT * FROM orders
WHERE created_at > '2024-01-01';
-- 집계 쿼리 - Citus가 분산 실행 후 병합
SELECT
DATE_TRUNC('day', created_at) as order_date,
COUNT(*) as order_count,
SUM(total_amount) as daily_revenue
FROM orders
WHERE created_at >= '2024-01-01'
GROUP BY DATE_TRUNC('day', created_at)
ORDER BY order_date;
-- ============================================
-- 5. Co-location (같은 샤드 키로 관련 테이블 배치)
-- ============================================
CREATE TABLE order_items (
item_id BIGSERIAL,
order_id BIGINT,
user_id BIGINT NOT NULL, -- 같은 샤드 키
product_id INT,
quantity INT,
price DECIMAL(10,2),
PRIMARY KEY (user_id, item_id)
);
-- orders와 같은 샤드에 배치 (Co-location)
SELECT create_distributed_table('order_items', 'user_id',
colocate_with => 'orders');
-- Co-located 조인 - 로컬 조인으로 매우 효율적
SELECT o.order_id, COUNT(oi.item_id) as item_count
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
AND o.user_id = oi.user_id -- 샤드 키 조인 필수!
WHERE o.user_id = 12345
GROUP BY o.order_id;
-- ============================================
-- 6. 샤드 밸런싱 및 모니터링
-- ============================================
-- 샤드별 데이터 분포 확인
SELECT
nodename,
count(*) as shard_count,
pg_size_pretty(sum(shard_size)) as total_size
FROM citus_shards
JOIN citus_stat_statements ON shard_id = shardid
GROUP BY nodename;
-- 샤드 리밸런싱
SELECT citus_rebalance_start();
-- 핫스팟 샤드 확인
SELECT
shardid,
nodename,
shard_size,
total_time_ms
FROM citus_shards s
JOIN (
SELECT shardid, sum(total_time) as total_time_ms
FROM citus_stat_statements
GROUP BY shardid
) q ON s.shardid = q.shardid
ORDER BY total_time_ms DESC
LIMIT 10;
-- ============================================
-- 7. 리샤딩 (샤드 수 변경)
-- ============================================
-- 샤드 수 조정 (다운타임 없이)
SELECT alter_distributed_table('orders', shard_count := 64);
"As the number of users exceeds 100 million, a single DB has reached its limit. If we use user_id as the shard key and split into 8 shards, each shard will handle about 12 million users. It would be good to co-locate all user-related data in the same shard to maintain join performance."
"The reason this query is slow is that it searches only by date without a shard key, so it scans all shards. We should avoid scatter-gather queries. Let's solve it by adding user_id to the condition or creating a separate table for statistics."
"A hotspot has occurred on shard 3. A specific large customer is concentrated on this shard. We need to consider resharding with consistent hashing, or separating large customers into a separate shard."
Using a low-cardinality column (e.g., gender, country) as the shard key causes data to be distributed unevenly. Choose an evenly distributed column with high cardinality.
Transactions spanning multiple shards require 2PC (Two-Phase Commit), which significantly degrades performance. Place related data in the same shard (co-location).
When the amount of data is small, sharding only increases complexity. First consider vertical scaling (scale-up) of a single DB, and shard only when it is truly necessary.
Provision a generous initial number of shards, enforce that applications always include the shard key, and detect hotspots early through monitoring.