Why FastCache Is the Go Developer’s Secret Weapon for Ultra‑Fast, Zero‑GC Local Caching
FastCache, an open‑source Go in‑memory cache from the VictoriaMetrics team, delivers extreme speed, zero garbage‑collector pressure, and built‑in thread safety through a shard‑bucket design, while offering a five‑method API that lets developers cache massive key‑value data with minimal configuration, making it ideal for high‑concurrency services and low‑latency workloads.
What is FastCache?
FastCache is an open‑source Go local‑cache library created by the VictoriaMetrics team. It targets high‑performance, low‑overhead scenarios where traditional caches such as sync.Map or a plain map with locks suffer from performance bottlenecks and heavy GC pressure.
In short: a lightweight, ultra‑fast, zero‑GC local cache that can store massive key‑value data without destabilising Go’s garbage collector.
Core Advantages
Extreme Speed – Sharded bucket + local lock design enables multi‑core parallelism, giving linear throughput growth and handling tens of millions of QPS.
Zero GC Pressure – Data lives in pre‑allocated memory blocks without extra pointers, eliminating GC‑induced latency spikes.
Simple API – Only five methods ( New, Set, Get, Del, Has) make it usable within five minutes, with no configuration required.
Thread‑Safety – Concurrency control is internal, allowing any number of goroutines to read/write without manual locking.
Automatic Eviction – A fixed maximum memory size automatically evicts the oldest entries when the cache is full, preventing memory blow‑out.
Gotchas (Pitfalls)
✅ Key/Value must be []byte → serialize strings/structs yourself
✅ No per‑item TTL → store a timestamp in the value and check on read
✅ Single value > 64KB → use SetBig for stable performance
✅ Memory usage is fixed after initialization → suitable for long‑running servicesQuick‑Start Code (Copy‑Paste Ready)
1️⃣ Install Dependency
go get github.com/VictoriaMetrics/fastcache2️⃣ Basic Usage (≈20 lines)
package main
import (
"fmt"
"github.com/VictoriaMetrics/fastcache"
)
func main() {
// Initialise a 100 MB cache
cache := fastcache.New(100 * 1024 * 1024)
// Set a key/value (both []byte)
cache.Set([]byte("user:1001"), []byte(`{"name":"张三","age":25}`))
// Get the value
var dst []byte
if val, ok := cache.Get(dst, []byte("user:1001")); ok {
fmt.Println("✅ Get success:", string(val))
}
// Check existence and delete
if cache.Has([]byte("user:1001")) {
cache.Del([]byte("user:1001"))
fmt.Println("🗑️ Cache entry deleted")
}
}Advanced: Storing Structs (Serialization Tips)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize
user := User{Name: "李四", Age: 30}
value, _ := json.Marshal(user)
cache.Set([]byte("user:1002"), value)
// Deserialize
var dst []byte
if val, ok := cache.Get(dst, []byte("user:1002")); ok {
var u User
json.Unmarshal(val, &u)
fmt.Println("👤 User info:", u) // Output: {李四 30}
}Applicable Scenarios (Quick Reference)
✅ High‑concurrency APIs, gateways, recommendation systems (million‑level QPS, low latency)
✅ Monitoring metrics, IoT device state streams (time‑series, large data volume)
✅ Simple cache needs (store‑retrieve‑delete without extra features)
❌ Not suitable when precise per‑item expiration is required (e.g., 5‑minute TTL)
❌ Not ideal for complex eviction policies like LRU/LFU
❌ Avoid storing values larger than 1 MB frequently
Conclusion
FastCache combines three key qualities—speed, lightweight API, and stability—by using a sharded bucket design, zero‑GC memory layout, and automatic memory management. For Go services that demand high concurrency and low latency, FastCache offers a ready‑to‑use, configuration‑free solution that keeps the runtime stable over long periods.
Go Development Architecture Practice
Daily sharing of Golang-related technical articles, practical resources, language news, tutorials, real-world projects, and more. Looking forward to growing together. Let's go!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
