DEV Community

Alex Spinov
Alex Spinov

Posted on

Garnet Has a Free API: Microsoft's High-Performance Redis-Compatible Cache

Garnet is a new remote cache-store from Microsoft Research that implements the Redis RESP protocol. Written in C#, it delivers better throughput and lower latency than Redis with features like server-side stored procedures.

What Is Garnet?

Garnet is a high-performance cache-store that offers significantly better throughput and scalability than existing solutions. It uses a novel storage engine based on FASTER and provides Redis-compatible API with extensions.

Key Features:

  • Redis RESP protocol compatible
  • Better throughput than Redis
  • Server-side stored procedures (C#)
  • Cluster mode with sharding
  • TLS encryption
  • Object store for complex types
  • Pub/Sub support
  • Checkpoint and recovery

Quick Start

# Docker
docker run -d --name garnet -p 6379:6379 ghcr.io/microsoft/garnet:latest

# Or build from source
git clone https://github.com/microsoft/garnet.git
cd garnet
dotnet run -c Release -f net8.0 --project Garnet

# Connect with any Redis client!
redis-cli -p 6379
Enter fullscreen mode Exit fullscreen mode

Garnet API (Redis-Compatible)

import redis

# Same Redis client works!
r = redis.Redis(host="localhost", port=6379)

# String operations
r.set("session:abc123", "user-42")
r.expire("session:abc123", 3600)
print(r.get("session:abc123").decode())

# Hash operations
r.hset("cache:product:1", mapping={
    "name": "Laptop",
    "price": "999.99",
    "cached_at": "2026-03-29T10:00:00Z"
})

# List operations (job queue)
r.lpush("jobs:pending", "job-1", "job-2", "job-3")
job = r.rpop("jobs:pending")
print(f"Processing: {job.decode()}")

# Sorted sets
r.zadd("trending", {"ai-tools": 500, "kubernetes": 320, "rust": 280})
top = r.zrevrange("trending", 0, 4, withscores=True)
for topic, score in top:
    print(f"{topic.decode()}: {int(score)}")

# Streams
r.xadd("audit", {"action": "login", "user": "admin", "ip": "192.168.1.1"})
Enter fullscreen mode Exit fullscreen mode

Garnet Extensions

// Server-side stored procedures (C#)
// Register custom commands in Garnet
public class RateLimiter : CustomTransactionProcedure
{
    public override bool Execute()
    {
        var key = GetNextArg().ToString();
        var limit = int.Parse(GetNextArg().ToString());
        var window = int.Parse(GetNextArg().ToString());

        var count = api.StringIncrement(key);
        if (count == 1) api.KeyExpire(key, TimeSpan.FromSeconds(window));

        WriteSimpleString(count <= limit ? "ALLOWED" : "DENIED");
        return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

Garnet vs Redis vs KeyDB

Feature Garnet Redis KeyDB
Language C# (.NET) C C++
Threading Multi-threaded Single Multi
Stored procs C# Lua Lua/JS
License MIT SSPL BSD
Storage FASTER engine In-memory In-memory+SSD
By Microsoft Redis Ltd Snap

Resources


Need to scrape web data for your caching layer? Check out my web scraping tools on Apify — production-ready actors for Reddit, Google Maps, and more. Questions? Email me at spinov001@gmail.com

Top comments (0)