Three kinds of Redisnew
type
The three new types are not just new additions to Redis6, but are different from the more common types
Why do we have these type 1s
In many scenarios, the basic types of mysql or Redis can be solved, but waste a lot of useless resources
What would you do in such a scenario?
Mysql implementation
If you have not used redis or do not often use the first thought should be mysql, of course, I am no exception 😄. Each time we check in, we need to store the following data structure
This is how much data a user needs to store for a single check-in day (with more fields, of course)
First of all, we just want to know if we checked in on a particular day and we can use 0,1
Second, we just need to know how many 1’s there are, or how many consecutive 1’s there are
Redis is commonly used
- We can do this using the append of string
- We can do this using a list
Of course you can implement set, hash, zset, but I’m not going to give you any examples here so the question is, is this the best way to do it? Doesn’t the zeros and ones for data make you think of something?
Bit is the smallest unit of data that can be stored as 0 or 1. The string ‘1’ we store corresponds to 49 ASCII bits 0011 0001. We can find that we can store only one data when we could store 8 data, which leads to memory waste, so we can use the Bitmap provided by Redis to solve the problem
Why are there four common statistics for these types of 200-million level systems
- Aggregate statistics –
Intersection and equal set statistics - set
- Sorting statistics –
- List,zset list will have problems
- Binary statistics –
Sign in - bitmap
- Cardinal statistics –
Number of non-repeating elements - Hyperloglog
List sorting statistics, such as the display of the latest number of data, a large number of requests may occur in the pagination of duplicate data, so zset is better
Bitmap bitmap
A statistical binary state data type implemented using String as the underlying data structure
Bitmaps are essentially arrays, which are bitwise operations based on String data types. The array consists of multiple bits, each of which corresponds to an offset (we can call it an index or bit). The maximum number of bits supported by Bitmap is 2^32 bits, which can greatly save storage space. Up to 4.29 billion bytes of information can be stored using 512MB of memory (2^32 = 4294967296)
Common commands
SETBIT key offset Value Sets the value of the offset index (0 or 1 only) GETBIT key offset Obtains the value of the offset index BITCOUNT key [Start end] How many 1 bitOps exist in the range The two key AND OR operations AND, OR, XOR, NOT BITPOS find the index of the first 0 OR 1
Let’s verify that the bitmap is a string at the bottom and is a group of 8 bits. The bm1 we store only stores the first bit. Let’s look at get bm1 and try it. The get command is a string. If you open our redis visual client, you can see that we have saved 8 bits, or 1byte of data
Application scenarios
- Sign in the scene
- Day live statistics
- Keep punching in
- .
Hyperloglog
Hyperloglog can be used on websites or other products where UV(one-day unique visits),PV(page views),DAU(daily active),MAU(monthly active) and other data are available
It just does non-repeating cardinality statistics, not sets and does not keep data, just numbers rather than details.
Hyperloglog trades space for accuracy and takes up only 12KB at most. (More on that later)
Common commands
PFADD key element …. PGMERGE new_key key1 key2… PFCOUNT key Indicates the estimated value of the statistics key. Merge multiple keys into the new key
Application scenarios
Product UV(one-day unique visits),PV(page views),DAU(daily active) and other statistics
GEO
The geolocation processing layer applies Zset to GeoHash
Common commands
GEOPOS returns the positions (longitude and latitude) of all elements in a given position from within the key. GEODIST returns the distance between two given positions. Centered on a given latitude and longitude, GEORADIUS returns all positional elements within a given maximum distance from the center. GEORADIUSBYMEMBER Similar to GEORADIUS, GEOHASH returns a GEOHASH representation of one or more positional elements
Application scenarios
- taxi-hailing app
- Shared bikes nearby
- Meituan nearby businesses
- .
conclusion
Ok, these are the three types of extension, there are a lot of underlying not explained, will be explained in detail in the source code.