Quantcast
Channel: Programming – Gea-Suan Lin's BLOG
Viewing all articles
Browse latest Browse all 130

Cloudflare Workers 提供的 MySQL/MariaDB 方案

$
0
0

在「Build global MySQL apps using Cloudflare Workers and Hyperdrive」這邊看到 Cloudflare 提供了 Cloudflare Workers 使用 MySQL/MariaDB 的方案。

本來以為是做出了 replicated globally 的系統,還在想要怎麼處理 ACID 問題,花了點時間看完才發現是弄出了一個類似 ProxySQL 的架構,最終還是要連回某個區域的 MySQL 主機上,可能是 AWSGCPAzure 或是自己的 MySQL 伺服器:

Now, you can create new Hyperdrive configurations for MySQL databases hosted anywhere (we’ve tested MySQL and MariaDB databases from AWS (including AWS Aurora), GCP, Azure, PlanetScale, and self-hosted databases).

文章裡面主要是解決了兩個問題,第一個是 Cloudflare Workers 的 javascript 環境其實並不像一般的 Node.js,支援所有功能,補上了 mysql2 需要用到的 timer 後,剩下的問題就是 eval() 了:

mysql2 uses eval to optimize the parsing of MySQL results containing large rows with more than 100 columns (see benchmarks). This blocked the driver from working on Workers, since the Workers runtime does not support this module.

不過也存在不使用 eval() 的替代方案,在新版的 mysql2 可以透過參數切換:

Luckily, prior effort existed to get mysql2 working on Workers using static parsers for handling text and binary MySQL data types without using eval(), which provides similar performance for a majority of scenarios.

In mysql2 version 3.13.0, a new option to disable the use of eval() was released to make it possible to use the driver in Cloudflare Workers[.]

到這邊算是搞定 library 這邊的事情了。

第二個是 Cloudflare 提供了 Hyperdrive,類似於 ProxySQL 的功能,讓 worker 不用對遠端的跑 TLS handshake,直接在本地機房解決:

Hyperdrive solves a core problem: connecting from Workers to regional SQL databases is slow. Database drivers require many roundtrips to establish a connection to a database.

Hyperdrive solves this problem by pooling connections to your database globally and eliminating unnecessary roundtrips for connection setup. As a plus, Hyperdrive also provides integrated caching to offload popular queries from your database.

不過不是很愛這種架構...


Viewing all articles
Browse latest Browse all 130

Trending Articles