Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript makes Snowflake algorithm become Air

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces how JavaScript makes the snowflake algorithm into the air, the content is very detailed, interested friends can refer to, hope to be helpful to you.

I can only make such shameless remarks here. Because the repair of xjjdog is mainly reflected in the back end, so I love my dog and my dog. This shows that struggle is a basic attribute of human beings: programmers are not monolithic in addition to being product managers and project managers.

1. Where there is smoke, there is fire

As the title says, this will be related to the snowflake algorithm.

We have a system that uses a MySQL database, so we use self-increasing ID in the primary key selection of the database.

ID INT PRIMARY KEY AUTO_INCREMENT

This kind of ID is simple and smooth, but it has a series of disadvantages, but it is enough to be used in general systems.

Before going online, the project team invited the best architect in the company to conduct a centralized physical examination of the project. One of the important steps is for ID generators.

"Don't you know that today's development systems should at least use Snowflake as the ID generator?" The architect is very dissatisfied with the proposal of self-adding ID.

It points out that even if you use UUID, it is better than self-increasing ID in scenarios such as system expansion, sub-database, sub-table, data migration, and so on.

When the big guys discussed it, they thought it was very reasonable. UUID is too disordered, and Meituan Leaf is too complex, so you might as well directly use the old Snowflake and generate the simplest ID directly.

Something like this.

527574217068392807 527574217068392808

To give you an intuitive understanding, let's take a look at the maximum value of Long in Java.

9223372036854775807

Take another look at the maximum value of Int.

2147483647

You can see that the generated Snowflake ID is larger than Int and smaller than Long (compared to the maximum), so it's better to use bigint storage in the database.

Just do it. As soon as the batch script is changed, the primary key becomes bigger and longer.

two。 Problems occur

Don't say, this kind of ID looks more pleasing to the eye. ID in URL transmission, in formdata transmission, a look at the comparison of professional!

/ edit.do?id=527574217068392810

After the system was modified as recommended, the unit test was smooth. The black box test was hastily clicked and passed.

The psychic event was discovered by the client.

Customers say that many records cannot be edited or deleted. Prompt no record found.

You also know the urination of many companies, communicate with customers, and usually don't know much about technology. When you take a picture with Niu x's mobile phone to the customer's screen, the original picture is sent with more than a dozen MB. But the weird thing is that the picture is big, but the content is vague.

The back-end programmer squints to open the picture, dig out the ID displayed inside, and check it in the system.

There is no record of this.

There must be something wrong with the squinting posture. The back-end programmer has to record it again. Unfortunately, there is still no record of this.

I have no choice but to make a copy of the customer's database. When you click on the page, there is a problem!

The data returned in the browser response is actually different from that in preview.

3. Problem verification

In other words, a good number: 527183991665594368, translated by the browser, becomes 527183991665594400.

Let's debug it in the devtools of the browser.

For further verification, let's try it all from typescript to js.

# cat test.ts let a = 527183991665594368; console.log (a); # tsc test.ts # cat test.js var a = 527183991665594368; console.log (a); # node test.js 527183991665594400

As you can see, this problem exists in the whole ecology of js, which really spoils the back end.

4. Why?

That's because. In JavaScript, there are two kinds of numbers. Number and BigInt. The most commonly used one is number.

The largest Number, called Number.MAX_SAFE_INTEGER, has a value of:

2 ^ 53-1 or

+ /-9007199254740991

As we all know, Long in Java is 64-bit. The secure Integer in Js does not reach the length defined in Java at all.

This is the evil IEEE_754 specification, which causes a loss of precision when the length of the Long is greater than 17 bits.

In the latest TypeScript3.2, but directly using the type of BigInt for coding, or the pain of using the encapsulation of long.js, but it is still too troublesome, requires too much coding, and may also be missed.

It is not very reliable to use digital types to transmit data. When you turn around, things will change.

The best way is to use string for delivery. Even if the length of the background ID becomes 128bit in the future, you are not afraid of this conversion.

In Java, if you are using jackson, you can change the string directly through annotations without changing the database.

@ JsonSerialize (using=ToStringSerializer.class) private Long id

This problem is obviously not a back-end pot. The back end transmits the correct data to the front end, and whether it can be handled correctly or not has nothing to do with the back end at all. This non-standard handling of JS in accordance with the norms has made a lot of people step into the pit. Whether it is cute new or old birds, still fall into the pit one after another, it has to be said that this feature is very anti-human.

About JavaScript how to make snowflake algorithm into the air to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report