Skip to content

Architecture principles ​

An architecture principle is a strong, reusable opinion on how something should be structured and designed — a guideline for how to architect, not the implementation detail of any one service. This page covers how Luna is put together as a set of microservices, the anatomy of a single service, the flavours a service can take, and the principles every service is expected to follow.

More detail and visuals

This page is the written reference. For the fuller picture — architecture diagrams, component maps, and worked examples — see the Luna architecture Miro board.

Microservices at Luna ​

Luna is a collection of decoupled microservices, each owning a slice of the business domain (engagements, expenses, auth, and so on). A microservice is an architectural pattern that decomposes business functionality into independent, loosely-coupled applications, and each Luna service is the source of truth for its own data — no service reaches into another's database directly.

Services communicate in two ways:

  • Asynchronously — via SNS topics and SQS queues.
  • Synchronously — over HTTP, through a shared API Gateway (or a local one where a service needs different or no auth).

How Luna services fit together and communicate

Anatomy of a microservice ​

A service is composed of specialised components following a separation of concerns. It contains only the components it needs, and at most one Lambda per type — one subscriber, one querier, and so on. Data stores, buckets, and topics are the exception: a service can have several of each.

Anatomy of a Luna microservice

ComponentHow many per service
Local API GatewayOne — only if the service has external/public endpoints
LambdaOne per type (e.g. one subscriber, one querier)
SQS queueOne
Dead-letter queue (DLQ)One per queue
SNS topicMultiple (or none)
DynamoDB tableMultiple (prefer one where possible)
S3 bucketMultiple (or none)

API Gateway ​

The API Gateway authenticates incoming requests and exposes client endpoints, integrating them to the Lambdas that handle them. There are two kinds:

  • Shared API Gateway — for internal API endpoints that authenticate with OKTA. Services import the shared gateway and its Cognito JWT authorizer rather than defining their own.
  • Local API Gateway — for external or public endpoints, where requests need different authentication (or none at all).

Lambdas ​

Luna services follow CQRS — responsibilities are split, with one Lambda per responsibility. Each Lambda type has a single, well-defined job:

LambdaTriggerResponsibility
GatewayExternal/public API GatewayHandles external endpoints — authorizes the request (an authenticated caller still must be allowed to act), validates it (usually schema validation), and transforms it into Luna's model
CommandsSync API — POST / PUT / PATCH / DELETEWrites and actions (DB writes, S3 writes, publishing messages). Flow: authorize → validate → business logic → transform → respond
QuerierSync API — GETReads (DB queries, S3 reads). Same flow, with pagination and response size in mind
SubscriberSQS (async events, incl. from S3)Consumes async messages; idempotent and order-independent. Throws on failure so the message retries and ultimately lands in the DLQ. Often tuned with a higher timeout/memory
SchedulerEventBridge cronRuns scheduled jobs. Throws only when the run should retry
PublisherService's DDB streamPublishes the corresponding SNS events when data changes
StreamsService's DDB streamTransforms stream records onto a Kinesis Firehose stream for the Redshift analytics pipeline
OLTP-StreamsInvoked by Streams (not the DDB stream directly)Writes the forwarded records into the OLTP (Aurora) database

Databases and buckets ​

A service can have any number of databases (or none), in one of two flavours:

  • NoSQL — DynamoDB. A service may have multiple tables, but prefer keeping data in one table where possible.
  • SQL — Postgres on serverless Aurora.

Databases must have Point-In-Time Recovery enabled and be backed up cross-account. Services can also have S3 buckets for storing objects (images, video, and so on); these are likewise backed up cross-account.

Queues and DLQs ​

If a service does async communication, it goes through a queue:

  • One queue per service.
  • A standard queue by default — FIFO where that's more appropriate.
  • A batch size of 1, unless a larger batch is better for performance.
  • Every queue has a DLQ, which retains messages for 14 days.

Topics ​

A service can have multiple topics, or none. Topics follow a one-topic-per-event rule — e.g. an andi-updated topic for the ANDI_UPDATED event.

Monitoring and alerting ​

Each service carries enough monitoring and alerting to be observable — alarms and alerts across its resources (the local API Gateway, Lambdas, queue/DLQ, and so on). Log groups and every other resource are defined as code so a service can be recreated from scratch with no manual resource creation.

Flavours of microservice ​

Every service is one — or a blend — of the archetypes below. The archetype drives which components a service has and how it relates to data and users. When designing a new service, work out its flavour by asking:

  • Is it the source of truth for core domain data, or does it consume data owned by other services?
  • Does it serve end users directly, or support other services behind the scenes?
  • What does it depend on, and how does data flow in and out?
  • How does it integrate with third-party systems?
  • Is it single-purpose, or does it group related functionality and entities?
  • Does it coordinate workflows across several services?
  • What level does it operate at — domain-specific, platform-wide, or utility?
FlavourWhat it isExamples
DomainSource of truth for core entities. CRUD over API, owns a DynamoDB table, publishes events on changeexpenses, shares
FeatureUser-facing; aggregates and transforms data owned by domain services. May keep derived copies, but isn't the source of truthrevenue-forecast, reports
UtilityGeneric, reusable capability (e.g. emails, PDF generation); idempotent, holds no core domain dataemails, pdf-generator, scheduler
GatewayIntegrates third-party systems via in/outbound webhooks; stores credentials; often a local API Gateway with public endpointsfinance-gateway, ai-gateway, employee-gateway
Micro-monolithSeveral flavours fused around one strongly-cohesive entity (domain + feature together)engagements, skills, invoices
OrchestratorCoordinates workflows across services (e.g. Step Functions, scheduled fan-out); stores workflow metadata, not domain dataNo current example. The marketing-qualified-leads service used to implement it (Step Functions state machine); see its code at the last commit before removal
PlatformCross-cutting capability every service relies on; operates at the infrastructure levelauth, janitor

Principles ​

The following principles are inter-linked and complement one another.

Separation of responsibility — principle of least knowledge ​

Responsibilities are separated into independent functionalities, each knowing only what it needs to. Read is separate from write; sync is separate from async; use only the information you need. CQRS is a good fit for this.

Observability, resilience, scalability (ORS) ​

Services are built to be observable (monitoring, alerting, the right level of logging), resilient to faults elsewhere in the system (and when they can't be, failing gracefully and meaningfully), and scalable to meet future demand.

Domain encapsulation ​

Domains are independent entities representing an atomic unit of business operations, and are the source of truth for their own data. No domain directly accesses or modifies another domain's data — dependent domains get it through the appropriate interface (messages or APIs).

Secure — principle of least privilege, zero trust ​

Services are secure by design and assume the principle of least privilege, holding access only to the resources they need.

One topic per event ​

Emit one SNS topic per event. This reduces Lambda invocations (and cost), allows finer granularity that helps observability and resilience, and keeps concerns separated per topic.

Business-centric modelling ​

Model services around the business, not around the technology.

Made with ❤️ by the Luna team