当前位置:网站首页>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
边栏推荐
- ShardingSphere之读写分离(八)
- typescript18-对象类型
- Artificial Intelligence and Cloud Security
- 87. Convert String to Integer
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- Go study notes (84) - Go project directory structure
- C language force buckles the rotating image of the 48th question.auxiliary array
- typescript11 - data types
- Kotlin协程:协程上下文与上下文元素
- 【Demo】ABAP Base64加解密测试
猜你喜欢
typescript11 - data types
Typescript18 - object type
Jmeter parameter transfer method (token transfer, interface association, etc.)
typescript17 - function optional parameters
4G通信模块CAT1和CAT4的区别
Responsive layout vs px/em/rem
Mysql:Invalid default value for TIMESTAMP
typescript13-类型别名
Thesis understanding: "Designing and training of a dual CNN for image denoising"
ShardingSphere's public table combat (7)
随机推荐
Rocky/GNU之Zabbix部署(2)
孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
Error in go mode tidy go warning “all” matched no packages
《实战》基于电商领域的词性提取及其决策树模型建模
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
Responsive layout vs px/em/rem
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
typescript13-类型别名
How to Add a Navigation Menu on Your WordPress Site
ShardingSphere's unsharded table configuration combat (6)
Go study notes (84) - Go project directory structure
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!
WMware Tools installation failed segmentation fault solution
Can deep learning solve the parameters of a specific function?
typescript13 - type aliases
响应式布局与px/em/rem的比对
822. Walk the Grid
蓝牙mesh系统开发三 Ble Mesh 配网器 Provisioner
人工智能与云安全