Fixed Window Counter

How it works

key  = "rl:{user_id}:{window_start_unix}"
INCR key
EXPIRE key {window_size_seconds}

A "bucket" is just a time slot. You chop the clock into equal chunks and each chunk gets its own Redis key. When the clock moves to the next chunk you start a fresh counter from zero; the old key expires automatically via TTL.

Time:    0s──────60s──────120s──────180s
         [bucket 0] [bucket 1] [bucket 2]

Request at T=37s  → key "rl:fw:user_42:0"   → INCR → count = 1
Request at T=58s  → key "rl:fw:user_42:0"   → INCR → count = 2
Request at T=61s  → key "rl:fw:user_42:60"  → INCR → count = 1  ← new key

The number in the key is the Unix timestamp of that window's start — the key name itself encodes which bucket you're in. No cleanup code needed.

type FixedWindow struct {
    rdb    *redis.Client
    limit  int64
    window time.Duration
}

// Allow returns (allowed, remaining, err).
func (fw *FixedWindow) Allow(ctx context.Context, userID string) (bool, int64, error) {
    // Bucket key changes every window. No manual deletes — TTL handles it.
    bucket := time.Now().Truncate(fw.window).Unix()
    key := fmt.Sprintf("rl:fw:%s:%d", userID, bucket)

    pipe := fw.rdb.Pipeline()
    incr := pipe.Incr(ctx, key)
    pipe.Expire(ctx, key, fw.window*2) // 2× so the key outlives its window
    if _, err := pipe.Exec(ctx); err != nil {
        return false, 0, err
    }

    count := incr.Val()
    remaining := fw.limit - count
    if remaining < 0 {
        remaining = 0
    }
    return count <= fw.limit, remaining, nil
}

Reading the two tricky lines:

  • time.Now().Truncate(fw.window) rounds the current time down to the start of the window. At 10:42:37 with a 60 s window that gives 10:42:00, so every request in the same minute lands on the same key and the first request of the next minute lands on a brand-new one.
  • pipe.Expire(ctx, key, fw.window*2) auto-deletes the key after twice the window. The 2× is a cheap safety margin (clock skew, slightly-late requests) so the key is guaranteed to outlive the window it represents.

The boundary burst problem

Window 1: 00:00 – 01:00       Window 2: 01:00 – 02:00
          │                             │
          └── 100 requests at 00:59     └── 100 requests at 01:01
  1. At 00:59 the user fires 100 requests. The minute-0 counter goes 0 → 100. All allowed — exactly at the limit.
  2. Two seconds later at 01:01 the clock crossed into a new minute. The key changed, so the counter starts fresh at 0.
  3. The user fires 100 more. All allowed again.

Both minutes individually obeyed "100 per minute." But 200 requests landed inside a 2-second span — double the intended rate. The limiter was looking at fixed calendar minutes; the abuser was looking at the seam between them. This isn't a bug in the code — it's baked into the idea of resetting on fixed boundaries.

Memory / userO(1) — one key
AccuracyApproximate — 2× burst at boundaries
Latency~1 RTT (pipelined INCR + EXPIRE)
Best forInternal services where burst tolerance is fine