πŸ—„οΈ Databases

Vitess

Vitess

MySQL horizontal scaling middleware. Developed at YouTube.

πŸ“– Detailed Explanation

Vitess is a database clustering system that enables horizontal scaling of MySQL. It was developed to handle large-scale MySQL workloads at YouTube, and is currently a graduated project of the CNCF (Cloud Native Computing Foundation).

Vitess's core features include automatic sharding (sharding logic defined with VSchema), query routing (applications use a single endpoint), connection pooling, query rewriting (optimizing complex queries), and online schema changes.

It is used to run MySQL at petabyte scale in large-scale services such as Slack, Square, GitHub, and HubSpot. It is optimized for Kubernetes environments.

πŸ’» Code Example

# Vitess VSchema μ˜ˆμ‹œ (샀딩 μ„€μ •)
# 파일: vschema.json
{
  "sharded": true,
  "vindexes": {
    "hash": {
      "type": "hash"
    }
  },
  "tables": {
    "users": {
      "column_vindexes": [
        {
          "column": "user_id",
          "name": "hash"
        }
      ]
    },
    "orders": {
      "column_vindexes": [
        {
          "column": "user_id",
          "name": "hash"
        }
      ]
    }
  }
}

# vtgate μ—°κ²° (MySQL ν˜Έν™˜ ν”„λ‘œν† μ½œ)
mysql -h vtgate-host -P 15306 -u user -p

# μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œλŠ” 일반 MySQL λ“œλΌμ΄λ²„ μ‚¬μš©
import mysql.connector

conn = mysql.connector.connect(
    host="vtgate-host",
    port=15306,
    user="app_user",
    database="commerce"
)

# μΏΌλ¦¬λŠ” Vitessκ°€ μžλ™μœΌλ‘œ μƒ€λ“œμ— λΌμš°νŒ…
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE user_id = 12345")
cursor.execute("SELECT * FROM orders WHERE user_id = 12345")  # 같은 μƒ€λ“œ

πŸ—£οΈ Examples of Practical Conversations

CTO: "We can't scale up any further with a single MySQL instance. What options do we have?"

DBA: "If we adopt Vitess, we can shard while keeping the existing MySQL. And we can keep application code changes to a minimum."

CTO: "How is that different from sharding it ourselves?"

DBA: "If you declare the sharding logic with VSchema, Vitess automatically handles query routing, join processing, and rebalancing. The operational burden is greatly reduced."

Interviewer: "How did you implement MySQL sharding?"

Candidate: "I adopted Vitess and applied hash sharding based on user_id. I defined the sharding key with VSchema, and vtgate routes queries to the appropriate shards."

Interviewer: "How do you handle cross-shard queries?"

Candidate: "Vitess gathers results from multiple shards using a scatter-gather approach. However, for performance, I designed queries as single-shard queries whenever possible, and separated cross-shard queries into asynchronous processing."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "Looking at this query, there's no user_id in the WHERE clause, so it looks like it will scan all shards."

Developer: "Oh, I didn't include the sharding key in the lookup condition."

Reviewer: "In Vitess, queries without a sharding key are broadcast to all shards. Add a user_id condition if possible, or if that's unavoidable, put a cache layer in front."

Developer: "Understood. I'll review the API design again from the beginning."

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More