easy mysql database sharding with cubrid shard - 2013 percona

44
Easy MySQL Database Sharding with CUBRID SHARD Esen Sagynov 6/7/22

Upload: cubrid

Post on 15-Jan-2015

2.493 views

Category:

Technology


8 download

DESCRIPTION

If you ask companies who operate mission-critical services, they will tell: 1) that a relational database system is still the best choice for mission-critical data; 2) that service availability is more important than performance; 3) that high performance is good, but predictable performance is the king. This is a fact, and we know it. At NHN we have over 30,000 Web servers that operate over 150 large scale Web and mobile services. At such scale we must know what scales, how to provide high-availability and operate at predictable speed. At Percona Live MySQL Conference 2013 I will talk about CUBRID SHARD, a universal database sharding solution for CUBRID, MySQL, and Oracle. CUBRID SHARD can be used with a heterogeneous database backend, i.e. some shards can be stored in CUBRID, some in MySQL or even Oracle. At NHN we deploy various combinations: MySQL only, MySQL + Oracle, MySQL + CUBRID, CUBRID only, and Oracle only. I will explain how DBAs can easily configure it, and how we have implemented this feature. CUBRID SHARD allows to store unlimited number of database shards and distribute data based on modulo, DATETIME, or hash/range calculations. The developers can even feed in their own library to calculate the SHARD_ID using some complicated custom algorithm. At the session I will show how easy it is to setup all this. No need for a third-party management tool. With CUBRID SHARD application developers do not need to modify the application logic to provide data sharding. This is DBAs job as all this is handled by the database system automatically. CUBRID SHARD is designed to be very efficient. It provides built-in (*) distributed load balancing and (*) connection and statement pooling. At the conference I will present several cases where CUBRID SHARD is deployed as a shard manager and a connection manager, or where it's used as a way for seamless data migration between different systems. Who should come to the session? If you run a service which spends money on a database solution, on tools you need to shard databases or manage connections, you should come and learn how CUBRID SHARD can provide your applications native scale-out through single database view.

TRANSCRIPT

Page 1: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Easy MySQL Database Shardingwith CUBRID SHARD

Esen SagynovApril 10, 2023

Page 2: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

2

Today

1. About NHN2. Sharding in Production3. Why CUBRID SHARD4. How to shard MySQL databases5. DEMO6. CUBRID SHARD in Ndrive

Page 3: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

About me3

• Esen Sagynov (NHN Corp.)

– @CUBRID– fb.com/cubrid

[email protected]

Page 4: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

30,000+Web Servers

Japan

USA

Korea

China 150+ Web Services

About NHN

Vietnam

Singapore

Page 5: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Sharding in Production

• Uses RDBMS with Sharding• Data is stored as simple Key-Value.

• Uses RDBMS with Sharding• Sharding and Replication is abstracted through Gizzard

• Uses RDBMS with Sharding• Hbase usage is limited

• Uses RDBMS to store data• Data caching in a variety of ways

• Uses RDBMS with Sharding• ACID is the reason to use RDBMS

• Uses RDBMS with Sharding• Easier to implement, best suits their needs

• Uses RDBMS with Sharding and HA• Data consistency and relationship are the reason

Page 6: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Sharding Solutions

Name Type Requirements Interface

DB ETC

Hibernate shards AS frameworkDBMS w/Hibernatesupport

- Hibernate- JVM Java

HiveDB AS framework MySQL - Hibernate- JVM Java

dbShards AS & Middleware MySQL Java, C, PHP, Python, Ruby

Gizzard (Twitter) Middleware Any storage - JVM Java

Spider for MySQL Middleware &Storage Engine MySQL Any

Spock Proxy Middleware MySQL Any

Shard-Query Middleware MySQL PHP, RESTful API

CUBRID SHARD Middleware- CUBRID- MySQL- Oracle

Any

Page 7: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Sharding Solution Categories

• Application layer• Storage layer• Heavy middleware• Lightweight middleware

Page 8: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

8

Application & Storage Layers

Application Layer• Hibernate Shards• HiveDB

Disadvantage• Requires Hibernate/Java• Uses many XML files for configuration• Not for running services

Storage Layer• Spider for MySQL

Disadvantage• Requires to change storage

engine• Not for running services

Page 9: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

9

Heavy Middleware

dbShards Gizzard• Requires to change application code• Requires agents to be installed on

each DB server• Not for running services

• Not active

Page 10: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

10

Lightweight Middleware

• Spock Proxy– Active project– Lightweight– Flexible– Easy to configure– No application change

Page 11: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

11

Spock Proxy

Spock ProxySharding rule storage DatabaseSharding strategy ModuloDetermine Sharding Key Full SQL ParsingStrength No need to change SQLWeakness • Performance degradation:

• Extra SQL parsing• Resultset merging

• Not all MySQL SQL is supported• Single threaded

Blog post: http://www.cubrid.org/blog/dev-platform/database-sharding-platform-at-nhn/

Page 12: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

12

Spock Proxy Performance

• Single threaded• Parses and rewrites SQL

1 5 10 20 30 50 70 100 200 4000

50000100000150000200000250000300000350000400000450000500000

App ShardingSpock ProxyCUBRID SHARD

Concurrent clients

Exec. time

Page 13: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Spock Proxy

Active project Lightweight Flexible Easy to configure No application change✕No performance impact

Page 14: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

14

Lightweight, Easy to ConfigureSharding Middleware

CUBRID SHARD

Page 15: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

15

Spock Proxy vs. CUBRID SHARD

Spock Proxy CUBRID SHARD

Sharding rule storage Database Configuration file

Sharding strategy Modulo • Modulo• User defined hash function

Determine Sharding Key Full SQL Parsing SQL Hint Search

Strength No need to change SQL • Supports CUBRID and MySQL• Full MySQL SQL support• Higher performance

• No SQL parsing• Multi-threaded• Connection pooling• Load balancing

• Custom sharding strategy• Easy configuration

Weakness • Performance degradation:• Extra SQL parsing• Resultset merging

• Supports MySQL only• Not all MySQL SQL is supported• Single threaded

• Requires to change SQL queries to insert the sharding hint

Blog post: http://www.cubrid.org/blog/dev-platform/database-sharding-platform-at-nhn/

Page 16: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

CUBRID Facts

RDBMS True Open Source @ www.cubrid.org Optimized for Web services High performance Large DB support High-Availability feature DB Sharding support 90+% MySQL compatible SQL syntax + Oracle analytical functions ACID Transactions Online Backup Supported by NHN Corporation

Page 17: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

SHARD Broker

CUBRID SHARD Architecture

APIC, JDBC, ADO.NET, OLEDB, ODBC,PHP, Perl, Python, Ruby, Node.js

BrokerCAS

CAS for MYSQL

CAS

Proxy

CAS for MYSQL

CUBRID

Server

ShardsMySQL

Server

CUBRID

Server

MySQL

Server

Single database view

OR

Page 18: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

SHARD Environment

APIC, JDBC, ADO.NET, OLEDB, ODBC,PHP, Perl, Python, Ruby, Node.js

SHARD Broker 1

ShardsMySQ

L Serve

r 3

MySQL

Server N

SHARD Broker N

SHARD Broker 2

MySQL

Server 1

MySQL

Server 2

……

Page 19: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Installing CUBRID SHARD is easy!

Page 21: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Configuring is very easy and intuitive!

Page 22: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

23

Configuration Steps

• Create1. Shards2. Database Users3. Database Schema

4. Configure CUBRID SHARD– shard database information– backend shards connection information– sharding strategy

5. Start CUBRID SHARD6. Change application code

– connection URL– shard hint shard #0 shard #1 shard #2 shard #N

CUBRID SHARD

Client app

Page 23: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 1. Create Shards

• Host 1..N:

$> mysql –ushard -ppassword –hnode1mysql> CREATE DATABASE sharddb;

Page 24: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 2. Create Users

• Host 1..N:

$> mysql –ushard -ppassword –hnode1mysql> USE mysql;mysql> GRANT ALL PRIVILEGES ON sharddb@localhost TO shard@localhost IDENTIFIED BY ‘shard123’mysql> GRANT ALL PRIVILEGES ON sharddb@localhost TO shard@shardBrokerNode IDENTIFIED BY ‘shard123’

Page 25: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 3. Create same tables

$> mysql –ushard -ppassword –hnode1mysql> USE sharddb;mysql> CREATE TABLE tbl_users (id BIGINT PRIMARY KEY, name VARCHAR(20), age SMALLINT)

$> mysql –ushard -ppassword –hnode2…

• Host 1..N:

Page 26: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 4. Simple Configuration

• shard.conf– Main configuration file for CUBRID SHARD.

• shard_connection.txt– Predefined list of shard IDs, database and host

names for CUBRID/MySQL.• shard_keys.txt– A list of shard_key_columns and their mapping

with shard_id

Doc page: http://www.cubrid.org/manual/91/en/shard.html#configuration-and-setup

Page 27: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

shard.conf

Set:1. SHARD_DB_NAME2. SHARD_DB_USER3. SHARD_DB_PASSWORD4. APPL_SERVER

…SHARD_DB_NAME = sharddbSHARD_DB_USER = shardSHARD_DB_PASSWORD = shard123APPL_SERVER = CAS_MYSQL…

Doc page: http://www.cubrid.org/manual/91/en/shard.html#default-configuration-file-shard-conf

Page 28: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

shard_connection.txt

Set:1. Shard ID2. Real database name3. Remote/local host name

# shard-id real-db-name connection-info0 sharddb mysqlA:33061 sharddb mysqlB:33062 sharddb mysqlC:3306…

** Host names must be identical to the output of hostname command of every node.

Doc page: http://www.cubrid.org/manual/91/en/shard.html#setting-shard-metadata

Page 29: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

shard_keys.txt

Set:1. Min shard key2. Max shard key3. Shard ID

[%student_no]# min max shard_id0 63 064 127 1128 191 2192 255 3

** Default sharding strategy is to apply modulo 256 (SHARD_KEY_MODULAR in shard.conf ).

Doc page: http://www.cubrid.org/manual/91/en/shard.html#setting-shard-metadata

Page 30: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Custom Library

int fn_shard_key_udf(int type, void *val){ int mod = 2;

if (val == NULL) {

return ERROR_ON_ARGUMENT; }

switch(type) {

case SHARD_U_TYPE_INT:{ int ival; ival = (int) (*(int *)val); return ival % 2;} break;case SHARD_U_TYPE_STRING: return ERROR_ON_MAKE_SHARD_KEY;default: return ERROR_ON_ARGUMENT;

} return ERROR_ON_MAKE_SHARD_KEY;}

shard.conf1. SHARD_KEY_LIBRARY_NAME2. SHARD_KEY_FUNCTION_NAME

[%student_no]SHARD_KEY_LIBRARY_NAME=$CUBRID/conf/shard_key_udf.soSHARD_KEY_FUNCTION_NAME =fn_shard_key_udf

Doc page: http://www.cubrid.org/manual/91/en/shard.html#setting-user-defined-hash-function

Page 31: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 5. Start CUBRID SHARD

$> cubrid shard start@ cubrid shard start ++cubrid shard start: success

Page 32: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

# 6. Connection URL

connectionURL ="jdbc:cubrid:localhost:45511:sharddb:shard:shard123:

?althosts=node2:port2,node3:port3

&loadBalance=true";

DB name

Password

Shard broker port

UsernameAPI level failover SHARD nodes

API levelload balancing

Page 33: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Querying Shards

SELECT name FROM student WHERE student_no = /*+ shard_key */ ?;

SQL hint

Shard key column

• bind variable• fixed value

Page 34: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

Types of SQL Hints

SQL Hints Description

/*+ shard_key */ a hint to specify the location of - a bind variable - or the literal valuewhich corresponds to the shard key column

/*+ shard_val(value) */

a hint to explicitly specify the shard key in case thecolumn that corresponds to the shard key does notexist in a query

/*+ shard_id(shard_id) */

A hints which can be used to directly processuser queries on a particular shard

Page 35: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

String query = "SELECT name FROM student WHERE student_no = /*+ shard_key */ ?; ";PrepareStatement query_stmt = connection.prepareStatement(query);query_stmt.setInt(1,100);ResultSet rs = query_stmt.executeQuery();// fetch resultset

2. Query hint analysis3. shard_key hashing4. Passing the (unchanged)

query to the selectedshard.

CUBRID

SHARD

shard #0 shard #1 shard #2 shard #3

key_columnrange

(hash result) shard_idmin max

student_no 0 63 0

student_no 64 127 1

student_no 128 191 2

student_no 192 255 3

Shard selection

1. Execute queryClient app

Page 36: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

MySQL Sharding DEMO

Requirements:

• 1GB free RAM• 3GB free space for 2 VMs• VirtualBox• Vagrant

Page 37: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

39

MySQL Sharding DEMO

https://github.com/kadishmal/cubrid-shard-demo

Page 38: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

CUBRID SHARD

• Easy– No configuration hassle– No “moving parts”

• Reliable– High performance– No SPOF

• Open source– Supported by NHN

Page 39: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

CUBRID SHARD Disadvantages

Need to alter SQL to add HintsNo Data Rebalancing

Need to carefully plan the sharding strategy in advance.

No GUI monitoring tool. Only command line.

Page 40: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

43

CUBRID SHARD is great when…

• Services are already running and stable• But data is growing fast• And you need a stable solution• Quick installation and easy configuration• Time constraints

Page 41: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

44

Ndrive cloud storage service

• User files meta data• Sharding strategy by user ID• 24 master shards– Intel(R) Xeon(R) L5640 @ 2.27GHz * 8, 16G RAM, 820G HDD

• 10TB data• Load pattern:– 75~80% SELECT vs. 20~25% INSERT– Avg. ~3000 QPS/shard– Avg. ~5% CPU load/shard

Page 42: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

45

Ndrive cloud storage service

• 1 SHARD BROKER• 4 Proxies per Broker• 50 CAS per proxy

• No performance degradation after CUBRID SHARD is used

64 128 192 256 3200

10

20

30

40

50

60

70

80

90

0

2000

4000

6000

8000

10000

12000

14000

16000

SHARD vs. Broker Performance Comparison

Broker - broker cpuShard - proxy CPUBroker - Mean Time(ms)Shard - Mean Time(ms)Broker - RPSShard - RPS

Vuser

RP

S

Page 43: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

CUBRID SHARD Next

Auto-rebalancing in CUBRID SHARDCM shard monitoringAggregation feature

Page 44: Easy MySQL Database Sharding with CUBRID SHARD - 2013 Percona

47

Questions?

• Esen Sagynov (NHN Corp.)

– @CUBRID– fb.com/cubrid

[email protected]