Core Architecture
Kratos uses a component-based architecture where each part of the system has a clear responsibility:Application Layer
The App struct manages the lifecycle of all components, coordinating startup, shutdown, and signal handling.
Transport Layer
Pluggable transport servers (HTTP, gRPC) that handle network communication independently.
Registry Layer
Service registration and discovery for microservice coordination.
Middleware Layer
Cross-cutting concerns like logging, tracing, and authentication.
Key Components
Transport Abstraction
Kratos defines a cleantransport.Server interface that all transport implementations must satisfy:
transport/transport.go
Service Registry
The registry package provides interfaces for service registration and discovery:registry/registry.go
ServiceInstance contains all metadata needed for service discovery:
registry/registry.go
Design Principles
Separation of Concerns
Each layer in Kratos has a specific responsibility:- Transport layer handles network protocols
- Business layer contains domain logic
- Data layer manages persistence
- Service layer orchestrates use cases
Interface-Driven Design
Kratos heavily uses Go interfaces to define contracts:- Easy mocking for tests
- Swappable implementations
- Clear API boundaries
Context Propagation
Kratos uses Go’scontext.Context throughout the framework for:
- Request-scoped values (app info, transport metadata)
- Cancellation signals
- Timeout management
app.go:91
NewContext function creates a context carrying application information:
app.go:204-206
Lifecycle Management
The framework provides hooks at various lifecycle stages:Extensibility
Kratos is designed to be extended:- Custom transports: Implement the
transport.Serverinterface - Middleware: Add cross-cutting concerns via middleware chains
- Registry adapters: Implement
RegistrarandDiscoveryfor different service registries - Custom options: Use the functional options pattern for configuration
The framework comes with built-in support for HTTP and gRPC transports, but you can add support for any protocol by implementing the
transport.Server interface.Next Steps
Application Lifecycle
Learn how the App struct manages component lifecycles
Dependency Injection
Understand how to wire components together