Failed logins / time series with auto expiry

32 views
Skip to first unread message

Simon

unread,
Jun 27, 2021, 10:29:13 AM6/27/21
to Redis DB
Hi, I'm new to Redis and struggling to work out the best approach to implement this requirement:

On our website, if there is an invalid login attempt (on any user name), we want to log that attempt in redis, along with the username, timestamp of the attempt.

When a certain threshold is hit (e.g. 50 failed attempts in any 5 minute rolling period), then our system should refuse any more attempts until the 5 minutes has elapsed.

So, we sort of need something like this:

user.1 -> timestamp1|timestamp2|timestamp3
user.2 -> timestamp4|timestamp5
user.3 -> timestamp6|timestamp7|timestamp8

When each timestamp is over 5 minutes, it should be expelled from the user's list, so that we can then count all timestamps less than 5 minutes for a given user.

What would be the best approach in REDIS for this? I'm not sure whether to use sorted sets, sets, lists, hashes, etc.

Thanks guys.

abiji...@gmail.com

unread,
Jun 27, 2021, 8:50:37 PM6/27/21
to Redis DB
Hi Simon,

I can think of two approaches to solve this,
  1. List
  2. Sorted set.
List
Key : user-id
Values: timestamp
Store the timestamps into the list Redis will reply with the size of the list.
When size hits the threshold you have configured manually check for validity of timestamps. Truncate unwanted entries using ltrim.
Additionally you can also set expiry to the list key. There might be cases where frequent attempts occur between the provided window which will be handled in you server code.

Sorted Set
Key: user-id
values: timestamp, rank 0

Store all the timestamps with rank zero. You can use zcard to check for size or write a Lua script that stores and returns zcard size for each add.
Since all ranks are zero you can try using Redis lexical support for sorted set.
You can set expiry to this key also.


Regards,
Abijith Krishna

Simon

unread,
Jul 2, 2021, 1:25:42 PM7/2/21
to Redis DB
Thank you, that's very helpful. 
Reply all
Reply to author
Forward
0 new messages