# Layra > A curated directory of software architecture patterns optimized for LLM consumption. ## Docs ### System Architecture - [CQRS / Event Sourcing](https://layra4.dev/patterns/cqrs-event-sourcing.md): Separates read and write models into distinct paths, with Event Sourcing storing every state change as an immutable event rather than overwriting current state. Provides a complete audit trail and enables powerful temporal queries. - [Event-Driven Architecture](https://layra4.dev/patterns/event-driven.md): An architecture where components communicate by producing and consuming events, enabling loose coupling and asynchronous processing. Components react to what has happened rather than being told what to do. - [Hexagonal Architecture](https://layra4.dev/patterns/hexagonal.md): An architecture that isolates core domain logic from external concerns by defining ports (interfaces) that the domain exposes and adapters that implement those interfaces for specific technologies. - [Microservices](https://layra4.dev/patterns/microservices.md): An architecture where the system is composed of small, independently deployable services, each organized around a specific business capability and communicating over network protocols. - [Modular Monolith](https://layra4.dev/patterns/modular-monolith.md): A monolith with well-defined module boundaries that enforce separation of concerns while retaining the simplicity of a single deployment unit. Increasingly recognized as the sweet spot between monolith and microservices. - [Monolith](https://layra4.dev/patterns/monolith.md): A single deployment unit where all application code lives in one codebase and is deployed as one artifact. The simplest architecture to start with and the right default for most new projects. - [Serverless](https://layra4.dev/patterns/serverless.md): An architecture where the cloud provider manages all server infrastructure, automatically scaling functions from zero to handle requests and charging only for actual execution time. ### Application Architecture - [Clean Architecture](https://layra4.dev/patterns/clean-architecture.md): Organizes code into concentric layers where dependencies always point inward toward the domain, making business logic independent of frameworks, databases, and delivery mechanisms. - [Domain-Driven Design](https://layra4.dev/patterns/domain-driven-design.md): Models complex business domains through a ubiquitous language shared between developers and domain experts, organizing code around bounded contexts, aggregates, and value objects to keep software aligned with business reality. - [Layered / N-Tier](https://layra4.dev/patterns/layered-n-tier.md): Organizes code into horizontal layers — typically presentation, business logic, and data access — where each layer only calls the one directly below it, providing a simple and intuitive structure. - [MVC](https://layra4.dev/patterns/mvc.md): Separates an application into three interconnected components — Model (data/logic), View (presentation), and Controller (input handling) — enabling parallel development and easier testing. - [Vertical Slice Architecture](https://layra4.dev/patterns/vertical-slice.md): Organizes code by feature rather than by technical layer — each 'slice' contains everything needed to handle a single use case (request, handler, validation, persistence, response), minimizing cross-cutting coupling and making features independently developable. ### Data Architecture - [AI Feature Store](https://layra4.dev/patterns/ai-feature-store.md): A centralized platform for defining, storing, and serving ML features consistently across training and inference, ensuring point-in-time correctness, reuse, and low-latency access for both batch and real-time workloads. - [Batch Processing](https://layra4.dev/patterns/batch-processing.md): Processing large datasets by breaking computation into parallel tasks distributed across a cluster. Input and output are immutable files on a distributed filesystem. Evolved from rigid MapReduce (map then reduce) to flexible dataflow engines (arbitrary DAGs of operators) that pipeline data through memory rather than materializing to disk between stages. - [Change Data Capture](https://layra4.dev/patterns/change-data-capture.md): Observing all data changes written to a database and extracting them as a stream of events that can be consumed by other systems. Turns the database's internal replication log into an external API, enabling derived systems (search indexes, caches, data warehouses) to stay in sync without tight coupling. - [Column-Oriented Storage](https://layra4.dev/patterns/column-oriented-storage.md): Storing data by column rather than by row, so that analytical queries scanning millions of rows but only a few columns read far less data from disk. Combined with aggressive compression (bitmap encoding, run-length encoding) and vectorized CPU processing, column stores can be 100x faster than row stores for analytical workloads. - [Data Encoding & Schema Evolution](https://layra4.dev/patterns/schema-evolution.md): Strategies for encoding data in a way that supports independent evolution of producers and consumers. Binary encoding formats (Protocol Buffers, Avro, Thrift) use schemas with field tags or name resolution to enable forward compatibility (old code reads new data) and backward compatibility (new code reads old data) — critical for rolling deployments, microservices, and long-lived data storage. - [Data Partitioning](https://layra4.dev/patterns/data-partitioning.md): Splitting a large dataset across multiple nodes so that each node stores and processes a subset of the data. The two primary strategies — key range partitioning and hash partitioning — trade off between efficient range queries and even load distribution. - [Data Replication Strategies](https://layra4.dev/patterns/replication-strategies.md): Patterns for keeping copies of data on multiple machines to improve availability, reduce latency, and scale reads. The three fundamental approaches — single-leader, multi-leader, and leaderless — each offer different trade-offs between consistency, availability, and complexity. - [Distributed Consensus](https://layra4.dev/patterns/consensus-algorithms.md): Getting multiple nodes to agree on a value in the presence of faults. Consensus algorithms (Raft, Paxos, Zab) enable leader election, distributed locking, atomic broadcast, and linearizable storage — the foundational building blocks for any distributed system that needs coordination. Critically important, notoriously difficult to implement correctly. - [Polyglot Persistence & Data Integration](https://layra4.dev/patterns/polyglot-persistence.md): Using multiple specialized data stores — each optimized for a specific access pattern — connected by asynchronous event streams rather than relying on a single general-purpose database. The primary database is the system of record; all other representations (search indexes, caches, analytics stores) are derived data, rebuilt from the authoritative source via change data capture or event logs. - [Stream Processing](https://layra4.dev/patterns/stream-processing.md): Processing unbounded datasets as events arrive, rather than in fixed-size batches. Stream processors consume ordered event logs, maintain state, perform windowed computations and joins, and produce derived outputs continuously — enabling near-real-time analytics, materialized views, and event-driven workflows. - [Transaction Isolation Strategies](https://layra4.dev/patterns/transaction-isolation.md): Isolation levels determine what concurrency anomalies a database prevents. The spectrum runs from Read Committed (prevents dirty reads/writes) through Snapshot Isolation (adds consistent reads via MVCC) to Serializable (prevents all race conditions). Three serializable implementations exist: actual serial execution, two-phase locking, and serializable snapshot isolation — each with radically different performance characteristics. - [Vector Database Architecture](https://layra4.dev/patterns/vector-database.md): Purpose-built storage engines optimized for indexing, storing, and querying high-dimensional vector embeddings, enabling fast approximate nearest-neighbor search for AI/ML workloads, semantic search, and recommendation systems. ### AI & LLM Architecture - [Agent Orchestration](https://layra4.dev/patterns/agent-orchestration.md): Uses an LLM as a reasoning engine that plans, selects tools, and executes multi-step actions autonomously to accomplish a user's goal. - [Chain-of-Thought & Reasoning Patterns](https://layra4.dev/patterns/chain-of-thought.md): Elicits step-by-step reasoning from LLMs through prompting techniques like chain-of-thought, self-consistency sampling, and tree-of-thought search, improving accuracy on complex tasks without changing the model or infrastructure. - [Few-Shot & In-Context Learning](https://layra4.dev/patterns/few-shot-learning.md): Teaches LLMs desired behavior by including carefully selected examples in the prompt, enabling task-specific performance without fine-tuning through static example sets or dynamically retrieved demonstrations. - [Fine-Tuning Pipeline](https://layra4.dev/patterns/fine-tuning-pipeline.md): Adapts a pre-trained language model to specific tasks or domains by training on curated datasets, using techniques ranging from parameter-efficient methods like LoRA to full fine-tuning and alignment via RLHF or DPO. - [LLM Guardrails & Evaluation](https://layra4.dev/patterns/llm-guardrails.md): A defense-in-depth architecture for validating LLM inputs and outputs, detecting harmful content and hallucinations, and continuously evaluating model quality through automated frameworks and adversarial testing. - [LLM Memory Architecture](https://layra4.dev/patterns/llm-memory.md): Provides LLM-based agents and chatbots with structured short-term and long-term memory systems, enabling context retention across conversations, experience recall, and progressive knowledge accumulation beyond a single context window. - [LLM Routing & Model Gateway](https://layra4.dev/patterns/llm-routing.md): Routes LLM requests to the optimal model based on query complexity, cost constraints, and latency requirements, providing a unified API abstraction across multiple providers with fallback chains and budget management. - [Multi-Agent Systems](https://layra4.dev/patterns/multi-agent-systems.md): Multiple specialized AI agents collaborate, delegate, and communicate to solve complex tasks that exceed the capability of any single agent. - [Prompt Chaining](https://layra4.dev/patterns/prompt-chaining.md): Decomposes complex LLM tasks into a sequence of simpler prompt steps where each step's output feeds into the next, enabling reliable multi-step reasoning with validation and error handling between stages. - [Prompt Template Management](https://layra4.dev/patterns/prompt-management.md): Treats prompts as first-class software artifacts with versioning, composition, A/B testing, evaluation pipelines, and deployment workflows, enabling teams to iterate on LLM behavior with the same rigor as shipping code. - [RAG Architecture](https://layra4.dev/patterns/rag-architecture.md): Augments LLM responses with relevant context retrieved from external knowledge bases, reducing hallucinations and enabling domain-specific answers without fine-tuning. - [Semantic Search Architecture](https://layra4.dev/patterns/semantic-search.md): Combines dense vector retrieval, sparse lexical matching, and neural reranking to deliver search results that understand meaning and intent rather than relying solely on keyword overlap. - [Structured Output Generation](https://layra4.dev/patterns/structured-output.md): Constrains LLM outputs to conform to predefined schemas using provider JSON modes, grammar-based decoding, or validation-retry loops, ensuring type-safe, parseable results for downstream systems. - [Tool Use & Function Calling](https://layra4.dev/patterns/tool-use.md): Extends LLM capabilities by allowing models to invoke external tools and functions, enabling real-world actions like API calls, database queries, code execution, and multi-step workflows through structured schema definitions and execution loops. ### Integration Patterns - [API Gateway](https://layra4.dev/patterns/api-gateway.md): Provides a single entry point for all client requests, centralizing cross-cutting concerns like routing, authentication, rate limiting, and protocol translation. - [Circuit Breaker](https://layra4.dev/patterns/circuit-breaker.md): Prevents cascading failures in distributed systems by wrapping remote calls in a state machine that trips open after repeated failures, fast-failing subsequent requests instead of waiting for timeouts. - [Publish-Subscribe](https://layra4.dev/patterns/pub-sub.md): Decouples message producers from consumers by introducing a broker with named topics. Publishers send messages to topics without knowing who will receive them; subscribers register interest in topics and receive all matching messages. - [Saga Pattern](https://layra4.dev/patterns/saga.md): Manages distributed transactions across multiple services by breaking them into a sequence of local transactions, each with a compensating action that undoes its work if a later step fails. - [Service Mesh](https://layra4.dev/patterns/service-mesh.md): A dedicated infrastructure layer for handling service-to-service communication in microservices, implemented as a network of sidecar proxies that transparently manage traffic routing, security (mTLS), observability, and resilience without changing application code.