l1l2-cache-patterns

Implement two-tier GET-by-ID caching with freecache, Dragonfly, and NATS invalidation.

1|Updated Apr 17, 2026
One-click install
npx skills add https://github.com/PremModhaOfficial/motadata-ai-pipeline --skill l1l2-cache-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: l1l2-cache-patterns
Source: https://github.com/PremModhaOfficial/motadata-ai-pipeline/tree/main/.claude/skills/l1l2-cache-patterns
Command: npx skills add https://github.com/PremModhaOfficial/motadata-ai-pipeline --skill l1l2-cache-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Two-tier caching reduces database load and latency in multi-tenant microservices by keeping hot data in an in-process L1 cache and a shared L2 cache, while ensuring tenant isolation.

Core Features & Use Cases

  • L1: In-process freecache per replica with tenant-keyed keys and 60s TTL for GET-by-ID lookups.
  • L2: Dragonfly, a Redis-compatible shared cache with tenant-keyed keys and 300s TTL, checked by DAL before hitting PostgreSQL.
  • Cache invalidation: NATS events from DAL to entity services keep L1 and L2 coherent; NATS KV Store is not used for caching.

Quick Start

Configure per-replica L1 freecache and shared L2 Dragonfly, and wire NATS-based invalidation to enable tenant-aware GET-by-ID caching.

Frequently Asked Questions about l1l2-cache-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement two-tier caching for multi-tenant microservices?

Implement two-tier caching by using an in-process L1 freecache with 60s TTL per replica and a shared L2 Dragonfly cache with 300s TTL, ensuring tenant isolation through tenant-prefixed keys for GET-by-ID lookups.

What is the best way to maintain cache coherence across L1 and L2 cache tiers?

Maintain cache coherence by emitting NATS events from the DAL to entity services, triggering synchronized eviction and invalidation flows across both L1 and L2 cache layers whenever underlying data changes.

Does this two-tier caching approach support tenant isolation with Dragonfly and freecache?

Yes, tenant isolation is supported by applying tenant-prefixed keys across both the in-process freecache L1 layer and the shared Dragonfly L2 cache, preventing cross-tenant data access during GET-by-ID lookups.

How do I minimize database load in multi-tenant microservices without sacrificing read latency?

Minimize database load by checking the per-replica L1 freecache first, then the shared L2 Dragonfly cache, so that PostgreSQL is only queried by the DAL when the requested data is absent from both cache tiers.

Can I use NATS for cache invalidation instead of NATS KV Store in a multi-tenant architecture?

Yes, NATS events are used specifically to drive cache invalidation and eviction flows to keep L1 and L2 coherent, while explicitly avoiding NATS KV Store for caching to ensure data consistency through the DAL.

When should I not use a shared L2 cache for my multi-tenant application?

Avoid relying solely on a shared L2 cache when ultra-low latency is critical, as the per-replica L1 freecache handles hot data access faster; L2 Dragonfly serves as a fallback to reduce PostgreSQL load before hitting the database.