当前位置:网站首页>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
边栏推荐
- What is Promise?What is the principle of Promise?How to use Promises?
- Jetpack Compose learning (8) - State and remeber
- 《实战》基于情感词典的文本情感分析与LDA主题分析
- BOM系列之history对象
- In Google Cloud API gateway APISIX T2A and T2D performance test
- tensorflow与GPU版本对应安装问题
- Error in go mode tidy go warning “all” matched no packages
- "Actual Combat" based on part-of-speech extraction in the field of e-commerce and its decision tree model modeling
- 使用docker安装mysql
- 深度学习可以求解特定函数的参数么?
猜你喜欢
typescript14-(单独指定参数和返回值的类型)
Typescript14 - (type) of the specified parameters and return values alone
无线模块的参数介绍和选型要点
typescript9 - common base types
The level of ShardingSphere depots in actual combat (4)
Summary of MySQL database interview questions (2022 latest version)
【952. Calculate the maximum component size according to the common factor】
MySQL database advanced articles
ShardingSphere之未分片表配置实战(六)
Yolov7实战,实现网页端的实时目标检测
随机推荐
typescript10-常用基础类型
XSS related knowledge
typescript11-数据类型
MySql data recovery method personal summary
typescript10-commonly used basic types
87. Convert String to Integer
剑指offer17---打印从1到最大的n位数
Mini Program - Global Data Sharing
Niuke.com question brushing training (4)
基于Keras_bert模型的Bert使用与字词预测
ShardingSphere之未分片表配置实战(六)
查看zabbix-release-5.0-1.el8.noarch.rpm包内容
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
ShardingSphere之水平分库实战(四)
ShardingSphere read-write separation (8)
使用docker安装mysql
Zabbix干啥用?
权限管理怎么做的?
MySQL高级-六索引优化
Typescript18 - object type