当前位置:网站首页>Design and practice of the smallest short website system in the whole network
Design and practice of the smallest short website system in the whole network
2022-04-23 11:27:00 【BreezAm】
List of articles
Introduce
Today, we introduce the design and implementation of a short website system . The so-called short link is that no matter how long your link is , Eventually it will generate a Fixed length short links . Although the obligation is simple , But it involves a lot of details . Ensure short links only and Access speed Become a core issue , Then start performing .
Short link application scenario :
- Marketing : Text messaging 、 Mobile link forwarding ( Bili, Bili )
- Article content layout
- Enterprises send interview questions
- Voting and questionnaires
One 、 Related concepts and technologies
1.1 Redis cache
In this system , We will use redis data type
Hash, Used to store long links we want to access , We all know ,Hash Query forTime complexitybyO(1), Therefore, it is used to preserve code And a long link KV Correspondence can improve the efficiency of short chain to long linkForwarding speed(302 agreement ), The storage model is as follows :

1.2 Generate 6 Bit random code algorithm
The core of short URL implementation is to ensure the generation of 6 position code Must be unique , Using this algorithm can not completely guarantee the uniqueness , So I designed it in
Obligation layerintroduce redis Advanced data typesThe bloon filterMade an optimization , To ensure that the generated 6 Bit code is unique .
public class ShortUrlCodeUtil {
private static final String randomStr = "hnmnd0d";
public static String genShortCode(String url, String randomStr) {
return shortUrl(url, randomStr)[0];
}
public static String genShortCode(String url) {
return shortUrl(url, randomStr)[0];
}
private static String[] shortUrl(String url, String randomStr) {
String[] chars = new String[]{
"a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"
};
String encrypt = (MD5.encrypt(randomStr + url));
String hex = encrypt;
String[] resUrl = new String[4];
for (int i = 0; i < 4; i++) {
String t = hex.substring(i * 8, i * 8 + 8);
long lHex = 0x3FFFFFFF & Long.parseLong(t, 16);
String charTemp = "";
for (int j = 0; j < 6; j++) {
long index = 0x0000003D & lHex;
charTemp += chars[(int) index];
lHex = lHex >> 5;
}
resUrl[i] = charTemp;
}
return resUrl;
}
}
1.3 The bloon filter
Think about the problem :
- Why use a bloom filter , Don't use set aggregate ?
- What are the obligations of Bloom filter ?
- The bottom principle of Bloom filter ?
- This system uses
The bloon filterThe purpose of is to generate6 position code duplicate removal, Prevent duplicate codes , Although the probability of repetition is very small , But we should still prevent , So I made aOptimize, As for the same weight removal , Why not set, For reasons of length , Don't expand .- secondly , Bloom filter has another function in this system , That's the solution
Cache hit penetrationproblem , When a user sends a short link request , First, the system willanalysisOut 6 position code, Then go firstThe bloon filterlookup , IfThere isGo againcacheobtain code CorrespondingLong link, Cache does not exist, go againdatabaselookup ; Ifnon-existentWords , Throw an exception directly , No more database and cache queries .

@Service
public class ShortURLService {
/** * Estimated number of values to put */
private Integer size=10_0000;
/** * Expected miscalculation rate */
private Double error=0.01;
/** * Instantiate the bloom filter */
private BloomFilter<String> shortUrlCodeFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), size, error);
/** * Put the short URL code * * @param code */
public void put(String code) {
shortUrlCodeFilter.put(code);
}
/** * Determine whether the bloom filter contains this element * * @param code Short URL code * @return */
public boolean contain(String code) {
return shortUrlCodeFilter.mightContain(code);
}
}
1.4 302 and 301
Because the system needs to
Long linkPass in the backgroundRequest forwardingForward it by , So it usesRequest forwarding protocol. There are two types of request forwarding , One is301, The other is302, What's the difference between them ?
1)301permanentRedirect
2)302temporaryRedirect
Because we need to do Data statistics , That is, recordsShort linkOfTraffic volume, So we need to use302 agreement.
Two 、 The system design
2.1 Database design
The design of this system is a little
SimpleShort chain generation system , So the design of the table is not complicated , The obligation is simple , The main table isShort link information table, Used to store generated short links .
2.1.1 ER chart
Just 4 A watch : User table 、 Statistical table 、 Short chain grouping table 、 Short link information table .
- One
userYou can create multiple groups- One
GroupThere can be multiple short link messages- One
Short linkThere can be multiple statistics ( Daily visits )

2.1.1 Table design
️1. User table
Used to save the registration
User information

️2. Short chain information table
Used to record the generated
Short link information.

️3. Short chain grouping table
Sign inUsers can create new short linksgrouping, Every successful user registration will have aDefault groupingAndAt least one group.

️4. Short chain access statistics
Used to record
Each short linkDaily usersTraffic volume

3、 ... and 、 Detailed design of the system
3.1 Short chain generation
3.1.1 principle
The process of short link generation is probably , The user enters a
Long link, Request short chain generation method , First, the method needs to beLock, Prevent multiple threads, multiple usersAt the same time, request the methodappearMultithreading problem, After the long chain passes through the short chain generator, a6 Number of codes, Then go toThe bloon filterTo determine whether it already exists , If it already exists, it needs toRecall generatorGenerate , The next step is to take this 6 Save bit code todatabaseandThe bloon filter.
3.1.2 flow chart

3.2 Short chain visit website
3.2.1 principle
The short chain access process is roughly : The user enters a
Short link, SystemanalysisGive the last6 Bit code, Then use this code toThe bloon filterQuery exists , IfThere isJust go tocacheGet long chain , If cachingnon-existentGo again if you likedatabaseInquire about , And parse the queried short chainRefresh to cache, Next forward by request , Will getLong linkadopt302 agreementforwarded ; If the bloom filter does not exist6 Bit codeIt's directThrow an exception.
3.2.2 flow chart

Four 、 Conclusion
The application scenarios of short links are very extensive , So when faced with such obligations , Designing a reliable short link becomes very important , Because this system is the first version designed and developed by myself , The design is also very simple , There may be some problems , Welcome to leave a message in the comment area to point out , You can also ask questions and suggestions for modification , thank !.
5、 ... and 、 Open source address
The source code of the system has been uploaded to the code cloud (Gitee), Welcome to read , If it helps , Please also order star️.
A commercial version is being developed …
| Back end | front end |
|---|---|
| https://gitee.com/BreezAm/TZ-SHORTURL | https://gitee.com/BreezAm/shorturl-site |
Online address :http://url.breez.work
版权声明
本文为[BreezAm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231124084779.html
边栏推荐
- 进程间通信 -- 消息队列
- nacos基础(8):登录管理
- 解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
- golang之笔试题&面试题01
- Nacos Foundation (8): login management
- PCB的注意事项
- PDMS软光刻加工过程
- Implementation of partition table of existing data table by MySQL
- Get things technology network optimization - CDN resource request Optimization Practice
- mysql插入datetime类型字段不加单引号插入不成功
猜你喜欢

C#的学习笔记【八】SQL【一】

On the integration of steam education in early childhood education

赛微微电科创板上市破发:跌幅达26% 公司市值44亿

讯飞2021年营收183亿:同比增41% 净利为15.56亿

Understanding of MQ

nacos基础(9):nacos配置管理之从单体架构到微服务

Interpreting the art created by robots

On lambda powertools typescript

实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题

GPU, CUDA,cuDNN三者的關系總結
随机推荐
The way to change children's programming structure
oh-my-lotto
Solve the problem of "suncertpathbuilderexception: unable to find valid certification path to requested target"
Implementation of inserting millions of data into MySQL database in 10 seconds
Oracle connectivity test gadget
qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
Summary of QT semaphore unresolved errors
PDMS soft lithography process
ImportError: libX11.so.6: cannot open shared object file: No such file or directory
AcWing 1874. Moo encryption (enumeration, hash)
On lambda powertools typescript
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
PyTorch 神经网络训练器
解析社交性机器人对基础科学的作用
laravel-admin表单验证
How does QT turn qwigdet into qdialog
Get things technology network optimization - CDN resource request Optimization Practice
Nacos Basics (5): getting started with Nacos configuration
Usage of rename in cygwin
解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)