HyperLogLog Introduction

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Introduction

HyperLogLog is a method for estimating the number of elements in a set and was added in Redis version 2.8.9, released on April 22, 2014.   This document was written based on version 4.0.9.
  • Use: It is mainly used to get an approximation with an error of 1% or less for very large data. For example, you can use it to count a search engine's daily queries. It uses very little memory and the error is small.
  • Memory usage: The biggest advantage is that it uses very little memory.   If you use a Java HashSet or a Redis Set, memory usage grows with the number of elements. For example, storing 1 million numbers in a Redis Set uses 4,848kb, and storing 10 million numbers uses 46,387kb, but Redis HyperLogLog uses a fixed 12kb regardless of the number of elements. Compared with a Set of 1 million numbers, that is only 0.25% of the memory.
    Redis HyperLogLog uses 16384 registers and 6 bits per register, so the memory used is 12kb.   Calculation: 16,384 * 6bits = 98,304bits / 8 = 12,288bytes
  • Cardinality estimation error: The HyperLogLog error is 1.04/SQRT(m). m is the number of registers; Redis uses 16384, so the error is 0.81%.
  • HyperLogLog is an algorithm published by Philippe Flajolet in 2007. The name HyperLogLog comes from extending the previous algorithm, Loglog Counting, so Hyper was added.
  • Salvatore used the PF prefix for HyperLogLog related commands to honor Philippe Flajolet.
  • Add speed: To compare insert speed between HyperLogLog and Set, 1 million items were added with PFADD and SADD. PFADD took 5.23 seconds and SADD took 8.64 seconds, so PFADD was about 1.6 times faster.
  • Query speed: Both PFCOUNT and SCARD are O(1). When checked with Slowlog, both took 4 microseconds.
  • HyperLogLog data does not use a separate data structure; it uses String. Inside the String is the HyperLogLog data structure.
  • References
  • Test Data

HyperLogLog command list

CommandsVersionSyntaxDescription
PFADD2.8.9key ele [ele ...]Add an element
PFCOUNT2.8.9key [key ...]Get the number of elements
PFMERGE2.8.9destkey sourcekey [sourcekey ...]Merge sets

Total : 3


<< SCRIPT DEBUG HyperLogLog PFADD >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.