How ultimatesoftware/surge works
Project Type and Purpose
• Surge is a multi-module library for building stateful, message-driven JVM and polyglot applications on top of Apache Kafka.
• Its goal is to provide CQRS/event-sourcing, aggregate processing, and persistence purely within Kafka (no external DB), with support for Scala/Java DSLs, metrics, health/tracing, multilanguage sidecars, and pluggable serialization.
File Structure
• Top-level modules/ directory holds primary libraries:
– command-engine (core aggregate processing & persistence)
– common (actor abstractions, Kafka utilities, health, tracing)
– serialization (message & aggregate formatting)
– metrics (counters, gauges, histograms)
– multilanguage & *-sdk modules (polyglot gateways & samples)
– surge-docs (documentation pipelines)
• Each module follows a standard Maven/Gradle layout with src/main and src/test.
• Build and CI files (build.sbt, .github/workflows) orchestrate multi-module build, release, and documentation.
README for Additional Insights
• Surge abstracts away an external state store by leveraging Kafka topics for:
– Command topics (incoming commands)
– Event topics (published events)
– State topics (KTable/state-changelog streams)
• It plugs into any web framework as a library, exposing DSLs (Scala or Java) or a gRPC sidecar for other languages.
Components & Interactions
a. Main Components
1. Developer Application (your business service)
2. Surge SDK layer
• ScalaDSL / JavaDSL modules – public API for defining commands, events, business logic
• Multilanguage Sidecar / gRPC Gateway – for C#, Python, etc.
3. Common Library
• Akka Actor System binding & cluster sharding
• Kafka producer/consumer abstractions
• Health & tracing (JMX, OpenTelemetry)
4. Command-Engine Core
• SurgePartitionRouter – routes commands to entity partitions
• SurgeProcessingTrait – defines aggregate lifecycle
• PersistentActorRegion – wraps Kafka Streams / Akka persistence over Kafka topics
5. Serialization Plugin
• Formats commands/events/aggregates to/from bytes for Kafka topics
6. Metrics Module
• Captures counters, gauges, timers around processing, publishes to dropwizard, Micrometer, etc.
7. External Systems
• Kafka Cluster (Command Topic, Event Topic, State Topic/KTable)
• Optional: Zookeeper (implicit), Monitoring dashboards, Tracing backends
b. Relationships & Data Flows
1. Developer App → calls Surge DSL to sendCommand(…).
2. SDK layer serializes the command (via Serialization module) → publishes to Kafka Command Topic (via Common/KafkaProducerActor).
3. Command-Engine consumes Command Topic, routes to the correct aggregate partition (SurgePartitionRouter) in an Akka Actor System.
4. Aggregate logic executes (SurgeProcessingTrait), emits zero or more events.
5. Events are serialized → published to Kafka Event Topic.
6. Kafka Streams KTable subscribes to Event Topic to update State Topic (aggregate snapshots).
7. Command-Engine can read current state directly from Kafka State Topic for validation/business logic.
8. Health/tracing metrics emitted continuously (Common health bus, Metrics module).
9. Multilanguage sidecar: runs a gRPC server, receives external language commands, passes them into the same Command Topic, and returns results via gRPC.
c. Architectural Patterns & Principles
• CQRS / Event-Sourcing: Commands → Events → State, purely over Kafka.
• Actor Model (Akka): concurrency and partition routing via cluster sharding.
• Hexagonal/plugin-based: clear separation between public DSLs, core engine, serialization, metrics.
• Reactive Streams / Kafka Streams: stateful stream processing for aggregate state.
d. Key Technologies / Frameworks
• Scala, Java, sbt multi-project build
• Akka Actor System & Cluster Sharding
• Apache Kafka (Producer, Consumer, Streams)
• Protobuf / custom serializers
• Micrometer/Dropwizard metrics (in Metrics module)
• OpenTelemetry / JMX (in Common health/tracing)
• gRPC (.proto definitions in multilanguage modules)