Exploring Garnet: A Next-Generation Cache-Store By Microsoft
Garnet is an exciting project from Microsoft Research that addresses the ever-growing demand for efficient data storage mechanisms to support interactive web applications and services. Let’s dive into the details:
Introducing Garnet: A Next-Generation Cache-Store
What Is Garnet?
Garnet is an open-source, next-generation cache-store system designed to offer high performance, extensibility, and low latency. Researchers at Microsoft have been working on Garnet for nearly a decade, aiming to tackle the challenges posed by the rapid advances in interactive web applications and services.
Key Advantages of Garnet:
- Performance Gains: Garnet has been deployed in various use cases at Microsoft, including the Windows & Web Experiences Platform, Azure Resource Manager, and Azure Resource Graph. Its performance improvements over legacy cache-stores are significant.
- Extensibility: Unlike some existing cache systems, Garnet is explicitly designed to be easily extensible by application developers. This means you can build on its capabilities and tailor it to your specific needs.
- Rich Feature Set: Garnet supports more than just simple get/set operations. It offers APIs for raw strings, analytic data structures (like Hyperloglog), and complex data types (such as sorted sets and hash). Additionally, it allows users to checkpoint and recover the cache, create data shards, maintain replicated copies, and even handle transactions.
- Thread-Scalable: Garnet scales well within a single node, making it suitable for multi-core architectures.
- Open Source: Garnet is now available as an open-source download on GitHub. Microsoft aims to encourage collaboration, academic research, and further development in this critical area by open-sourcing it.
Use Cases and Deployment
Garnet has already found its way into various Microsoft services, demonstrating its practical applicability. Whether you’re dealing with Windows applications, web experiences, or managing resources in Azure, Garnet can help improve performance and reduce operational costs.
How to Get Started with Garnet
If you’re interested in exploring Garnet further, head over to the official Garnet GitHub repository to access the source code, documentation, and community discussions.
Use Docker for localhost work
docker run --network=host --ulimit memlock=-1 ghcr.io/microsoft/garnet
On Linux configured with basic authentication, replace {{replace_password_here}} with your password
docker run --network=host --ulimit memlock=-1 ghcr.io/microsoft/garnet --auth Password --password {{replace_password_here}}
Examples
Garnet with Console Application
Garnet implementation is as easy as RedisStack.
using System; using System.Threading.Tasks; using Garnet.Client; namespace GarnetClientSample { class Program { static async Task Main(string[] args) { // Replace with your Garnet server details (hostname and port) var garnetServer = new GarnetServer("localhost", 6379); try { // Connect to the Garnet server await garnetServer.ConnectAsync(); // Set a key-value pair await garnetServer.SetAsync("myKey", "Hello, Garnet!"); // Retrieve the value for a key var retrievedValue = await garnetServer.GetAsync("myKey"); Console.WriteLine($"Retrieved value: {retrievedValue}"); // Delete the key await garnetServer.DeleteAsync("myKey"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } finally { // Clean up and disconnect await garnetServer.DisconnectAsync(); } } } }
Garnet with ASP.NET WebAPI C#
In Program.cs or startup.cs
using Garnet.Server; // ... public void ConfigureServices(IServiceCollection services) { // Other services... services.AddGarnet(options => { options.Hostname = "localhost"; // Garnet server hostname options.Port = 6379; // Garnet server port }); }
In Controller class
using System; using System.Threading.Tasks; using Garnet.Client; using Microsoft.AspNetCore.Mvc; namespace GarnetWebApi.Controllers { [ApiController] [Route("api/[controller]")] public class CacheController : ControllerBase { private readonly GarnetServer _garnetServer; public CacheController(GarnetServer garnetServer) { _garnetServer = garnetServer; } [HttpGet("{key}")] public async Task<IActionResult> Get(string key) { try { var value = await _garnetServer.GetAsync(key); return Ok(value); } catch (Exception ex) { return BadRequest($"Error: {ex.Message}"); } } [HttpPost("{key}")] public async Task<IActionResult> Set(string key, [FromBody] string value) { try { await _garnetServer.SetAsync(key, value); return Ok("Value set successfully!"); } catch (Exception ex) { return BadRequest($"Error: {ex.Message}"); } } } }
Garnet is way more than this, so please feel free to try it in your project’s cache requirement or try it just for fun.
Happy Coding 🚀