Snapshots
Snapshots let the SDK persist feature definitions (and JWKS) so:
- startup can be faster
- your service can continue evaluating during transient outages
In-memory Snapshot (tests / demos)
sp := snapshot.NewMemoryProvider()
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
File Snapshot
sp := snapshot.NewFileProvider("/var/lib/toggly")
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
The provider stores JSON snapshot files inside the directory you provide.
Redis
Store snapshots in Redis for distributed caching across multiple instances.
import (
"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly/snapshot"
"github.com/redis/go-redis/v9"
"time"
)
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
sp := snapshot.NewRedisProvider(snapshot.RedisOptions{
Client: redisClient,
Prefix: "toggly", // Default: "toggly"
TTL: 24 * time.Hour, // Zero means no expiration
})
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
Redis keys:
{prefix}:definitions- Feature definitions{prefix}:jwks- JWK set for signed definitions
MongoDB
Store snapshots in MongoDB for high availability.
import (
"context"
"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly/snapshot"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
mongoClient, _ := mongo.Connect(context.Background(),
options.Client().ApplyURI("mongodb://localhost:27017"))
collection := mongoClient.Database("toggly").Collection("snapshots")
sp := snapshot.NewMongoDBProvider(snapshot.MongoDBOptions{
Collection: collection,
DefinitionsID: "toggly_definitions", // Default
JWKSID: "toggly_jwks", // Default
})
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
PostgreSQL
Store snapshots in PostgreSQL.
import (
"database/sql"
"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly/snapshot"
_ "github.com/lib/pq"
)
db, _ := sql.Open("postgres", "postgres://user:pass@localhost/dbname?sslmode=disable")
sp := snapshot.NewPostgresProvider(snapshot.PostgresOptions{
DB: db,
TableName: "toggly_snapshots", // Default
DefinitionsID: "toggly_definitions",
JWKSID: "toggly_jwks",
AutoCreateTable: true, // Default
})
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
SQLite
Store snapshots in SQLite for local/embedded storage.
import (
"database/sql"
"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly/snapshot"
_ "github.com/mattn/go-sqlite3"
)
db, _ := sql.Open("sqlite3", "./toggly.db")
sp := snapshot.NewSQLiteProvider(snapshot.SQLiteOptions{
DB: db,
TableName: "toggly_snapshots", // Default
DefinitionsID: "toggly_definitions",
JWKSID: "toggly_jwks",
AutoCreateTable: true, // Default
})
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
SnapshotProvider: sp,
})
Dependencies
The providers require additional Go modules:
# Redis
go get github.com/redis/go-redis/v9
# MongoDB
go get go.mongodb.org/mongo-driver/mongo
# PostgreSQL
go get github.com/lib/pq
# SQLite
go get github.com/mattn/go-sqlite3
Clearing snapshots
if err := sp.Clear(ctx); err != nil {
// handle error
}
Clear removes persisted feature definitions and JWKS. Snapshots store exact signed
defs JSON (RawDefs) for cold-start verification. See
Server-side reliability and
Signed definitions.
Custom Provider
Implement your own provider for any storage backend:
type Provider interface {
LoadDefinitions(ctx context.Context) (*DefinitionsSnapshot, error)
SaveDefinitions(ctx context.Context, snap DefinitionsSnapshot) error
LoadJWKS(ctx context.Context) (*JWKSnap, error)
SaveJWKS(ctx context.Context, snap JWKSnap) error
Clear(ctx context.Context) error
}