New in ADR-0006: QueryWithFallback + Boilerplate Reduction Helpers (85% less code!)
Production-Ready Examples

Copy, Paste, Run

Working examples for user management, e-commerce, multi-tenant SaaS, event logging, and more. All under 300 lines of code.

7
Complete Examples
~1min
To Run Locally
100%
Production-Ready

Featured Examples

Start with these to understand core concepts

Featured
Beginner

User Management

~200 lines • Simple API

Complete authentication system with email lookups, role-based queries, and automatic Redis indexing. Perfect starting point for learning SmarterBase.

// Create user with auto-indexing
user, _ := userManager.CreateUser(ctx,
    "alice@example.com",
    "Alice Smith",
    "admin",
)

// Indexes updated automatically:
// ✅ email → user_id (O(1) lookup)
// ✅ role → [user_ids] (multi-value)
// O(1) email lookup via Redis
user, _ := userManager.GetUserByEmail(ctx,
    "alice@example.com",
)

// O(1) role-based query
admins, _ := userManager.ListUsersByRole(ctx,
    "admin",
)
// Returns all admin users instantly
// Type-safe batch loading (v1.6)
users, _ := smarterbase.BatchGet[User](
    ctx, store, userKeys,
)

// 🚀 10-100x faster than loops
// 🎯 Replaces 15 lines of boilerplate
// ✅ Compile-time type safety
Authentication
Redis Indexes
QueryWithFallback
CRUD Operations
~5 minutes to understand
What You'll Learn:
  • Creating entities with automatic ID generation
  • Setting up Redis indexes for fast lookups
  • Using QueryWithFallback for Redis → scan resilience
  • Coordinating index updates with IndexManager
Intermediate

E-Commerce Orders

~250 lines • Core API

Order management with atomic status transitions, distributed locks, and revenue calculations. Learn how to handle critical operations safely.

// Atomic status update with distributed lock
err := smarterbase.WithAtomicUpdate(
    ctx, store, lock,
    "orders/"+orderID, 10*time.Second,
    func(ctx context.Context) error {
        var order Order
        store.GetJSON(ctx, key, &order)

        // Validate transition
        if !isValid(order.Status, "shipped") {
            return errors.New("invalid transition")
        }

        order.Status = "shipped"
        return store.PutJSON(ctx, key, &order)
    },
)

// ✅ No race conditions
// ✅ Automatic retry on conflict
// ✅ TTL prevents deadlocks
Distributed Locks
QueryWithFallback
Atomic Updates
State Machines
~10 minutes to understand
What You'll Learn:
  • Using distributed locks for critical operations
  • Implementing state machine transitions safely
  • Querying by multiple indexes (user, status)
  • Calculating aggregates (revenue, totals)

All Examples

Choose based on your use case or learning goals

Production Patterns

NEW - ADR-0006

Redis fallback patterns with QueryWithFallback. Automatic profiling, graceful degradation, and resilience best practices.

QueryWithFallback
Redis Fallback
Auto Profiling
85% Less Code
~300 lines • Core API
Use cases: High-scale production, resilience
View Example

Multi-Tenant Config

Advanced

SaaS configuration management with per-tenant isolation, plan upgrades, and feature flags.

SaaS Patterns
Tenant Isolation
Plan Management
~300 lines • Core API
Use cases: SaaS platforms, feature flags
View Example

Event Logging

Beginner

Append-only event logs with JSONL format. Perfect for audit trails and activity tracking.

JSONL Format
Streaming
Append-Only
~150 lines • Core API
Use cases: Audit trails, activity logs
View Example

Schema Migrations

Intermediate

Evolve schemas without downtime using versioning and lazy migrations on read.

Versioning
Lazy Migration
Schema Evolution
~180 lines • Core API
Use cases: Schema changes, field refactoring
View Example

Metrics Dashboard

Beginner

Prometheus metrics integration with operation counters, latency histograms, and error tracking.

Prometheus
Observability
Metrics
~120 lines • Core API
Use cases: Production monitoring, alerting
View Example

Simple API Series

Beginner

4 progressive examples: Quickstart, CRUD, Indexing, and Versioning. Perfect for rapid development.

Simple API
Auto-Indexing
Struct Tags
50-100 lines each • Simple API
Use cases: Prototypes, MVPs, demos
View Examples (4)

Compare Examples

Choose the right example for your needs

Example Lines API Complexity Key Features Best For
User Management
View →
~200
Simple
Beginner
Indexes
BatchGet
CRUD
Auth systems, User profiles
E-Commerce Orders
View →
~250
Core
Intermediate
Locks
Atomic
Indexes
Orders, Invoices, Fulfillment
Multi-Tenant Config
View →
~300
Core
Advanced
SaaS
Isolation
Plans
SaaS Config, Feature Flags
Event Logging
View →
~150
Core
Beginner
JSONL
Streaming
Append
Audit Trails, Activity Logs
Schema Migrations
View →
~180
Core
Intermediate
Versioning
Migration
Schema Evolution
Metrics Dashboard
View →
~120
Core
Beginner
Prometheus
Metrics
Production Monitoring

Recommended Learning Path

Progress from basics to advanced patterns

Step 1

User Management

Start here to learn CRUD operations, indexes, and BatchGet[T]

⏱️ 30 minutes
Start Learning
Step 2

Event Logging

Learn JSONL format, streaming, and append-only patterns

⏱️ 20 minutes
Continue
Step 3

E-Commerce Orders

Master distributed locks, atomic updates, and transactions

⏱️ 45 minutes
Continue
Step 4

Multi-Tenant Config

Advanced SaaS patterns with tenant isolation and plan management

⏱️ 60 minutes
Continue

Ready to Build?

After completing these examples, you'll understand 90% of SmarterBase patterns. Check out the full documentation for production deployment.

View Full Documentation

Run Examples Locally

Get started in under 1 minute

1

Install SmarterBase

go get github.com/adrianmcphee/smarterbase
2

Start Redis (Optional for some examples)

docker run -d -p 6379:6379 redis:7-alpine
3

Clone and Run Example

git clone https://github.com/adrianmcphee/smarterbase.git
cd smarterbase/examples/user-management
go run main.go
All examples run standalone
No database setup required. Examples use filesystem backend by default.