当前位置:网站首页>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
边栏推荐
- Interpretation of 2022 robot education industry analysis report
- QT 64 bit static version display gif
- Redis optimization series (II) redis master-slave principle and master-slave common configuration
- 云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
- redis优化系列(二)Redis主从原理、主从常用配置
- golang之笔试题&面试题01
- 年度最尴尬的社死瞬间,是Siri给的
- Pytorch neural network trainer
- Learn go language 0x07: stringer exercise code in go language journey
- C#的学习笔记【八】SQL【一】
猜你喜欢
Advanced file IO of system programming (13) -- IO multiplexing - Select
Résumé de la relation entre GPU, cuda et cudnn
讯飞2021年营收183亿:同比增41% 净利为15.56亿
Simple construction of rebbitmq
Interprocess communication -- message queue
Nacos Foundation (6): Nacos configuration management model
nacos基础(9):nacos配置管理之从单体架构到微服务
赛微微电科创板上市破发:跌幅达26% 公司市值44亿
解析社交性机器人对基础科学的作用
rebbitMQ的简单搭建
随机推荐
MySQL interview questions explain how to set hash index
Learning go language 0x08: practice using error in go language journey
云呐|如何管理好公司的固定资产,固定资产管理怎么做
Mysql中一千万条数据怎么快速查询
R-drop: a more powerful dropout regularization method
MySQL partition table can be classified by month
golang之筆試題&面試題01
Analyze the rules for the use of robots with good performance
解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
Maker education for primary and middle school students to learn in happiness
On lambda powertools typescript
docker MySQL主从备份
RebbitMQ的初步了解
nacos基础(8):登录管理
Understanding of fileprovider path configuration strategy
Interpretation of 2022 robot education industry analysis report
解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
AcWing 1874. Moo encryption (enumeration, hash)
How to quickly query 10 million pieces of data in MySQL
Interpretation of biological recognition in robot programming course