TiDB
MySQL Compatible Distributed NewSQL Database
A MySQL-compatible distributed NewSQL database. Supports HTAP (Hybrid Transactional/Analytical Processing), handling OLTP and OLAP workloads simultaneously in a single database.
MySQL Compatible Distributed NewSQL Database
A MySQL-compatible distributed NewSQL database. Supports HTAP (Hybrid Transactional/Analytical Processing), handling OLTP and OLAP workloads simultaneously in a single database.
TiDB is an open-source distributed SQL database developed by PingCAP. It was designed with inspiration from Google's Spanner and F1 papers, and it fully supports the MySQL protocol, so existing MySQL applications can be migrated as they are.
HTAP (Hybrid Transactional/Analytical Processing): This is TiDB's core differentiator. It performs OLTP processing with TiKV (row store) and OLAP processing with TiFlash (columnar store) at the same time. In services that need real-time analytics, analytical queries can be run immediately without a separate data warehouse.
Horizontal Scaling: When nodes are added, data is automatically distributed and throughput increases. It can scale from several TB to hundreds of TB, and both writes and reads scale linearly.
Strong Consistency: It uses the Raft consensus algorithm to guarantee strong consistency even in distributed environments. It provides Snapshot Isolation by default, and you can choose between Pessimistic and Optimistic transaction modes.
Cloud Native: On Kubernetes, automated deployment, scaling, and backup/recovery are possible through TiDB Operator. A fully managed service is also provided through TiDB Cloud.
-- TiDB SQL 예제 (MySQL 호환)
-- ============================================
-- 기본 테이블 생성 (MySQL과 동일)
-- ============================================
CREATE DATABASE ecommerce;
USE ecommerce;
-- 사용자 테이블
CREATE TABLE users (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- 샤딩 힌트 (선택적)
INDEX idx_email (email)
) ENGINE=InnoDB;
-- 주문 테이블 (파티셔닝 예시)
CREATE TABLE orders (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT NOT NULL,
total_amount DECIMAL(15,2) NOT NULL,
status ENUM('pending', 'paid', 'shipped', 'delivered') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB
-- TiDB에서 Range 파티셔닝 (대용량 테이블 최적화)
PARTITION BY RANGE (YEAR(created_at)) (
PARTITION p2022 VALUES LESS THAN (2023),
PARTITION p2023 VALUES LESS THAN (2024),
PARTITION p2024 VALUES LESS THAN (2025),
PARTITION pmax VALUES LESS THAN MAXVALUE
);
-- ============================================
-- HTAP: TiFlash 컬럼 저장소 설정
-- ============================================
-- TiFlash 레플리카 추가 (실시간 분석용)
ALTER TABLE orders SET TIFLASH REPLICA 1;
ALTER TABLE users SET TIFLASH REPLICA 1;
-- TiFlash 동기화 상태 확인
SELECT * FROM information_schema.tiflash_replica;
-- ============================================
-- HTAP 쿼리 예시
-- ============================================
-- OLTP: 단순 조회 (TiKV 자동 사용)
SELECT * FROM orders WHERE user_id = 12345 LIMIT 10;
-- OLAP: 분석 쿼리 (TiFlash 자동 사용)
SELECT
DATE_FORMAT(created_at, '%Y-%m') AS month,
COUNT(*) AS order_count,
SUM(total_amount) AS total_sales,
AVG(total_amount) AS avg_order_value
FROM orders
WHERE created_at >= '2024-01-01'
GROUP BY month
ORDER BY month;
-- 강제로 TiFlash 사용 (힌트)
SELECT /*+ READ_FROM_STORAGE(TIFLASH[orders]) */
status, COUNT(*) AS cnt
FROM orders
GROUP BY status;
-- 강제로 TiKV 사용 (힌트)
SELECT /*+ READ_FROM_STORAGE(TIKV[orders]) */
* FROM orders WHERE id = 1;
-- ============================================
-- 분산 트랜잭션
-- ============================================
-- Pessimistic 트랜잭션 (기본값, MySQL과 동일)
START TRANSACTION;
UPDATE accounts SET balance = balance - 100
WHERE id = 'acc-001';
UPDATE accounts SET balance = balance + 100
WHERE id = 'acc-002';
-- 제약조건 확인
SELECT balance FROM accounts WHERE id = 'acc-001';
COMMIT;
-- ============================================
-- 유용한 시스템 쿼리
-- ============================================
-- 클러스터 상태 확인
SELECT * FROM information_schema.cluster_info;
-- 느린 쿼리 확인
SELECT * FROM information_schema.slow_query
ORDER BY query_time DESC LIMIT 10;
-- 리전 분포 확인
SELECT * FROM information_schema.tikv_region_status
WHERE table_name = 'orders';
-- 실행 계획 (분산 처리 확인)
EXPLAIN ANALYZE
SELECT * FROM orders WHERE user_id = 12345;
# TiDB + Python (mysql-connector 또는 pymysql)
import mysql.connector
from mysql.connector import pooling
from decimal import Decimal
from contextlib import contextmanager
# ============================================
# 연결 설정
# ============================================
# TiDB 연결 풀 (MySQL 드라이버 그대로 사용)
db_config = {
'host': 'tidb-host.example.com',
'port': 4000, # TiDB 기본 포트
'user': 'root',
'password': 'your_password',
'database': 'ecommerce',
'pool_name': 'tidb_pool',
'pool_size': 10,
'autocommit': False,
}
connection_pool = mysql.connector.pooling.MySQLConnectionPool(**db_config)
@contextmanager
def get_connection():
conn = connection_pool.get_connection()
try:
yield conn
finally:
conn.close()
# ============================================
# CRUD 작업
# ============================================
class OrderService:
def create_order(self, user_id: int, items: list) -> dict:
"""주문 생성 - 분산 트랜잭션"""
with get_connection() as conn:
cursor = conn.cursor(dictionary=True)
try:
# TiDB는 기본적으로 Pessimistic 트랜잭션
total = sum(item['price'] * item['quantity'] for item in items)
# 주문 생성
cursor.execute("""
INSERT INTO orders (user_id, total_amount, status)
VALUES (%s, %s, 'pending')
""", (user_id, total))
order_id = cursor.lastrowid
# 주문 아이템 생성
for item in items:
cursor.execute("""
INSERT INTO order_items (order_id, product_id, quantity, price)
VALUES (%s, %s, %s, %s)
""", (order_id, item['product_id'], item['quantity'], item['price']))
# 재고 차감
cursor.execute("""
UPDATE products
SET stock = stock - %s
WHERE id = %s AND stock >= %s
""", (item['quantity'], item['product_id'], item['quantity']))
if cursor.rowcount == 0:
raise ValueError(f"재고 부족: {item['product_id']}")
conn.commit()
return {'order_id': order_id, 'total': total}
except Exception as e:
conn.rollback()
raise e
def get_sales_analytics(self, start_date: str, end_date: str) -> list:
"""매출 분석 - TiFlash OLAP 쿼리"""
with get_connection() as conn:
cursor = conn.cursor(dictionary=True)
# TiFlash 힌트로 컬럼 저장소 강제 사용
cursor.execute("""
SELECT /*+ READ_FROM_STORAGE(TIFLASH[orders, order_items]) */
DATE_FORMAT(o.created_at, '%%Y-%%m-%%d') AS date,
COUNT(DISTINCT o.id) AS order_count,
SUM(o.total_amount) AS total_sales,
COUNT(oi.id) AS item_count
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
WHERE o.created_at BETWEEN %s AND %s
GROUP BY date
ORDER BY date
""", (start_date, end_date))
return cursor.fetchall()
# ============================================
# 재시도 로직 (분산 트랜잭션 충돌 대비)
# ============================================
import time
import random
def execute_with_retry(fn, max_retries=3):
"""트랜잭션 충돌 시 재시도"""
for attempt in range(max_retries):
try:
return fn()
except mysql.connector.Error as e:
# TiDB 트랜잭션 충돌 에러 코드
if e.errno in (9007, 8028) and attempt < max_retries - 1:
# 지수 백오프 + 지터
wait_time = (2 ** attempt) + random.uniform(0, 1)
print(f"재시도 {attempt + 1}/{max_retries}: {wait_time:.2f}초 대기")
time.sleep(wait_time)
continue
raise
# ============================================
# 사용 예시
# ============================================
order_service = OrderService()
# OLTP: 주문 생성
result = execute_with_retry(
lambda: order_service.create_order(
user_id=12345,
items=[
{'product_id': 1, 'quantity': 2, 'price': Decimal('10000')},
{'product_id': 2, 'quantity': 1, 'price': Decimal('25000')}
]
)
)
print(f"주문 생성: {result}")
# OLAP: 실시간 분석 (TiFlash)
analytics = order_service.get_sales_analytics('2024-01-01', '2024-12-31')
print(f"매출 분석: {analytics}")
# TiDB CLI 및 운영 명령어
# ============================================
# TiUP으로 로컬 설치 (개발/테스트)
# ============================================
# TiUP 설치 (TiDB 패키지 매니저)
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
# 환경변수 적용
source ~/.bashrc
# 로컬 클러스터 실행 (TiDB + TiKV + PD + TiFlash)
tiup playground --db 1 --kv 3 --pd 1 --tiflash 1
# MySQL 클라이언트로 접속
mysql -h 127.0.0.1 -P 4000 -u root
# ============================================
# 프로덕션 클러스터 배포
# ============================================
# 클러스터 배포 설정 파일 (topology.yaml)
cat > topology.yaml << 'EOF'
global:
user: "tidb"
ssh_port: 22
deploy_dir: "/tidb-deploy"
data_dir: "/tidb-data"
server_configs:
tidb:
log.slow-threshold: 300
pd_servers:
- host: 10.0.1.1
- host: 10.0.1.2
- host: 10.0.1.3
tidb_servers:
- host: 10.0.1.4
- host: 10.0.1.5
tikv_servers:
- host: 10.0.1.6
- host: 10.0.1.7
- host: 10.0.1.8
tiflash_servers:
- host: 10.0.1.9
EOF
# 클러스터 배포
tiup cluster deploy tidb-prod v7.5.0 ./topology.yaml --user root -p
# 클러스터 시작
tiup cluster start tidb-prod
# 클러스터 상태 확인
tiup cluster display tidb-prod
# ============================================
# 클러스터 관리
# ============================================
# 노드 스케일 아웃 (TiKV 추가)
tiup cluster scale-out tidb-prod scale-out.yaml
# 노드 스케일 인 (TiKV 제거)
tiup cluster scale-in tidb-prod --node 10.0.1.8:20160
# 설정 변경
tiup cluster edit-config tidb-prod
# 롤링 업그레이드
tiup cluster upgrade tidb-prod v7.6.0
# ============================================
# 백업 및 복구 (BR 도구)
# ============================================
# 전체 백업 (S3)
tiup br backup full \
--pd "10.0.1.1:2379" \
--storage "s3://bucket/backup" \
--s3.region "ap-northeast-2"
# 데이터베이스 백업
tiup br backup db \
--pd "10.0.1.1:2379" \
--db "ecommerce" \
--storage "s3://bucket/backup"
# 복구
tiup br restore full \
--pd "10.0.1.1:2379" \
--storage "s3://bucket/backup"
# ============================================
# 데이터 마이그레이션 (MySQL -> TiDB)
# ============================================
# TiDB Data Migration (DM) 설정
tiup dmctl --master-addr 10.0.1.1:8261 \
operate-source create ./source.yaml
# 마이그레이션 태스크 시작
tiup dmctl --master-addr 10.0.1.1:8261 \
start-task ./task.yaml
# ============================================
# 모니터링 (Grafana/Prometheus)
# ============================================
# TiDB Dashboard 접속 (기본 포트)
# http://PD_IP:2379/dashboard
# Grafana 접속 (TiUP으로 설치 시)
# http://GRAFANA_IP:3000
# 느린 쿼리 로그 확인
tiup ctl:v7.5.0 pd -u http://10.0.1.1:2379 log slow-query
"If we run analytical queries on MySQL for the real-time dashboard, it affects the service. If we move to TiDB, TiFlash handles the analytical queries, so we can do real-time analytics without putting load on the production DB. And without an ETL pipeline."
"TiDB is fully compatible with the MySQL protocol, so we only need to change the connection string, with no driver changes. We can migrate gradually while syncing MySQL in real time with the DM tool. We do need to check some MySQL-specific functions, though."
"This analytical query seems slow because it's going to TiKV. Check whether the TiFlash replica is configured, and try forcing it with a hint. Also check the sync status in information_schema.tiflash_replica."
AUTO_INCREMENT is sequential, so hotspots can occur. Using AUTO_RANDOM or SHARD_ROW_ID_BITS is recommended.
TiFlash uses asynchronous replication, so the latest data may be delayed. If real-time freshness is important, use a TiKV hint.
Stored procedures, triggers, and some functions are limited. Verify with a compatibility check tool before migrating.
Use TiFlash replicas for large tables, prevent hotspots with AUTO_RANDOM, run ANALYZE regularly, and configure at least 3 PD nodes.