Auto Increment in Redis

355 views
Skip to first unread message

Madhusudhana

unread,
Aug 25, 2023, 9:55:27 AM8/25/23
to Redis DB
Hello Redis Experts,

I have one requirement in my app, where users are hitting on one URL, that URL count has to increase automatically, I can use MongoDB but it will be a burden to DB.
So I decided to use Redis but in Redis, there is no auto-increment on one field.
Any better approach would be nice to share 



Thanks
Madhusudhan Reddy

Greg Andrews

unread,
Aug 27, 2023, 2:46:18 PM8/27/23
to Redis DB
I'm not sure what you mean by "auto-increment". Are you envisioning a field in a hash key that Redis increments when some other command is received, such as reading or writing the contents of the key?  Something else?

David Maier (Redis)

unread,
Aug 28, 2023, 6:08:24 AM8/28/23
to redi...@googlegroups.com
Hi Madhusudhana,

The easiest is to just use the URL as the key and let the application perform an INCR command on that key whenever the URL is visited. You might also want to take a look at the HyperLogLog commands in order to track the unique visitors in a space-efficient way.

BTW: In relational databases auto-incrementing columns are usually associated to events that change data (e.g., inserting). You can implement something similar by using keyspace notifications or triggers and functions with Redis.

Regards,
David

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/8af133c2-cfc1-49f0-9011-1ce3b9b7a2cen%40googlegroups.com.


Disclaimer

The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.

Bill Schoonmaker

unread,
Sep 6, 2023, 2:19:32 PM9/6/23
to Redis DB
INCR has been in there since version 1. Couple this with functions and triggers in 7.2 and you should be able to implement a nice auto increment pretty handily. Good luck!

127.0.0.1:6379> set counter 0
OK
127.0.0.1:6379> incr counter
(integer) 1
127.0.0.1:6379> get counter
"1"
127.0.0.1:6379> incr counter
(integer) 2
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379>
Reply all
Reply to author
Forward
0 new messages