Secure Login System with Temporary Account Lock Using Redis
Stop brute-force attacks with a scalable login throttle using Redis counters and TTL-based account locks — built for IT consulting and payment gateway platforms.
In modern IT consulting and payment gateway platforms, securing client accounts against unauthorized access is critical. One of the most common attack vectors is brute-force login attempts, where an attacker repeatedly tries different passwords until one succeeds.
A robust, low-latency solution to mitigate this risk is to limit login attempts and temporarily lock accounts after multiple failed tries — protecting both client credentials and sensitive account balance data.
This post explains how to design a clean, scalable implementation using Redis for counters and TTL-based locks.
🔒 Why Limit Login Attempts?
Allowing unlimited login attempts exposes clients and their account balances to several risks:
- Brute-force attacks: Automated bots try thousands of password combinations to gain unauthorized access.
- Credential stuffing: Attackers reuse leaked passwords from other platforms to access client portals.
- Account compromise: Weak or predictable passwords make client accounts and payment data easier to breach.
By setting limits and temporary blocks, you can:
- Reduce the chance of unauthorized access to client accounts and account balances.
- Alert clients of suspicious activity on their accounts.
- Give the system time to throttle repeated attempts before a breach occurs.
🧠 The Core Concept
We’ll follow three simple principles:
- Track login attempts – Every failed login increments a counter per client.
- Temporary block – When failures exceed a threshold (e.g., 3), block the client account for a defined time (e.g., 15 minutes).
- Reset on success – Successful logins clear counters and unlock the client.
Why Redis?
- ⚡ Blazing fast (millions of ops/sec) — ideal for high-throughput payment gateway environments
- ⏱ TTL support for auto-expiring counters and blocks
- 🧩 Centralized and scalable for distributed IT consulting and payment infrastructure
⚙️ How It Works (Step-by-Step)
1️⃣ Track Failed Attempts
Each failed login increments a Redis counter for the client:
login:fail:<clientId> → integer
Set a 15-minute TTL so old failures auto-expire.
INCR login:fail:<clientId>
EXPIRE login:fail:<clientId> 900 # 15 minutes
2️⃣ Block After Threshold
If the counter exceeds the limit (e.g., 3), add a temporary block key:
login:block:<clientId> → "1" with 15-minute TTL
This prevents further login attempts until the lock expires, safeguarding client account balances during the lockout period.
SET login:block:<clientId> 1 EX 900 NX
3️⃣ Reset After Success
On successful login, clear both Redis keys:
DEL login:fail:<clientId> login:block:<clientId>
💻 Example Implementation (Node.js + Redis)
Here’s a practical snippet using Express and ioredis:
// src/auth/login.ts
import type { Request, Response } from "express";
import { Redis } from "ioredis";
const redis = new Redis(process.env.REDIS_URL ?? "redis://127.0.0.1:6379");
const FAIL_KEY = (clientId: string) => `login:fail:${clientId}`;
const BLOCK_KEY = (clientId: string) => `login:block:${clientId}`;
const FAIL_TTL_SECONDS = 15 * 60;
const BLOCK_TTL_SECONDS = 15 * 60;
const MAX_ATTEMPTS = 3;
export async function loginHandler(req: Request, res: Response) {
const { identifier, password } = req.body;
const client = await findClient(identifier);
const clientId = client?.id ?? `ghost:${identifier}`;
const isBlocked = await redis.exists(BLOCK_KEY(clientId));
if (isBlocked) {
return res.status(429).json({ message: "Invalid credentials." });
}
const isValid = client ? await verifyPassword(password, client.passwordHash) : false;
if (!isValid) {
const fails = await redis.incr(FAIL_KEY(clientId));
await redis.expire(FAIL_KEY(clientId), FAIL_TTL_SECONDS);
if (fails >= MAX_ATTEMPTS) {
await redis.set(BLOCK_KEY(clientId), "1", "EX", BLOCK_TTL_SECONDS);
}
return res.status(401).json({ message: "Invalid credentials." });
}
await redis.del(FAIL_KEY(clientId), BLOCK_KEY(clientId));
const token = await createSession(client.id);
return res.json({ token });
}
async function findClient(identifier: string) { return null as any; }
async function verifyPassword(plain: string, hash: string) { return false; }
async function createSession(clientId: string) { return "token"; }
🔐 Security Best Practices
- Use Generic Error Messages → Always respond with neutral messages (“Invalid credentials”) to avoid revealing account existence
- Hash Passwords Properly → Use bcrypt or Argon2 to protect client credentials
- Add IP-Based Rate Limiting → Prevent distributed brute-force attacks targeting client portals
- Implement Exponential Backoff → Increase block time with repeated failures to deter persistent attackers
- Client Alerts → Send email or Telegram notification when a client account is locked
- Comprehensive Logging → Track login anomalies for forensic analysis and compliance in payment gateway environments
⚡ Benefits of Using Redis in Payment Gateway Infrastructure
Redis makes implementing secure throttling effortless across IT consulting and payment gateway systems:
- 🚀 Atomic counters ensure precise increments even under high transaction loads
- 🕒 TTL expiration automatically cleans up stale client login attempts
- 🧠 Centralized data layer supports horizontal scaling across distributed payment nodes
- 🔁 Session storage for client sessions can also be handled within the same Redis instance
🧭 Final Thoughts
With minimal overhead, you can build a resilient defense layer for your IT consulting platform or payment gateway against brute-force and credential-stuffing attacks on client accounts.
When combined with strong passwords, 2FA, and IP-based throttling, this approach provides both security and speed — protecting client account balances and transaction integrity without hurting the user experience.
Stay Secure. Build Smart.
— Protize Engineering
Related Articles
Continue reading about Security
Authentication Done Right: JWT, Refresh Tokens, and NestJS Guards
Build a complete, production-grade authentication system with short-lived JWT access tokens, secure refresh tokens, and NestJS Guards that protect your routes automatically.
Integrating AWS WAF Firewall - Smart Whitelisting for Clients and Users
Enhancing platform security with AWS WAF integration for dynamic IP whitelisting of clients and users through the dashboard.