Prismatic Kernel

Prismatic Kernel

200 files Foundation

Overview

Prismatic Kernel is the platform's foundational process management layer. It orchestrates application startup across the entire umbrella, manages supervision trees, coordinates inter-application communication, and provides system-level services including health aggregation, resource monitoring, and graceful shutdown coordination. The Kernel ensures all 90+ applications start in the correct dependency order and maintain operational health throughout their lifecycle.

As the lowest layer of the platform stack, the Kernel embodies OTP design principles at every level. Each stateful concern runs in its own supervised process, inter-application messaging flows through PubSub channels rather than direct function calls, and failure isolation is guaranteed through supervision boundaries. The Kernel does not contain business logic -- it provides the infrastructure on which all business logic runs.

The application registry tracks the health status of every running application, enabling platform-wide health queries that aggregate individual application states into a unified status. When an application enters a degraded state, the Kernel can trigger remediation actions including restart sequences, resource reallocation, or alert escalation to operators. The Kernel's position as the first application to start and the last to stop gives it unique authority over the platform's operational lifecycle, making it the single point of coordination for deployment, scaling, and recovery operations.

Architecture

Prismatic Kernel
+-- SupervisorTree     # Top-level application supervision
|   +-- AppSupervisor  # Per-application supervisor management
|   +-- ServiceSupervisor  # Shared service supervision
+-- AppRegistry        # Application discovery, registration, health
+-- MessageRouter      # Inter-app PubSub and request-reply
+-- ResourceManager    # Memory, CPU, process count monitoring
+-- LifecycleManager   # Startup sequencing, shutdown drain, hot-reload

The Kernel starts first during platform boot and orchestrates the startup of all dependent applications through a topologically-sorted dependency graph. Each application's health is verified after startup before dependent applications are allowed to proceed, preventing cascading failures from partially-initialized dependencies.

Key Modules

ModuleResponsibility
PrismaticKernelPublic facade: health/0, cast/2, call/3, register/2, shutdown/2
PrismaticKernel.ApplicationOTP application entry point, first process to start
PrismaticKernel.SupervisorTreeTop-level supervisor managing all application supervisors
PrismaticKernel.AppRegistryETS-backed application registry with health tracking
PrismaticKernel.MessageRouterPubSub-based inter-application message routing
PrismaticKernel.ResourceManagerSystem resource monitoring (memory, CPU, processes)
PrismaticKernel.LifecycleManagerStartup sequencing and graceful shutdown coordination
PrismaticKernel.DependencyGraphTopological sort of application dependencies
PrismaticKernel.HealthAggregatorPlatform-wide health status computation

Key Features

Supervision Management

  • Application dependency graph resolution with cycle detection and topological sorting
  • Ordered startup with post-boot health verification gates ensuring healthy dependencies
  • Graceful shutdown with configurable drain timeouts per application for zero-downtime deployments
  • Automatic restart with exponential backoff and failure budgets preventing restart storms

Startup Sequencing

The startup sequence follows a strict topological order derived from the application dependency graph. Each application must pass its health check before dependents are started:

defmodule PrismaticKernel.LifecycleManager do
  @spec boot_sequence() :: {:ok, list(atom())} | {:error, term()}
  def boot_sequence do
    with {:ok, graph} <- PrismaticKernel.DependencyGraph.build(),
         {:ok, order} <- topological_sort(graph),
         :ok <- start_in_order(order) do
      {:ok, order}
    end
  end

  defp start_in_order(apps) do
    Enum.reduce_while(apps, :ok, fn app, :ok ->
      case start_and_verify(app) do
        :ok -> {:cont, :ok}
        {:error, reason} -> {:halt, {:error, {app, reason}}}
      end
    end)
  end

  defp start_and_verify(app) do
    with :ok <- Application.ensure_started(app),
         :ok <- wait_for_health(app, timeout: 30_000) do
      :ok
    end
  end
end

Dependency Graph Construction

The dependency graph is constructed from each application's mix.exs dependencies and optional runtime dependency declarations. Cycle detection prevents circular dependency deadlocks:

defmodule PrismaticKernel.DependencyGraph do
  @spec build() :: {:ok, Graph.t()} | {:error, :cycle_detected}
  def build do
    apps = Application.loaded_applications()
    graph = Enum.reduce(apps, Graph.new(), fn {app, _, _}, g ->
      deps = Application.spec(app, :applications) || []
      Enum.reduce(deps, Graph.add_vertex(g, app), fn dep, g2 ->
        Graph.add_edge(g2, dep, app)
      end)
    end)

    case Graph.topsort(graph) do
      false -> {:error, :cycle_detected}
      order when is_list(order) -> {:ok, %{graph: graph, order: order}}
    end
  end
end

Inter-App Communication

  • PubSub message routing between applications with topic filtering and pattern matching
  • Request-reply patterns with configurable timeouts and fallbacks for synchronous operations
  • Event broadcasting for system-wide notifications and alerts
  • Circuit breaker protection for inter-app calls to failing services

System Services

  • Platform-wide health aggregation across all running applications with degradation detection
  • Resource utilization monitoring (memory, CPU, process count, message queues) with alerting
  • Process registry and service discovery for dynamic service location
  • Configuration hot-reloading without application restart

Resource Monitoring

The ResourceManager tracks system-level metrics and triggers alerts when thresholds are exceeded:

ResourceWarning ThresholdCritical ThresholdAction
Memory (per app)500 MB1 GBAlert, investigate
Process count50,000100,000Alert, scale down
Message queue depth10,00050,000Alert, backpressure
CPU utilization80% sustained95% sustainedAlert, shed load
ETS table size100 MB500 MBAlert, compaction

Graceful Shutdown

The shutdown coordinator ensures zero-downtime deployments by draining active connections before stopping application processes. Each application registers a drain handler that receives advance notice of pending shutdown:

PhaseDurationAction
Signal received0sBegin shutdown sequence
Drain period0-30sStop accepting new requests, complete in-flight
Grace period30-60sForce-terminate remaining processes
Cleanup60-65sRelease resources, close connections
Exit65sProcess terminates

Usage

# Check platform-wide health status
{:ok, health} = PrismaticKernel.health()
# => %{status: :healthy, apps: 90, processes: 12_450, uptime: "14d 3h"}

# Send inter-app message via PubSub
PrismaticKernel.cast(:prismatic_perimeter, {:scan_complete, results})

# Request-reply with timeout
{:ok, response} = PrismaticKernel.call(:prismatic_api, {:get_endpoints}, timeout: 5_000)

# Register a named service for discovery
PrismaticKernel.register(:security_rating_engine, self())

# Query application status
{:ok, status} = PrismaticKernel.app_status(:prismatic_api)
# => %{status: :running, uptime: "14d 3h", memory: "124 MB", processes: 47}

# Trigger graceful shutdown with drain
:ok = PrismaticKernel.shutdown(:prismatic_crawler, drain_timeout: 30_000)

# Get dependency graph for visualization
{:ok, graph} = PrismaticKernel.dependency_graph()
# => %{nodes: 90, edges: 234, layers: 7}

NABLA Compliance

NABLA AxiomKernel EnforcementImplementation
Provenance MandatoryEvery health status change carries timestamp and sourceHealth events include full context metadata
Signal PluralityHealth aggregation requires multiple process signalsPer-app health combines process, memory, and message queue signals
Source IndependenceEach application's health checked independentlyAppRegistry maintains independent health state per app
Time DecayHealth status timestamps enable freshness evaluationStale health data triggers re-check
Absence InformativeMissing health reports indicate application failureAppRegistry detects absence and triggers investigation

Testing

Supervision tests verify dependency graph resolution, topological sort correctness, and startup ordering. Health aggregation tests verify correct status computation from individual application states including degraded and failing scenarios. Message routing tests verify PubSub delivery, request-reply patterns, and circuit breaker behavior.

Integration tests exercise the full startup sequence with mock applications to verify ordering and health gate enforcement. Property-based tests generate random dependency graphs to verify cycle detection and topological sort correctness invariants. Shutdown tests verify drain behavior using simulated in-flight requests to confirm zero-request-loss guarantees.

Integration Points

ApplicationRelationship
Every platform applicationAll 90+ apps depend on Kernel for supervision and lifecycle
Prismatic TelemetrySystem metrics emitted through telemetry events
Prismatic SafetySafety constraints on restart and shutdown operations
Prismatic CoreHigher-level platform coordination built on Kernel primitives
Prismatic ResilienceResilience patterns coordinated with Kernel

Performance

OperationLatencyNotes
Health query< 5msETS-backed aggregation
PubSub message routing< 1msIn-memory message passing
Request-reply (local)< 10msGenServer call with timeout
App status query< 1msETS registry lookup
Full platform health< 50msAggregation across 90+ apps
Startup sequence (full)30-60s90+ apps with health verification
Graceful shutdown30-65sDrain + grace + cleanup

Telemetry events: [:prismatic, :kernel, :app_started], [:prismatic, :kernel, :health_changed], [:prismatic, :kernel, :resource_alert].


Connect & Contribute

Created by Tomáš Korcak (korczis) | Open Source under GHL