What is a short link?
Short links are the conversion of regular links into shorter urls. For example, we received a common marketing SMS link hm.tb.cn/x.V8VgyA short link advantages: short, fewer characters, beautiful, easy to publish and spread.
Baidu short url: dwz.cn/
Google short url: goo.gl/
Realize the principle of
The basic principles can be roughly divided into the following five steps
- The user enters a short link to hm.tb.cn/x.V8VgyA
- DNS resolves the IP address of hm.tb.cn
- Resolve the IP address, send an HTTP GET request, and query the short code x.v8vgya
- The server queries the long URL through the short code
- Use the 301 redirect to jump to the corresponding long URL
Note: Here we need to distinguish the difference between 301 and 302 redirects, depending on the specific application scenario. HTTP response status code. 301 is a permanent redirect and 302 is a temporary redirect. 301 is a permanent redirect. 302 GET requests are not cached by default
Short link transform core algorithm
How to convert long links to short links, our general application scenarios such as SMS marketing, users through short links to jump to the corresponding marketing activities landing page, short link long link conversion is the core of the long chain short chain mapping relationship, and how to convert long links to short links.
There are two commonly used algorithms 1. Hash 2. Global unique ID algorithm
- 1. MurmurHash indicates the hash algorithm
There are many kinds of Hash algorithms, widely used and high performance MurmurHash algorithm, such as our common Redis consistent Hash algorithm is based on MurmurHash implementation.
MurmurHash provides two lengths of hash, 128bits and 32bits,
- 2. Globally unique ID
Global unique ID, in fact, we can think of many, such as mobile phone number, membership ID, etc., or code generation such as snowflake algorithm, MySQL self increasing ID.
After obtaining the unique short code, we need the short code to be as short as possible. We also need to convert the long id to a BASE62 character, which is two characters less than BASE64: ‘/’,’+’.
public class Base62Utils {
private static final char[] toBASE62 = {
'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'.'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'
};
public static String toBase62(long num) {
StringBuilder sb = new StringBuilder();
do {
int i = (int) (num % 62);
sb.append(toBASE62[i]);
num /= 62;
} while(num > 0);
returnsb.reverse().toString(); }}Copy the code
Data storage design
Data storage design, mainly depends on the specific business scenarios, such as data volume in billions of levels, MySQL is estimated to be unable to make. So how to select a NoSQL database? Common NoSQL database MongoDB, Hbase, ES, DynamoDB, Cassandra or Riak, etc. Specific business selection also needs to consider your company’s technology stack and operation and maintenance costs and so on.
How do I support high concurrency? How to prevent cache penetration and so on?
Reference:
www.cnblogs.com/rjzheng/p/1…
Time.geekbang.org/column/arti…
zhuanlan.zhihu.com/p/91947139