当前位置:网站首页>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 complexity
byO(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 layer
introduce redis Advanced data typesThe bloon filter
Made 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 filter
The 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 penetration
problem , When a user sends a short link request , First, the system willanalysis
Out 6 position code, Then go firstThe bloon filter
lookup , IfThere is
Go againcache
obtain code CorrespondingLong link
, Cache does not exist, go againdatabase
lookup ; Ifnon-existent
Words , 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 link
Pass in the backgroundRequest forwarding
Forward 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)301permanent
Redirect
2)302temporary
Redirect
Because we need to do Data statistics , That is, recordsShort link
OfTraffic volume
, So we need to use302 agreement
.
Two 、 The system design
2.1 Database design
The design of this system is a little
Simple
Short 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
user
You can create multiple groups- One
Group
There can be multiple short link messages- One
Short link
There 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 in
Users can create new short linksgrouping
, Every successful user registration will have aDefault grouping
AndAt least one group
.
️4. Short chain access statistics
Used to record
Each short link
Daily 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 method
appearMultithreading problem
, After the long chain passes through the short chain generator, a6 Number of codes
, Then go toThe bloon filter
To determine whether it already exists , If it already exists, it needs toRecall generator
Generate , The next step is to take this 6 Save bit code todatabase
andThe 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
, Systemanalysis
Give the last6 Bit code
, Then use this code toThe bloon filter
Query exists , IfThere is
Just go tocache
Get long chain , If cachingnon-existent
Go again if you likedatabase
Inquire about , And parse the queried short chainRefresh to cache
, Next forward by request , Will getLong link
adopt302 agreement
forwarded ; If the bloom filter does not exist6 Bit code
It'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
边栏推荐
- Exploring the equipment and teaching of robot education
- 进程间通信 -- 消息队列
- 小程序 支付
- Three web components (servlet, filter, listener)
- Advanced file IO of system programming (13) -- IO multiplexing - Select
- ES6学习笔记二
- Write console script by laravel
- Learn go language 0x05: the exercise code of map in go language journey
- Applet payment
- Share two practical shell scripts
猜你喜欢
MQ在laravel中简单使用
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
Study notes of C [8] SQL [1]
少儿编程结构的改变之路
初探 Lambda Powertools TypeScript
Exploring the equipment and teaching of robot education
How to count fixed assets and how to generate an asset count report with one click
Laravel绑定钉钉群警报(php)
Interpreting the art created by robots
探究机器人教育的器材与教学
随机推荐
配电房远程综合监控系统在10kV预制舱项目中的应用
用curl库压缩成发送字符串为utf8并用curl库发送
MQ的了解
Compress the curl library into a sending string of utf8 and send it with curl library
qt 64位静态版本显示gif
初探 Lambda Powertools TypeScript
AcWing 1874. 哞加密(枚举,哈希)
赛微微电科创板上市破发:跌幅达26% 公司市值44亿
怎么进行固定资产盘点,资产盘点报告如何一键生成
TclError: no display name and no $DISPLAY environment variable
少儿编程结构的改变之路
GPU, CUDA,cuDNN三者的关系总结
QT信号量 无法解析的错误的总结
Cognition and R & D technology of micro robot
微型机器人的认知和研发技术
oh-my-lotto
docker MySQL主从备份
Learning go language 0x02: understanding slice
Simple construction of rebbitmq
Nacos Foundation (6): Nacos configuration management model