Skip to Content
Views

MySQL

Configure a MySQL circuit host, run the Views migration, and check key and identifier limits.

Build the host

Use a MySQL client from @effect/sql-mysql2 with SqlStorage.layer. Install @streamsy/storage at the same version as @streamsy/views for the transaction isolation that Views uses on MySQL.

The storage schema requires MySQL 8.0.17 or later for the utf8mb4_0900_bin collation. The dialect suites covered MySQL 8.4.11 only. MySQL 8.0.17 through 8.4.10 remain untested. See MySQL Unicode collations.

If you are opening an existing database, check the SQL storage migration instructions before you start the host.

Build CheckpointStore.layer, Transaction.layer, and ViewBackend.layerSql over storage. Pass the storage layer's SqlClient to View.migrate(sql) at startup, before running or attaching a circuit.

import { Effect, Layer, Redacted } from "effect";
import { SqlClient } from "effect/sql";
import { MysqlClient } from "@effect/sql-mysql2";
import { Streamsy } from "@streamsy/core";
import { CheckpointStore, Transaction, Projection } from "@streamsy/projection";
import { SqlStorage } from "@streamsy/storage";
import { View, ViewBackend } from "@streamsy/views";
import { Orders, ShopViews, shopCircuit } from "./streams.ts";

export const runShop = (databaseUrl: string, shopId: string) => {
  const client = SqlStorage.clientLayer(MysqlClient.make({ url: Redacted.make(databaseUrl) }));
  const storage = SqlStorage.layer(Streamsy.group(Orders, ShopViews)).pipe(
    Layer.provideMerge(client),
  );
  const host = Layer.mergeAll(CheckpointStore.layer, Transaction.layer, ViewBackend.layerSql).pipe(
    Layer.provideMerge(storage),
  );

  return Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient;
    yield* View.migrate(sql);
    yield* Projection.run(shopCircuit(1).at({ shopId }));
  }).pipe(Effect.provide(host));
};

Create the input for shopId before running the circuit. See the shop circuit declarations. StateTable requires a SQLite host; see State table operations.

Several hosts can use the same database. Assign one writer to each binding and stop and drain its runners before lifecycle operations. See One writer and Lifecycle and rebuilds.

Keep the storage boundary's default READ COMMITTED isolation when several hosts use the database. At this level, a circuit can read operator state consistent with its checkpoint when another host advances an existing binding during the transaction. See MySQL transactions.

At REPEATABLE READ, MySQL can also take gap locks when a unit deletes operator entries that do not exist. Units of two unrelated bindings can then deadlock. The unit that loses fails with a retryable storage-failure, and its runner runs it again.

Migration and stored state

View.migrate reserves one connection and takes the named lock streamsy_view_layout. Concurrent hosts wait up to 30 seconds for that lock. Call it outside a transaction. Inside one it needs a second connection, and it waits forever when the pool has none. MySQL commits DDL one statement at a time. If the layout table exists, the migration checks its version before any DDL. A newer layout fails with a non-retryable ViewFault.

The layout table holds the Views layout version, currently 1. The migration creates the five operator tables and inserts the version if it is absent. You can rerun the migration after an interrupted startup. Building SqlStorage.layer runs the separate Streamsy storage migration.

The operator tables use SHA-256 digests for equality lookups and keep full row, output, and owner keys as bytes. An ordered lookup that fills its limit can read every entry with the last row's 1,024-byte prefix before applying the limit. MySQL uses the same ordered lookup algorithm as Postgres.

Keys and identifiers

Views limits stored relation, arrangement, and node identifiers to 255 UTF-8 bytes. For a snapshot arrangement, count @snapshot/ and the complete JSON array of the State class's service key and collection name, including JSON punctuation and escapes.

An overlong identifier or an encoded row, output, or owner key with a lone UTF-16 surrogate fails the unit with a non-retryable ViewDataFault. U+0000 is valid in a key.

Views adds no length limit to row keys or sort keys. Keep MySQL's sql_mode strict so MySQL rejects values that exceed a column's length. Without strict mode, MySQL can truncate an overlong binary value. See MySQL binary column types.

MySQL closes the connection when it receives a packet larger than max_allowed_packet. Size the client and server packet limits for the rows and batches your circuit writes. See MySQL packet limits.

On this page