Backend Engineer Guide: High-Throughput, Brute-Force Resistant OTP Verification in Go & Redis

One-Time Password (OTP) verification endpoints are prime targets for SMS pumping fraud (toll fraud) and credential brute-forcing. Storing plaintext 6-digit OTPs in PostgreSQL or verifying them with non-atomic Redis GET followed by INCR allows an attacker firing 50 parallel requests to bypass a 5-attempt lockout before the counter increments. As a backend engineer, I architect high-throughput OTP engines in Go using HMAC-SHA256 digests and atomic Redis Lua verification scripts.

1. Atomic Attempt Counting & Constant-Time Verification via Redis Lua

We never store the raw 6-digit code in Redis; we store its keyed HMAC-SHA256 digest. When a user submits a code, a single atomic Redis Lua script checks the attempt counter, increments attempts before comparison, and deletes the key immediately upon match or lockout:

// auth/otp_verifier.go
package auth

import (
	"context"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"errors"

	"github.com/redis/go-redis/v9"
)

var verifyOTPLua = redis.NewScript(`
local hash_key = KEYS[1]
local attempts_key = KEYS[2]
local candidate_hmac = ARGV[1]
local max_attempts = tonumber(ARGV[2])

local expected = redis.call("GET", hash_key)
if not expected then
    return -1 -- Expired or non-existent
end

local attempts = redis.call("INCR", attempts_key)
if attempts > max_attempts then
    redis.call("DEL", hash_key, attempts_key)
    return -2 -- Locked out; OTP burned
end

if expected == candidate_hmac then
    redis.call("DEL", hash_key, attempts_key)
    return 1 -- Verified and consumed once
end
return 0 -- Mismatch
`)

func ComputeOTPMAC(secret []byte, phoneE164, code string) string {
	mac := hmac.New(sha256.New, secret)
	mac.Write([]byte(phoneE164 + ":" + code))
	return hex.EncodeToString(mac.Sum(nil))
}

func VerifyOTP(ctx context.Context, rdb *redis.Client, secret []byte, phoneE164, code string) (bool, error) {
	candidate := ComputeOTPMAC(secret, phoneE164, code)
	res, err := verifyOTPLua.Run(
		ctx,
		rdb,
		[]string{"otp:mac:" + phoneE164, "otp:tries:" + phoneE164},
		candidate,
		5,
	).Int()
	if err != nil {
		return false, err
	}
	if res == -2 {
		return false, errors.New("maximum verification attempts exceeded; request a new code")
	}
	return res == 1, nil
}

2. Mitigating SMS Pumping Fraud by ASN & Country Prefix Budgets

Before dispatching an outbound SMS or WhatsApp OTP, enforce dual sliding-window rate limits: per phone number (max 3 per 15m), per client IP/ASN, and per telecom country prefix so an automated botnet cannot drain your messaging balance on high-cost international prefixes.

3. Related Go Security & Backend Engineering Resources


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs