P-01 · Design doc

Session Risk Engine

A Go service that scores every login and session event as it happens and returns allow, challenge, or block within a strict latency budget.

Problem

Stolen passwords are cheap, and people reuse them across sites. Most fraud models score activity after the fact, which means the attacker is already inside the account by the time anyone notices. The decision has to happen in the login path itself, and it has to be fast enough that nobody is tempted to turn it off.

Goals and non-goals

Goals

  • Score every login and session event inline, under 20 ms at p99.
  • Explain every decision with machine-readable reasons.
  • Fall back to a safe default when Redis or the event stream is unavailable.

Non-goals

  • Replace multi-factor authentication or password hashing.
  • Train a machine-learning model before rule-based baselines are measured.

Threat model

Assume attackers hold valid credentials from other breaches and can rotate IP addresses and devices. The engine sees request metadata only, never passwords.

Threat model
ThreatActorProposed mitigationResidual risk
Credential stuffingBotnet with leaked password listsPer-account and per-IP failure velocity tracked in RedisLow-and-slow attacks spread over days
Account takeoverAttacker with a correct passwordNew-device and impossible-travel signals trigger a challengeAttacker on a familiar network and device
Engine outageDependency failure under loadCircuit breaker that fails to challenge, never to silent allowExtra friction for real users during an outage

Architecture

Proposed request flow
  1. Auth serviceSends a login event over gRPC
  2. Risk engineScores signals from live Redis state
  3. Decision + streamReturns a decision, publishes the event
Scoring is synchronous and bounded. History, analytics, and audit run off the Redpanda stream, so they never add latency to a login.

Alternatives considered

Offline machine-learning scoring

Good at finding patterns, but it decides after the attacker is already in.

Static rate limits

Cheap, but blind to a correct password used from a new country.

Inline rules with a latency budget

Explainable and fast. A model can be added later behind the same API.

Implementation

Simplified scoring rule. Signals are computed from Redis before this runs, and weights and thresholds come from config.

internal/score/score.goIllustrative
func Decide(s Signals, p Policy) (Decision, []string) {
    score, reasons := 0, []string{}
    if s.NewDevice {
        score += p.NewDeviceWeight
        reasons = append(reasons, "new_device")
    }
    if s.TravelKmPerHour > p.MaxTravelSpeed {
        score += p.ImpossibleTravelWeight
        reasons = append(reasons, "impossible_travel")
    }
    if s.FailedLogins10m >= p.FailureBurst {
        score += p.FailureBurstWeight
        reasons = append(reasons, "failure_burst")
    }

    switch {
    case score >= p.BlockAt:
        return Block, reasons
    case score >= p.ChallengeAt:
        return Challenge, reasons
    }
    return Allow, reasons
}

Postmortem

No prototype failure has been recorded yet. This is the first failure scenario to test.

Failure to test
Redis latency spikes push scoring past the 20 ms budget.
Next test
Inject Redis latency with Toxiproxy during a k6 run and verify the engine falls back to a challenge within budget.

Results

No measurements yet. These are the proposed metrics.

  • p99 scoring latency under loadNot yet measured
  • Detection rate on replayed takeover scenariosNot yet measured
  • False challenges on normal trafficNot yet measured