Multi-Tenant API Schema Registry Design
Overview
This design outlines SchemaHub, a multi-tenant schema registry supporting GraphQL (SDL), gRPC (Protobuf), and REST (OpenAPI 3.x). It provides:
- Multi-tenancy: Tenant isolation via namespaces with RBAC.
- Compatibility semantics: Protocol-specific evolution rules with automated checks.
- Notification channels: Webhooks, Kafka, email, and in-app feeds.
- SLAs: Configurable per-tenant guarantees with monitoring and alerting.
- Billing integration: Usage metering tied to external billing systems (e.g., Stripe, Zuora).
Key Principles:
- Eventual consistency for high availability.
- GitOps-friendly: Schema versions are immutable, git-commit-like artifacts.
- Scalable: PostgreSQL + Kafka + Elasticsearch stack.
- Security: JWT auth, schema signing (e.g., cosign), tenant-scoped access.
Architecture (High-Level):
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Producers │ │ SchemaHub │ │ Consumers │
│ (Services/CI) │───▶│ - API Gateway │───▶│ (API Gateways) │
│ - GraphQL SDL │ │ - Schema Store │ │ - Schema Fetch │
│ - Protobuf .proto│ │ - Compat Checker │ │ - Compat Val │
│ - OpenAPI YAML │ │ - Notif Engine │ │ - Mock Servers │
└─────────────────┘ │ - SLA Monitor │ └─────────────────┘
│ - Billing Meter │
└──────────────────┘
│ │
┌──────┴──────┐ └──────────┐
│ Kafka │ │
│ (Events) │ │ Stripe/Zuora
└────────────┘ │ API
│ │
┌──────┴──────┐ ┌──────┴──────┐
│ Postgres │ │ Elasticsearch│
│ (Schemas) │ │ (Search/Logs)│
└────────────┘ └──────────────┘
Core Data Model
Stored in PostgreSQL (schemas) + S3 (artifacts).
# Core Entities (JSON Schema-like)
Tenant:
id: UUID
name: string # e.g., "acme-corp"
sla: ref(SLA) # optional
billingConfig: object # {provider: "stripe", planId: string}
Namespace:
id: UUID
tenantId: UUID
name: string # e.g., "acme-corp/users-service"
rbac: map<Role, Principal[]> # owner, editor, viewer
Schema:
id: UUID
namespaceId: UUID
name: string # e.g., "UserService"
protocol: enum(GraphQL, gRPC, REST)
artifactUrl: string # S3 presigned URL to latest version
latestVersion: ref(Version)
tags: string[]
Version:
id: UUID # git-like SHA256 hash of artifact
schemaId: UUID
version: string # semver e.g., "1.2.3"
artifact: bytes # raw SDL/proto/YAML (or S3 ref)
compatibility: enum(BACKWARD, FORWARD, FULL, NONE)
deprecated: bool
releaseNotes: string
createdAt: timestamp
labels: map<string, string>
Webhook:
id: UUID
namespaceId: UUID
url: string
events: string[] # ["schema.created", "schema.deprecated"]
secret: string # HMAC verification
SLA:
id: UUID
tenantId: UUID
uptime: float # 99.99%
responseTimeP99: int # ms
currentStatus: enum(OK, WARNING, BREACH)
BillingMetric:
tenantId: UUID
period: string # "2024-01"
schemaReads: int64
versionsPublished: int64
notificationsSent: int64
cost: float # computed
Schema Ingestion & Management API
Registry exposes a GraphQL API (for flexibility) + REST proxy. Auth: JWT with tenant claims.
# SchemaHub GraphQL Schema (excerpt)
type Query {
schemas(tenantId: ID!, namespace: String): [Schema!]!
schemaVersions(schemaId: ID!, limit: Int): [Version!]!
searchSchemas(query: String!, tenantId: ID!): [Schema!]! # Elasticsearch-backed
}
type Mutation {
publishSchema(
namespace: String!,
protocol: Protocol!,
artifact: Upload!, # SDL/proto/YAML
compatibility: CompatibilityLevel,
labels: [LabelInput!]
): Version!
deprecateVersion(schemaId: ID!, versionId: ID!): Version!
configureWebhook(namespaceId: ID!, url: String!, events: [String!]): Webhook!
setSLA(uptime: Float!, p99: Int!): SLA!
}
enum Protocol { GRAPHQL, GRPC, REST }
enum CompatibilityLevel { BACKWARD, FORWARD, FULL, NONE }
extend type Subscription {
schemaChanged(namespaceId: ID!): Version! # Real-time via Kafka + WS
}
Ingestion Flow:
- Producer uploads artifact via
publishSchema.
- Validate syntax (GraphQL:
graphql-js; Proto: protoc; OpenAPI: swagger-parser).
- Compute compatibility vs. previous version.
- Store immutable version in Postgres/S3.
- Emit Kafka event:
schema.published.
- Meter usage for billing.
Compatibility Semantics
Automated checks on publish. Configurable per namespace (strict/lenient mode).
| Protocol | Backward Compatible Changes | Breaking Changes (Blocked by Default) | Tooling |
|---|
| GraphQL | Add types/fields; Deprecate fields | Remove/change type of existing field; Non-null to nullable | graphql-breaking-changes + custom SDL diff |
| gRPC/Proto | Add fields (with optional); Bump max in repeated; Deprecate | Remove fields; Change field numbers/types; Narrow types | buf breaking + protoc descriptors |
| REST/OpenAPI | Add paths/params (optional); Deprecate | Remove paths; Change param req'd; Type changes | openapi-diff + spectral linting |
Algorithm (Pseudo-code):
def check_compatibility(prev_desc, new_desc, level):
if level == 'BACKWARD':
return all_callable(prev_calls) and all_type_safe(new_to_prev)
# Parse to AST/descriptor, diff structurally
diffs = diff_schemas(prev_desc, new_desc)
return filter_breaking(diffs, rules[protocol][level])
Notification Channels
Event-Driven via Kafka topics (schema.{tenant}.{namespace}.changes).
- Webhooks: POST payload
{event: "published", version: {...}, prevVersion: {...}}. Retry with exponential backoff.
- Kafka: Tenants subscribe to their topics.
- Email/In-App: Via SES + tenant prefs.
- Real-time: GraphQL Subscriptions.
Config: Per-namespace webhooks (up to 10).
SLAs
- Monitoring: Prometheus metrics (
schema_read_latency, publish_success_rate).
- Per-Tenant: Config via
setSLA. Breach if <99.9% uptime over 30d window.
- Enforcement: Alert on PagerDuty; Auto-throttle heavy tenants.
- Dashboard: Grafana with tenant views.
Metrics:
schemahub_schema_reads_total{tenant="acme", namespace="users"} 1e6
schemahub_publish_errors_total{tenant="acme"} 5
Billing Integration
Metered Usage (compute hourly, bill monthly):
- Free Tier: 10 schemas, 100 versions/mo, 1k reads.
- Paid: 0.01/1kreads+0.10/version + $5/mo base.
- Metrics → Kafka → Billing Service.
- Integration:
# On event
meter = BillingMeter(tenant_id)
meter.inc('reads', 1)
meter.inc('versions_published', 1)
# End-of-period
invoice = compute_cost(metrics)
stripe.Invoice.create(customer=tenant.stripe_id, amount=invoice.total)
Webhook to Billing Provider:
POST /stripe/webhook
{event: "usage_report", tenant: "acme", reads: 1e6, cost: 10.00}
Deployment & Ops
- Kubernetes: Horizontal pods, schema store sharded by tenant.
- Backup: Daily S3 snapshots.
- Audit Logs: All actions in Elasticsearch.
- Onboarding: Self-service tenant signup → Stripe connect.
This design is production-ready, extensible (add AsyncAPI?), and aligns with tools like Apicurio/Compai. Implementation estimate: 4-6 engineer-months. Questions?