当前位置:网站首页>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
边栏推荐
- Learn go language 0x07: stringer exercise code in go language journey
- Pytorch neural network trainer
- 采用百度飞桨EasyDL完成指定目标识别
- mysql插入datetime类型字段不加单引号插入不成功
- Detailed explanation of MySQL creation stored procedure and function
- TclError: no display name and no $DISPLAY environment variable
- Nacos Basics (5): getting started with Nacos configuration
- MQ在laravel中简单使用
- Laravel always returns JSON response
- Golang's pen test questions & interview questions 01
猜你喜欢

Structure of C language (Advanced)

得物技术网络优化-CDN资源请求优化实践

Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition

解析幼儿教育中steam教育的融合

RebbitMQ的初步了解

GPU, CUDA,cuDNN三者的关系总结

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

Interpretation of biological recognition in robot programming course

解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)

rebbitMQ的简单搭建
随机推荐
Implementation of inserting millions of data into MySQL database in 10 seconds
解读机器人编程课程的生物认知度
云呐|如何管理好公司的固定资产,固定资产管理怎么做
系统编程之高级文件IO(十三)——IO多路复用-select
讯飞2021年营收183亿:同比增41% 净利为15.56亿
Redis optimization series (II) redis master-slave principle and master-slave common configuration
得物技术网络优化-CDN资源请求优化实践
MQ的了解
让中小学生在快乐中学习的创客教育
小程序 支付
Promise details
Write console script by laravel
Interpretation of biological recognition in robot programming course
map<QString, bool> 的使用记录
qt 64位静态版本显示gif
探究机器人教育的器材与教学
How does QT turn qwigdet into qdialog
ES6学习笔记二
MQ is easy to use in laravel
QT信号量 无法解析的错误的总结