How unkeyed/unkey works
Overall Architecture and Major Components
• Client/Frontend Layer – This includes the website, dashboards, documentation, and engineering portals. For example:
– apps/www: The public marketing website with components for landing pages, blogs, policies, and pricing.
– apps/dashboard: The web-based dashboard and admin interface where users manage keys, view analytics, configure settings, and monitor status.
– apps/docs: The documentation site with API references, guides, tutorials, and quickstart information.
– apps/engineering: An internal portal that provides in-depth architecture, design guidelines, internal documentation, and RFCs.
– apps/play: A playground or demo service used for experimenting with Unkey features.
• API and Core Services – These handle authentication, key management, rate limiting, and authorization:
– apps/api: The primary API service written mostly in TypeScript. It defines routes for creating, verifying, updating, and removing keys and handles API requests (including ratelimit and usage tracking).
– go/apps/api (and related directories in go/): The Golang implementation that provides many of the low‐level API endpoints, healthchecks, and OpenAPI documentation. It also integrates features like liveness checks, rate limiting endpoints, and service registration.
– apps/agent: A backend “agent” service written in Go and other languages that manages internal functions such as event routing, rate limiter replication, and connectivity (via protocols like gossip). It also holds business logic such as ratelimiting, encryption and key rotation.
– packages/ratelimit and packages/rbac: Shared libraries that encapsulate the business logic for managing rate limits and RBAC (roles and permissions) respectively. They are used across multiple services.
• Billing, Workflows and Data Services:
– apps/billing: Handles billing, subscription management, quota tracking and related data operations. It integrates with external payment processors and implements usage‐based billing.
– apps/workflows: Implements scheduled jobs and background maintenance tasks, such as refilling keys and invoicing. This service orchestrates tasks that are vital for data consistency and billing operations.
– apps/chproxy: Acts as a proxy for ClickHouse, which is the analytical database used for storing metrics, logs, and usage data.
– apps/logdrain: This service aggregates and drains logs from the system, helping in analytics and monitoring the key verification and API usage events.
• Internal and Shared Libraries:
– The “go” directory and its subdirectories (go/internal, go/pkg, etc.) contain multiple services and utilities handling lower-level tasks like caching, database interaction (SQL files in go/pkg/db), distributed membership (via “cluster” logic), and connection management.
– The “packages” directory (with subpackages such as api, cache, error, hono, nextjs, rbac) represents reusable modules that encapsulate core functionality. They are meant to be shared across services – for example, standardized error handling, caching, and tracing.
– The “internal” directory holds migration tools, billing migrators, authentication migrators, and wrappers for third-party services (like ClickHouse setup, AWS interactions, encryption utilities, etc.), ensuring smooth operations and system-wide consistency.
– The “tools” directory contains load testing (Artillery, k6), migration scripts, and local development utilities to simulate and manage the system.
Brief Service Descriptions (What Each Service Does)
• apps/agent: Operates as an internal agent managing event routing, ratelimiting, and key encryption duties. It uses protocols like gossip for state synchronization between nodes.
• apps/api: The main API gateway that processes incoming key-related actions (create, verify, update), handles authorization, rate limiting, and integrates with the agent for distributed operations.
• apps/billing: Manages subscription, quota, and billing-related calculations to ensure that usage is tracked and paid for accurately.
• apps/chproxy: Provides a proxy layer to the ClickHouse database making it easier to route analytical queries efficiently.
• apps/dashboard: Serves a web portal for users and administrators to manage API keys, view analytics, trace error logs, and configure system settings.
• apps/docs: Offers public-facing API documentation and developer guides outlining how to integrate and use Unkey’s authentication and authorization services.
• apps/logdrain: Consolidates logs from different parts of the system (API requests, agent events) to support monitoring, troubleshooting, and onward analytics.
• apps/play: A prototype or sandbox environment where developers can experiment with Unkey’s features without affecting production.
• apps/workflows: Schedules and orchestrates periodic background jobs related to maintenance tasks (for example, key refills, invoicing, and migrations).
• The “go” and “internal” directories: Implement many of the low-level services including API route handling, membership clustering, database interactions, and core business logic such as encryption, rate limiting, and logging.
• Packages (such as ratelimit, rbac, cache, error, hono) : Provide shared libraries and modules to standardize and centralize common functionality across the system.
Next Steps
• Frontend applications (apps/www, apps/dashboard, apps/docs) send requests to the API services (apps/api and the equivalent Go services).
• The API service communicates with the agent (apps/agent) to enforce rate limits and verifies keys.
• Both API and agent services interact with the ClickHouse proxy (apps/chproxy) for analytics and with shared libraries to ensure consistency in key validation and error handling.
• Billing and workflows services exchange data with the API layer to manage billing and task scheduling.