Skip to content

A distributed in-memory cache system with TTL, LRU eviction, replication, failover, and load testing using Node.js, Docker, and k6.

Notifications You must be signed in to change notification settings

sanskritityagi31/distributed-cache-system

Repository files navigation

📦 Distributed Cache System

A Node.js–based distributed in-memory cache system designed to reduce database load and improve read performance under concurrent traffic.
The system supports sharding, TTL-based expiration, LRU eviction, replication, failover simulation, and is Dockerized and load tested.


🚀 Features

  • Distributed cache nodes
  • Gateway-based request routing
  • Hash-based sharding
  • TTL-based expiration
  • LRU eviction
  • Replication and failover simulation
  • Docker & Docker Compose support
  • Load testing using k6

🏗️ Architecture (Textual)

Client → Gateway → Cache Nodes → Database

  • All client requests go through a Gateway
  • The Gateway shards keys across cache nodes
  • Cache nodes store data in memory
  • On cache miss, data is fetched from DB and cached

🔁 Request Flow (GET)

  1. Client sends GET request to Gateway
  2. Gateway routes request to cache shard
  3. Cache hit → return value
  4. Cache miss → fetch from DB
  5. Store in cache with TTL
  6. Return response to client

🧠 Replication & Consistency Model

  • Hash-based sharding assigns keys to primary nodes
  • Writes are replicated to a secondary node
  • Reads are served from the primary
  • Replica takes over on primary failure
  • Eventual consistency model

🛠️ Tech Stack

  • Node.js, Express
  • Docker, Docker Compose
  • AWS EC2
  • k6 (Load Testing)
  • Jest (Testing)

📡 API Endpoints

Set Cache Value

curl -X POST http://localhost:3000/cache \
-H "Content-Type: application/json" \
-d '{"key":"user123","value":"Alice","ttl":60}'

Get Cache Value
curl http://localhost:3000/cache/user123

Delete Cache Value
curl -X DELETE http://localhost:3000/cache/user123

📈 Performance

~6,000 requests/sec

Avg latency ~45 ms

P95 latency ~120 ms

Stable under node failure

🐳 Run with Docker
docker-compose up --build

👤 Author

Sanskriti Tyagi
GitHub: https://github.com/sanskritityagi31

About

A distributed in-memory cache system with TTL, LRU eviction, replication, failover, and load testing using Node.js, Docker, and k6.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published