Skip to content

🔥 feat: eliminate global mutex contention across storage-backed middleware (limiter, cache, session) #4358

Description

@pageton

Problem

A performance architecture review identified that rate limiter, cache, and session middleware all hold a global mutex while performing external storage I/O (Redis, database, etc.). This serializes all requests through these middleware regardless of key, collapsing throughput to ~1/storage-latency.

Affected middleware

Middleware File Lock I/O under lock
Limiter (fixed) middleware/limiter/limiter_fixed.go:52-91 mux.Lock() manager.get() + manager.set()
Limiter (sliding) middleware/limiter/limiter_sliding.go:54-102 mux.Lock() manager.get() + manager.set()
Cache middleware/cache/cache.go:313-546 mux.Lock() storage.Get() / storage.Set()
Session middleware/session/middleware.go:144-157 m.mu.Lock() store.Get()storage.GetWithContext()

Impact

With a 2ms Redis round-trip:

  • Effective throughput through any of these middleware caps at ~500 RPS regardless of hardware or concurrency.
  • All requests for different keys/IPs serialize on a single mutex.
  • At 10K QPS, this becomes the dominant bottleneck.

Proposed fix

Pattern: Per-key striped locking.

  1. Replace the single sync.RWMutex with a fixed array of 32 or 64 shards, each with its own mutex.
  2. Hash the key (IP, cache key, session ID) to select the shard.
  3. Never hold any shard lock across storage I/O — lock, read/write in-memory state, unlock, then perform storage I/O without the lock, then re-lock only to update in-memory state.
  4. Optionally keep an in-memory atomic counter as a fast path, syncing to storage asynchronously.

Benchmark suggestion

Before/after with benchstat:

go test -bench=BenchmarkLimiter -benchmem -count=6 ./middleware/limiter/ | tee /tmp/limiter-before.txt
# apply fix
go test -bench=BenchmarkLimiter -benchmem -count=6 ./middleware/limiter/ | tee /tmp/limiter-after.txt
benchstat /tmp/limiter-before.txt /tmp/limiter-after.txt

Priority

P0 — This is the single highest-impact performance issue in the framework. It affects every deployment using external storage with these middleware.


Identified during a full performance architecture review of the Fiber codebase.

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions