# ziggycreatures/fusioncache architecture

> Architecture diagram of the GitHub repository ziggycreatures/fusioncache, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ziggycreatures/fusioncache
- Repository: https://github.com/ziggycreatures/fusioncache

## Overview

Based on both the file tree and the README, FusionCache is a modular, hybrid caching library intended to provide a fast, resilient, multi-level caching solution for .NET applications. It’s not a typical full-stack web application; instead, it is a library (with multiple supporting modules) that provides both in-memory (L1) and distributed (L2) caching, along with resiliency features such as cache stampede protection, fail-safe mechanisms, auto-recovery, and even diagnostic/observability integrations like OpenTelemetry and structured logging.

##### Main Components
- Core Cache Engine: Represent the FusionCache core (located in the “src/ZiggyCreatures.FusionCache” directory). This component encapsulates the main API (GetOrSet, builder extensions, internal caching logic) and is responsible for coordinating cache read/write, fallback strategies, and resiliency.
- Caching Levels:
– L1 (Memory Cache): The in-process memory cache that quickly serves repeated requests.
– L2 (Distributed Cache): An optional distributed cache, via any implementation of the IDistributedCache interface, to allow for remote fallback and caching on multiple nodes.
- Backplane Components: These are used for synchronizing caches between nodes. There are separate backplane implementations such as:
– MemoryBackplane (src/ZiggyCreatures.FusionCache.Backplane.Memory)
– RedisBackplane (src/ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis)
- Supporting Modules and Plugins:
– Resiliency Modules (fail-safe, auto-recovery, cache stampede protection – implemented in the core as well as in helper classes under the Internals folder).
– Serialization Modules: Multiple serialization projects are provided (e.g. NewtonsoftJson, SystemTextJson, ProtoBufNet, CysharpMemoryPack, ServiceStackJson). These define how the entries are stored/transmitted especially when using distributed caching.
– Observability: The OpenTelemetry module (src/ZiggyCreatures.FusionCache.OpenTelemetry) and events/logging systems support metrics and diagnostics.
– ASP.NET Core Integration: A dedicated project for output caching integration (src/ZiggyCreatures.FusionCache.AspNetCore.OutputCaching) which is used for web scenarios, indicating that FusionCache can plug into web frameworks.
– Chaos Module: (src/ZiggyCreatures.FusionCache.Chaos) used for testing resilience by simulating failures or latency.
– Microsoft HybridCache integration: This module shows that FusionCache “talks” to Microsoft’s own caching abstractions.
- Additional utilities and extension points:
– Dependency Injection and Builder Extensions: Illustrates how FusionCache is configured and integrated within the host application.
– Plugins: Custom plug-in mechanisms (provided in the Plugins folder) that allow extension of core functionality, like additional metrics or custom behaviors.
- External Interactions:
– Integration points with standard external services such as IDistributedCache implementations, external data sources for factory methods, and potentially other logging/monitoring infrastructures.

##### Relationships and Data Flow
- The core caching component sits centrally. It directly communicates to the L1 memory cache for the fastest operations.
- When cache misses occur in the L1, it falls back to the L2 provider (if configured).
- If the cache is used across multiple nodes, a backplane (either in-memory for testing or Redis for production) is used to propagate cache invalidation or refresh events across nodes.
- The resiliency and fail-safe aspects intercept calls to the data source (factory method) and add circuit-breaker and timeout logic.
- Serializer modules interact with both the L2 caching layer and the backplane, converting cache entries to a transferable format.
- The dependency injection and builder extensions play a role in wiring up these components so that the user (or host application) doesn’t manually instantiate all the pieces.
- Observability modules (logging, OpenTelemetry instrumentation) hook into the core operations to propagate metrics and events.

##### Architectural Patterns & Design Principles
- Multi-Level (Hybrid) Caching: A pattern where an L1 fast cache is supplemented by an optional L2 distributed cache.
- Pluggability/Modularity: Separate serializer, backplane, and integration modules are self-contained and can be swapped out.
- Resiliency Strategy: With fail-safe, cache stampede protection, and auto-recovery to ensure service continuity.
- Dependency Injection & Fluent Builder Pattern: The design promotes configuring the core using DI and fluent interface extensions.
- Observability: Instrumentation is baked in using standardized interfaces such as ILogger and OpenTelemetry extensions, making the system’s internals transparent.

##### Conclusion
- At the center, the FusionCache core that orchestrates cache operations.
- Clearly delineated L1 (memory) and L2 (distributed) caching layers with fallback mechanisms.
- Side-by-side components for resiliency (fail-safe, auto-recovery, cache stampede protection), backplane synchronization, and observability.
- Separate modules for serializer implementations, ASP.NET output caching, and experimental chaos handling.
- Directional flows and interactions that emphasize how caching requests propagate and how configurations and plugins extend the core functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    EX["Application Request"]:::external

    subgraph "FusionCache Library"
        direction TB
        A["FusionCache Core"]:::core
        B["L1 Memory Cache"]:::l1
        C["Distributed Cache (L2)"]:::l2
        D["Memory Backplane"]:::backplane
        E["Redis Backplane"]:::backplane
        F["Resiliency / Chaos Module"]:::resiliency
        subgraph "Serialization Modules"
            direction TB
            G["NewtonsoftJson"]:::serialization
            H["SystemTextJson"]:::serialization
            I["ProtoBufNet"]:::serialization
            J["CysharpMemoryPack"]:::serialization
            K["ServiceStackJson"]:::serialization
        end
        L["Observability (OpenTelemetry)"]:::observability
        M["ASP.NET Output Caching"]:::aspnet
        N["Microsoft HybridCache Integration"]:::hybrid
        O["Plugins / Extension Points"]:::plugins
        P["Dependency Injection & Builder"]:::builder
    end

    %% External request flows
    EX -->|"calls"| A
    P -->|"configures"| A

    %% Core caching flows
    A -->|"callsL1"| B
    A -->|"fallbackToL2"| C
    A -->|"syncUpdate"| D
    A -->|"syncUpdate"| E
    A -->|"invokesResiliency"| F
    A -->|"observability"| L
    A -->|"integrates"| M
    A -->|"integrates"| N
    A -->|"pluginHook"| O

    %% Serialization interactions from Distributed Cache
    C -->|"serialize"| G
    C -->|"serialize"| H
    C -->|"serialize"| I
    C -->|"serialize"| J
    C -->|"serialize"| K

    %% Additional serialization interactions from Backplanes
    D -->|"serialize"| G
    E -->|"serialize"| H

    %% Class Definitions for styling
    classDef core fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef l1 fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef l2 fill:#ccffff,stroke:#006666,stroke-width:2px;
    classDef backplane fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef resiliency fill:#ffccff,stroke:#990099,stroke-width:2px;
    classDef serialization fill:#e0e0e0,stroke:#333333,stroke-width:2px;
    classDef observability fill:#cce6ff,stroke:#3366ff,stroke-width:2px;
    classDef aspnet fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef hybrid fill:#fff2cc,stroke:#e69138,stroke-width:2px;
    classDef plugins fill:#fce5cd,stroke:#ff9900,stroke-width:2px;
    classDef builder fill:#d9d2e9,stroke:#8e7cc3,stroke-width:2px;
    classDef external fill:#f4cccc,stroke:#ea9999,stroke-width:2px;

    %% Click Events
    click A "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache"
    click B "https://github.com/ziggycreatures/fusioncache/tree/main/src/ZiggyCreatures.FusionCache/Internals/Memory"
    click C "https://github.com/ziggycreatures/fusioncache/tree/main/src/ZiggyCreatures.FusionCache/NullObjects"
    click D "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Backplane.Memory"
    click E "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis"
    click F "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Chaos"
    click G "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Serialization.NewtonsoftJson"
    click H "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Serialization.SystemTextJson"
    click I "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Serialization.ProtoBufNet"
    click J "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Serialization.CysharpMemoryPack"
    click K "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.Serialization.ServiceStackJson"
    click L "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.OpenTelemetry"
    click M "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.AspNetCore.OutputCaching"
    click N "https://github.com/ziggycreatures/fusioncache/blob/main/src/ZiggyCreatures.FusionCache.MicrosoftHybridCache"
    click O "https://github.com/ziggycreatures/fusioncache/tree/main/src/ZiggyCreatures.FusionCache/Plugins"
    click P "https://github.com/ziggycreatures/fusioncache/tree/main/src/ZiggyCreatures.FusionCache/Internals/Builder"
```
