mgalgs
mgalgs

Reputation: 16759

How to expire a HyperLogLog in Redis?

HyperLogLogs take up 12KB of space. I don't see anything in the docs about when that storage is freed up.

My current plan is to call EXPIRE every time I call PFADD, but I can't find much discussion about expiring HLLs, so I'm wondering if I'm doing it wrong...

I'm planning on using HLLs to count the number of active visitors on my site in real-time. I only want to keep the counts for the past hour around, freeing up anything older than that.

Upvotes: 0

Views: 1517

Answers (1)

for_stack
for_stack

Reputation: 22906

NO, you cannot expire items added to the HLL. Instead, EXPIRE command will expire the whole HLL.

In order to achieve your goal, you can create HLL for each hour, and expire the whole HLL after some time.

// for the 2019082200
PFADD user:2019082200 user1
// also set expiration for the HLL, and expire it after 10 hours
EXPIRE user:2019082200 36000

// add more users
PFADD user:2019082200 user2

// until the next hour, create a new HLL for the next hour
PFADD user:2019082201 user1
EXPIRE user:2019082201 36000

Upvotes: 5

Related Questions