DEV Community

Alex Spinov
Alex Spinov

Posted on

TiDB Has a Free API — Here's How to Use It for Distributed SQL

TL;DR

TiDB is an open-source, MySQL-compatible distributed SQL database that scales horizontally. TiDB Cloud offers a free Serverless tier with a generous API — perfect for developers building scalable applications without managing infrastructure.

What Is TiDB?

TiDB (Titanium Database) is a distributed SQL database built by PingCAP. It combines the best of both worlds:

  • MySQL compatibility — drop-in replacement for MySQL
  • Horizontal scaling — automatically distributes data across nodes
  • HTAP (Hybrid Transactional/Analytical Processing) — run OLTP and OLAP in one database
  • Strong consistency — distributed transactions with Snapshot Isolation

TiDB Cloud Serverless — The Free Tier

TiDB Cloud Serverless gives you:

  • 25 GiB storage free
  • 250 million Request Units/month free
  • Automatic scaling — scales to zero when idle
  • Built-in vector search — AI-ready out of the box
  • MySQL protocol — use any MySQL client or ORM

Quick Start with the Data API

TiDB Serverless provides a Data API (beta) that lets you query your database via HTTP:

# Create a cluster at tidbcloud.com, then use the Data API
curl --digest -u 'YOUR_PUBLIC_KEY:YOUR_PRIVATE_KEY' \
  --request POST \
  --url "https://YOUR_HOST/v1beta/app/chat2query/YOUR_CLUSTER_ID/endpoint/chat2data" \
  --header 'content-type: application/json' \
  --data '{
    "cluster_id": "YOUR_CLUSTER_ID",
    "database": "test",
    "tables": ["users"],
    "instruction": "Find all active users"
  }'
Enter fullscreen mode Exit fullscreen mode

Using TiDB with Node.js

Since TiDB is MySQL-compatible, use any MySQL driver:

import mysql from 'mysql2/promise';

const connection = await mysql.createConnection({
  host: 'gateway01.us-east-1.prod.aws.tidbcloud.com',
  port: 4000,
  user: 'YOUR_USER',
  password: 'YOUR_PASSWORD',
  database: 'test',
  ssl: { minVersion: 'TLSv1.2' }
});

// Regular SQL — it's just MySQL!
const [rows] = await connection.execute(
  'SELECT * FROM users WHERE active = ? LIMIT 10',
  [true]
);

console.log(rows);
Enter fullscreen mode Exit fullscreen mode

Vector Search for AI Applications

TiDB now has built-in vector search — no separate vector database needed:

-- Create a table with vector column
CREATE TABLE documents (
  id INT PRIMARY KEY AUTO_INCREMENT,
  content TEXT,
  embedding VECTOR(1536)  -- OpenAI embedding dimension
);

-- Find similar documents
SELECT id, content,
  VEC_COSINE_DISTANCE(embedding, '[0.1, 0.2, ...]') AS distance
FROM documents
ORDER BY distance
LIMIT 5;
Enter fullscreen mode Exit fullscreen mode

TiDB vs Other Distributed Databases

Feature TiDB CockroachDB PlanetScale Neon
MySQL Compatible ❌ (PostgreSQL) ❌ (PostgreSQL)
Free Tier 25 GiB 10 GiB ❌ (removed) 0.5 GiB
Vector Search ✅ Built-in ✅ pgvector
HTAP
Horizontal Scale ✅ Auto

Use Cases

  1. SaaS applications needing MySQL compatibility + horizontal scaling
  2. AI/ML pipelines using vector search for RAG and similarity search
  3. Real-time analytics on transactional data (HTAP)
  4. Multi-tenant platforms with automatic data distribution
  5. Migration from MySQL — minimal code changes needed

Resources


Need to extract and process data at scale before loading into TiDB? Check out my web scraping tools on Apify — extract data from any website and pipe it directly into your TiDB database. Questions? Email me at spinov001@gmail.com

Top comments (0)