TiDB Cloud Serverless Driver (Beta): Seamless Database Connectivity in Edge Environments

Author: Yuhang Shi, Xiang Zhang, Arun Vijayraghavan

We are excited to unveil TiDB Cloud Serverless Driver (Beta) for JavaScript, your gateway to seamless database connectivity in edge computing environments. With this driver, you can now effortlessly connect to TiDB Serverless from edge environments such as Cloudflare Workers, Vercel Edge Functions, and Netlify Edge Functions.

TiDB Serverless is a fully managed cloud Database-as-a-Service (DBaaS) for auto-scaling workloads built with TiDB, an advanced open-source, distributed SQL database with MySQL compatibility. This offering not only facilitates easy connectivity but also empowers you to leverage the robust capabilities of TiDB, ensuring optimal performance and scalability for your database operations in edge computing scenarios.

Challenges Embracing Database Connections with Edge Computing

Edge computing is a paradigm that aims to minimize latency by bringing computing resources as close to end users as possible. Many modern serverless platforms offer edge computing solutions, such as Vercel Edge Function, Cloudflare Workers, and Netlify Edge Function. These platforms leverage a global edge network to run the code in proximity to the end user to reduce network latency and increase performance significantly.
Previously, applications connected to TiDB Serverless using traditional MySQL drivers like mysql2. However, when it comes to edge computing, achieving fine-grained sandboxing and ultra-low latency typically requires using JavaScript runtimes and engines other than Node.js, such as the V8 engine. These runtimes and engines often lack robust TCP support and full Node.js compatibility. Consequently, traditional MySQL drivers fall short of expectations for edge environments.

Serverless Driver: A Purpose-built Driver for Edge Database Connectivity

To address the database connectivity challenges in edge environments, we have developed the TiDB Cloud Serverless Driver, the JavaScript driver purpose-built for edge environments. It offers several key benefits:

  • Improved performance: The serverless driver is optimized for serverless settings, delivering superior performance.
  • Secure data transfer: The serverless driver facilitates secure data extraction and insertion from TiDB Serverless in edge environments via HTTPS.
  • Familiar user experience: The serverless driver provides an easy-to-use and familiar experience for developers who are used to traditional MySQL drivers, ensuring a smooth transition.

Beyond the driver specifications, edge functions require a database capable of managing tens of thousands of connections—a demand that TiDB Serverless meets effortlessly. TiDB Serverless, coupled with a serverless driver, overcomes the limitations inherent to edge functions and effectively bridges the gap between edge computing and databases in serverless environments.

How Do Serverless Drivers Work?

The serverless driver is a lightweight wrapper for the gateway, an internal component that provides HTTPS API.


Figure 1. Serverless Driver Work Flow

By initiating HTTPS requests to the gateway, the serverless driver provides user-friendly interfaces and configurations to ensure a seamless conversion of database data types to JavaScript data types. Upon receiving the HTTPS request, the gateway establishes communication with the corresponding TiDB instance over a TCP connection.

It’s worth noting that the gateway maintains the TCP connection for just a brief period to optimize query speed and support transactions. It won’t keep the connection open for extended periods of inactivity. When a TCP connection closes, the gateway behaves differently for transactional and non-transactional requests:

  • For non-transactional requests, the gateway opens a new TCP connection.
  • For transactional requests, the gateway rolls back the entire transaction with an invalid connection response.

Using TiDB Cloud Serverless Driver for Edge Database Connectivity

TiDB Cloud Serverless Driver preserves the familiar experience of MySQL developers through simple interfaces, thus minimizing the learning curve. As an application developer, you can effortlessly integrate the serverless driver into your existing applications without straying from your accustomed practices.

Install

The driver is published as a npm package. Install it with the following command:

Shell
npm install @tidbcloud/serverless

Connect

Before executing SQL, establish a connection using the following code:

JavaScript
const conn = connect({url: 'mysql://[username]:[password]@[host]/[database]'})

Execute

Use the ‘execute’ function to run SQL commands or parameterized SQL queries as shown below:

  • Run SQL Command:
JavaScript
const results = await conn.execute('select * from test where id = 1')
  • Use parameterized SQL queries:
JavaScript
const id = 1
const results = await conn.execute('select * from test where id = ?', [id])
const results = await conn.execute('select * from test where id = :id', {id:id})

Transactions (Experimental)

You can also perform transactions. For example:

import { connect } from '@tidbcloud/serverless'

const conn = connect({url: 'mysql://[username]:[password]@[host]/[database]'})

async function transfer(sender_id: string, receiver_id: string, amount: number) {
    try {
      const tx = await conn.begin()
      // decrement amount from the sender.
      await tx.execute('update accounts set balance = balance - ? WHERE account_id = ?',[amount,sender_id])
      // check the balance of sender
      const sender = tx.execute('select balance from accounts where account_id = ?',[sender_id])
      if (sender[0].balance < 0) {
          throw new Error(`${from} doesn't have enough balance`)
      }
      // increment amount from the receiver.
      await tx.execute('update accounts set balance = balance + ? WHERE account_id = ?',[amount,receiver_id])
      await tx.commit()
    } catch (err) {
      await tx.rollback()
      throw err
    }
}

transfer('sender_id','receiver_id',100)

In the above example, an exception trigger results in the rollback of the entire transaction.

Additionally, you can control rollback actions based on intermediate results per your specific business logic requirements.

For more detailed usage of the serverless driver, refer to the documentation.

Serverless Driver in Action

To demonstrate best practices for connecting to TiDB Serverless from edge environments, we’ve created a live example called car-sale-insight. We deployed the backend using various edge computing solutions, including Vercel Edge Function, Cloudflare Workers, and Netlify Edge Function. All you need to do is deploy the frontend locally or on your preferred deployment platform. Then, simply select the desired backend solution from a dropdown menu. There’s no requirement for additional backend configuration or extra steps — it’s that straightforward!


Figure 2. Insight into automotive sales

The care-sale-insight repository contains examples of popular edge providers. Feel free to use them as a starting point to connect your TiDB Serverless from the edge.

Evaluating Serverless Driver’s Performance in Edge Environments

To demonstrate the performance of the serverless driver, we conducted a few tests based on the ‘edge-data-latency’ project by Vercel. We ran the performance tests using Vercel Function, with the following critical factors in consideration:

  • Protocol: HTTP or TCP
  • Environment: Serverless Function or Edge Function
  • Geographical Location: Regional or Global (indicating whether the edge function is deployed in the same region as the database)

Note : While proximity to users often reduces latency of the edge environment, the location of the database is a critical consideration. Since the database is primarily single-region, having the edge function close to the database is beneficial. Vercel allows configuring the Edge Function in the same region as the database, whereas the Serverless Function can only be regional.

  • Query: Number of queries and data size

Considering these key factors, we evaluated performance across four different scenario combinations:


We used the following metrics for the evaluation:

  • Processing Time: Duration taken by the function to execute the queries and return the result.
  • End-to-end: Time taken for the client to receive the result, influenced by the user’s internet connection and location.

For each combination mentioned above, we executed a single query with a SELECT statement ten times.

Global vs Regional

The following charts show the comparison tests between combination 1 and combination 2:


Figure 3. Global vs Regional

It’s evident from the tests that global edge functions exhibit longer durations in both processing time and end-to-end latency tests compared to their regional counterparts. Therefore, it’s safe to say that configuring the Edge function to the same region as your database can significantly enhance your application’s speed.

Edge Function vs Serverless Function

The following charts show the comparison between combination 2 and combination 3. Due to the cold start time, only end-to-end latency was measured.


Figure 4. Edge vs Serverless

Here are our observations for test group 2:

  • The first query (#1) for both combinations takes longer because it includes the cold start time.
  • The cold start takes 1104ms for the edge function and 1905ms for the serverless function. If your application does not query the database frequently and always encounters a cold start, you may consider using the edge function.
  • For the remaining queries, both combinations performed similarly with an average latency of 500ms.

Serverless Driver vs MySQL2 Driver

The following charts show the comparison between combination 3 and combination 4.
Only processing time is measured, as it is hard to tell the difference with end-to-end latencies.


Figure 5. Serverless vs MySQL2

On average, the serverless driver exhibits a processing time of 13ms, while the MySQL2 driver takes 21ms. Our serverless driver over HTTPs outperformed the traditional TCP connection method.

Edge Database Connectivity Performance Evaluation Summary

In summary, the serverless driver, when paired with TiDB Serverless in Vercel Functions, showcases notable performance. It outperforms the traditional MySQL2 driver operating over TCP, with significantly lower average processing times. Moreover, for applications encountering infrequent database queries and cold starts, the synergy of the serverless driver with edge functions presents a compelling advantage. By leveraging the serverless driver’s efficiency and lower latency, developers can boost the overall responsiveness of their applications. This makes it a viable option for those seeking optimized performance and reliability in a serverless environment.

Conclusion

In conclusion, TiDB Cloud Serverless Driver not only enables seamless database connectivity to TiDB Serverless from various edge computing platforms but also exhibits impressive performance metrics. For application developers, serverless drivers bridge the gap between serverless databases and edge environments, enabling optimized, low-latency database interactions in edge computing scenarios.

TiDB Cloud Serverless Driver is still in the beta stage with a fast iteration pace. Any feedback is welcomed. Try TiDB Serverless and TiDB Cloud Serverless Driver free now!

Ready to supercharge your data integration with TiDB? Join our Discord community now! Connect with fellow data enthusiasts, developers, and experts too: Stay Informed: Get the latest updates, tips, and tricks for optimizing your data integration. Ask Questions: Seek assistance and share your knowledge with our supportive community. Collaborate: Exchange experiences and insights with like-minded professionals. Access Resources: Unlock exclusive guides and tutorials to turbocharge your data projects. Join us today and take your data integration to the next level with TiDB!

serverless drivers bridge the gap between serverless databases and edge environments, enabling optimized, low-latency database interactions in edge computing scenarios.

:+1:

1 Like