当前位置:网站首页>System design. Short chain system design
System design. Short chain system design
2022-07-31 00:59:00 【idle cat】
目录
直接记录 Long-chain and short-chain mapping table,直接查询
Millions of high-performance short-chain systems
What is a short chain
Shorter links

Why do you need short chains
Sending text messages should be shorter,美观,节省空间

The QR code generated by the short chain is less dense,更容易被识别.如图示:

The link is too long to send
Long link is hereQQWechat or Dingding is broken in the middle,很不友好
Short-chain principle
Visit the short chain Follow the long chain to respond to the request
Visit the short chain,Redirect to long chain
一般使用,重定向302
301 和 302 的区别
301永久重定向,That is, the browser only needs to request the long link for the first time,The next time you visit this short link, you will not request it from the short URL server,而是直接从浏览器的缓存里拿.This can improve the browser's access speed,但是也有一个问题,That is, if we want to count the visit data of the active link, there is no way to start.Or is this activity over and I want to remove the access entry,However, due to browser caching, some users may also access it.所以我们一般不使用301.
302 临时重定向,That is, every time the short link is accessed, the short URL server will be requested(除非响应中用 Cache-Control 或 Expired 暗示浏览器缓存),这样就便于 server 数据监控,所以虽然用 302 会给 server 增加一点压力,But the pros outweigh the cons.
短链系统
基本能力:
- 生成短链:According to the long chain 获取 短链,并记录
- Find the long chain:根据短链 映射 长链
- Whether the query has been generated:According to the long chain Check the short chain
- 超时过期:短链 Timeout cleanup mechanism
调用流程:

方案演进
直接记录 Long-chain and short-chain mapping table,直接查询

方案分析:
- 短链 生成 跟 长链 关系不大,Use a two-dimensional table to record the mapping relationship
- 长链,短链 是字符串,Creating an index is not cost-effective
- Concurrency has database query efficiency as a bottleneck
- Expiration time requires additional implementation
解决痛点:
- 路径1:存储映射关系,Combine short and long chains 做Hash计算,Then resume indexing.查询的时候,先根据HashCode查询,Then query based on text
- 路径2:存储映射关系,可以使用redis实现
- 路径3:Make short and long chains 有逻辑关系,Long chains can be directly reduced using short chains
存储映射关系,用HashCode优化查询效率

Get long chains based on short chains 或者 Get short chains based on long chains:使用类似HashMap的get算法,先根据HashCode查询,Then query according to the short chain text
逻辑:
- Find short scenes according to the length:
- 长链 计算 得到HashCode值
- 根据HashCode值查询,Then in the list of queries,再根据 文本查询
- Find long scenes based on short ones:
- 短链 计算 得到HashCode值
- 根据HashCode值查询,Then in the list of queries,再根据 文本查询
分析:
- Database performance bottlenecks still exist
- Expiration time requires additional logical guarantees
解决:使用rediscan solve these two problems
扩展:当然也可以使用MD5替代HashCode,也可以.
Redis替代DB存储映射关系
No matter what you useHash算法,may conflict.
For this conflict needs to be resolved,一个CodeCorresponding to multi-text scenarios.
Find the short-chain scenario based on the long-chain:
- 长链 计算Hash值
- 根据Hash值获取value,value应该是一个列表,And need to store long and short chains
- 数据结构:
{ “HashCode”:[ {唯一ID,LongUrl,ShortUrl}, {唯一ID,LongUrl,ShortUrl} ], } |
- 1:M场景 The timeout mechanism is implemented slightly with logic
- 唯一ID作为key设置expire过期时间
- 查询到“唯一ID”Need to determine whether it has expired
The advantage is high query performance,Can resist,And comes with an expiration mechanism
The disadvantage is that the maintenance of the structural relationship is slightly cumbersome and complicated
If it can be guaranteed URL和Hash值 1:1关系,The data structure is slightly simpler,Also need to maintain multiplekv关系
It is estimated that most of them will choose this method.
长短1:1Scenario improvements
前提:
- URL和Hash值 1:1关系
- Only do it for long chainsHash值,The short chain is relatively short, just inquire directly
- 使用redis实现
分析:
- URL根据HashAlgorithms compute directly
- 冲突解决:Short chains need to be judged heavy,If it is repeated, the long chain will add a fixed string and continue to calculate the short chain judgment weight
- 判重优化:You can use the Bloom filter to determine whether the short chain already exists
- hash算法可以是:MD5,SHA加密算法;非加密型MurmurHash 算法
存储kv对:
- 长 Code 》 短
- 短 》长

Millions of high-performance short-chain systems
1. Web是局限

2. OpenResty直接请求Redis

END
边栏推荐
- "Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
- 87. 把字符串转换成整数
- 权限管理怎么做的?
- MySQL database (basic)
- Mini Program - Global Data Sharing
- MySQL——数据库的查,增,删
- ShardingSphere之读写分离(八)
- Can deep learning solve the parameters of a specific function?
- [Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
- Adding, deleting, modifying and checking the foundation of MySQL
猜你喜欢

《实战》基于电商领域的词性提取及其决策树模型建模

分布式.幂等性

typescript13 - type aliases

typescript15-(同时指定参数和返回值类型)

typescript10-commonly used basic types

Summary of MySQL database interview questions (2022 latest version)

深度学习可以求解特定函数的参数么?

WMware Tools installation failed segmentation fault solution

孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手

The level of ShardingSphere depots in actual combat (4)
随机推荐
网站频繁出现mysql等数据库连接失败等信息解决办法
Adding, deleting, modifying and checking the foundation of MySQL
typescript18-对象类型
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
Dispatch Center xxl-Job
typescript10-commonly used basic types
24. 请你谈谈单例模式的优缺点,注意事项,使用场景
BOM系列之history对象
剑指offer17---打印从1到最大的n位数
ShardingSphere read-write separation (8)
The sword refers to offer17---print the n digits from 1 to the largest
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
图像处理工具设计
tensorflow与GPU版本对应安装问题
场景之多数据源查询及数据下载问题
MySQL高级-六索引优化
35. Reverse linked list
A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
Why use high-defense CDN when financial, government and enterprises are attacked?
蓝牙mesh系统开发二 mesh节点开发