Scylla
Create a ScyllaDB connection using the NetLifeGuru Scylla driver and the shared DB layer.
Use github.com/netlifeguru/db-scylla when your application connects to ScyllaDB or CQL-based workloads supported by the driver.
The Scylla driver implements the shared db.Conn interface, so once the connection is created, the rest of your application can use common db APIs such as List, Get, Value, Maps, Insert, Update, and Delete.
Scylla has database-specific behavior that is different from MySQL and PostgreSQL:
Databaserepresents the Scylla keyspace- consistency is configured through
db.Config.Consistency - placeholders use
? - SQL transactions are not supported in the same way as MySQL or PostgreSQL
- batch operations are supported through the Scylla driver
Install
Install the Scylla driver:
go get github.com/netlifeguru/db-scyllaInstalling the driver also installs:
github.com/netlifeguru/db
github.com/netlifeguru/mapperImport
import (
"github.com/netlifeguru/db"
"github.com/netlifeguru/db-scylla"
)Connection Example
This example creates a Scylla connection from environment variables.
package main
import (
"os"
"strconv"
"time"
"github.com/netlifeguru/db"
"github.com/netlifeguru/db-scylla"
)
func connectDB() (db.Conn, error) {
conn := scylla.New()
cfg := db.Config{
Identifier: "default",
Host: os.Getenv("DB_HOST"),
Database: os.Getenv("DB_NAME"), // Scylla keyspace
Username: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
MaxConns: 50,
MinConns: 5,
MaxConnIdleTime: 10 * time.Minute,
MaxConnLifetime: 2 * time.Hour,
HealthCheckPeriod: 30 * time.Second,
ConnectTimeout: 10 * time.Second,
Consistency: "local_quorum",
}
if port := os.Getenv("DB_PORT"); port != "" {
n, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
cfg.Port = n
}
if err := conn.CreatePool(cfg); err != nil {
return nil, err
}
return conn.Fork(), nil
}Environment Variables
The example above expects these environment variables:
DB_HOST=127.0.0.1
DB_PORT=9042
DB_NAME=app
DB_USER=scylla
DB_PASSWORD=secretDB_NAME is used as the Scylla keyspace.
DB_PORT is optional in the example. If it is not provided, the driver uses its default Scylla port.
Scylla Defaults
If values are not provided, the Scylla driver applies sensible defaults internally.
| Option | Default |
|---|---|
Identifier | default |
Host | 127.0.0.1 |
Port | 9042 |
MaxConns | 25 |
MinConns | 2 |
MaxConnIdleTime | 5m |
MaxConnLifetime | 1h |
ConnectTimeout | 5s |
HealthCheckPeriod | 30s |
Consistency | quorum |
Placeholder Style
Scylla uses ? placeholders.
users, err := db.List[User](ctx, conn, `
SELECT * FROM users_by_email
WHERE email = ?
`, email)The shared db helpers pass the arguments to the driver in order.
Insert Behavior
Scylla data models are often query-driven.
Instead of inserting one row into one normalized table, an application may write the same entity into multiple query tables.
Example:
const insertUserByIDQuery = `
INSERT INTO users_by_id (id, email, name, active, created_at)
VALUES (?, ?, ?, ?, ?)
`
const insertUserByEmailQuery = `
INSERT INTO users_by_email (email, id, name, active, created_at)
VALUES (?, ?, ?, ?, ?)
`A common pattern is to generate the ID in application code and write both query tables.
id := gocql.TimeUUID()
createdAt := time.Now().UTC()
if _, err := db.Insert(ctx, conn, insertUserByIDQuery, id, email, name, active, createdAt); err != nil {
return "", err
}
if _, err := db.Insert(ctx, conn, insertUserByEmailQuery, email, id, name, active, createdAt); err != nil {
return "", err
}
return id.String(), nilTransactions
Scylla does not use SQL transactions in the same way as MySQL or PostgreSQL.
The shared transaction guide applies to drivers that support SQL-style transactions, such as MySQL and PostgreSQL.
For Scylla workloads, use data modeling, idempotent writes, batches, or lightweight transactions where appropriate.
Batches
The Scylla driver supports batch operations.
Batch usage is documented in the Scylla Batches guide.
Use batches carefully and only when the data model requires grouped writes.
SQL Files
When using SQL model files, Scylla uses:
model.cqlThe shared db.LoadModel helper can load CQL sections from this file and store them in db.DialectSQL values.
SQL files are documented in the SQL Files guide.
Next Step
After creating a Scylla connection, continue with the shared querying guides:
- List, Get, Value, and Maps
- Query Objects
- Dialect SQL
- Mutations
- Scylla Batches