查看: 31736| 回复: 55
跳转到指定楼层
上一主题 下一主题
收起左侧

[经验总结] 十大经典系统设计题

   
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  • crawler 爬虫,meta高频



  • 视频上传观看系统(youtube,netflix)



  • event/metrics 采集实时计算系统(datadog,大数据公司等





  • 聊天系统 (基于AWS架构)



  • 群聊系统,slack,论坛







  • 朋友圈,newsfeed,timeline系统



  • tinyurl 系统





  • 设计yelp



  • airbnb booking system


  • ratelimit

Why Rate limiting?
* Preventing Resource Starvation: The most common reason for rate limiting is to improve the availability of API-based services by avoiding resource starvation. Load based denial of service (doS) attacks can be prevented if rate limiting is applied. Other users are not starved even when one user bombards the API with loads of requests.
* Security: Rate limiting prevents brute forcing of security intensive functionalities like login, promo code etc. Number of requests to these features is limited on a user level so brute force algorithms don’t work in these scenarios.
* Preventing Operational Costs: In case of auto-scaling resources on a pay per use model, Rate Limiting helps in controlling operational costs by putting a virtual cap on scaling of resources. Resources might scale out of proportion leading to exponential bills if rate limiting is not employed.

Rate Limiting Strategies
Rate limiting can be applied on the following parameters:
* User: A limit is applied on the number of requests allowed for a user in a given period of time. User based rate limiting is one of the most common & intuitive forms of rate limiting.

2. Concurrency: Here the limit is employed on the number of parallel sessions that can be allowed for a user in a given timeframe. A limit on the number of parallel connections helps mitigate DDOS attacks as well.
3. Location/ID: This helps in running location based or demography centric campaigns. Requests not from the target demography can be rate limited so as to increase availability in the target regions
4. Server: Server based rate limiting is a niche strategy. This is employed generally when specific servers need most of the requests, i.e. servers are strongly coupled to specific functions



Rate Limiting Algorithms
* Leaky Bucket: Leaky Bucket is a simple intuitive algorithm. It creates a queue with a finite capacity. All requests in a given time frame beyond the capacity of the queue are spilled off.
The advantage of this algorithm is that it smoothens out bursts of requests and processes them at a constant rate. It’s also easy to implement on a load balancer and is memory efficient for each user. A constant near uniform flow is maintained to the server irrespective of the number of requests.

Leaky Bucket
The downside of this algorithm is that a burst of requests can fill up the bucket leading to starving of new requests. It also provides no guarantee that requests get completed in a given amount of time.
2. Token Bucket: Token Bucket is similar to leaky bucket. Here we assign tokens on a user level. For a given time duration d, the number of request r packets that a user can receive is defined. Every time a new request arrives at a server, there are two operations that happen:
*                 Fetch token: The current number of tokens for that user is fetched. If it is greater than the limit defined then the request is dropped.
*                 Update token: If the fetched token is less than the limit for the time duration d, then the request is accepted and the token is appended.
This algorithm is memory efficient as we are saving less amount of data per user for our application. The problem here is that it can cause race condition in a distributed environment. This happens when there are two requests from two different application servers trying to fetch the token at the same time.

Token Bucket Algorithm
3. Fixed Window Counter: Fixed window is one of the most basic rate limiting mechanisms. We keep a counter for a given duration of time, and keep incrementing it for every request we get. Once the limit is reached, we drop all further requests till the time duration is reset.
The advantage here is that it ensures that most recent requests are served without being starved by old requests. However, a single burst of traffic right at the edge of the limit might hoard all the available slots for both the current and next time slot. Consumers might bombard the server at the edge in an attempt to maximise number of requests served.

Fixed Window Counter
4. Sliding Log : Sliding log algorithm involves maintaining a time stamped log of requests at the user level. The system keeps these requests time sorted in a Set or a Table. It discards all requests with timestamps beyond a threshold. Every minute we look out for older requests and filter them out. Then we calculate the sum of logs to determine the request rate. If the request would exceed the threshold rate, then it is held, else it is served.
The advantage of this algorithm is that it does not suffer from the boundary conditions of fixed windows. Enforcement of the rate limit will remain precise. Since the system tracks the sliding log for each consumer, you don’t have the stampede effect that challenges fixed windows.
However, it can be costly to store an unlimited number of logs for every request. It’s also expensive to compute because each request requires calculating a summation over the consumer’s prior requests, potentially across a cluster of servers. As a result, it does not scale well to handle large bursts of traffic or denial of service attacks.
5. Sliding Window: This is similar to the Sliding Log algorithm, but memory efficient. It combines the fixed window algorithm’s low processing cost and the sliding log’s improved boundary conditions.
We keep a list/table of time sorted entries, with each entries being a hybrid and containing the timestamp and the number of requests at that point. We keep a sliding window of our time duration and only service requests in our window for the given rate. If the sum of counters is more than the given rate of the limiter, then we take only the first sum of entries equal to the rate limit.
The Sliding Window approach is the best of the lot because it gives the flexibility to scale rate limiting with good performance. The rate windows are an intuitive way to present rate limit data to API consumers. It also avoids the starvation problem of the leaky bucket and the bursting problems of fixed window implementations



Rate Limiting in Distributed Systems
The above algorithms works very well for single server applications. But the problem becomes very complicated when there is a distributed system involved with multiple nodes or app servers.It becomes more complicated if there are multiple rate limited services distributed across different server regions. The two broad problems that comes across in these situations are Inconsistency and Race Conditions.
Inconsistency
In case of complex systems with multiple app servers distributed across different regions and having their own rate limiters, we need to define a global rate limiter.
A consumer could surpass the global rate limiter individually if it receives a lot of requests in a small time frame. The greater the number of nodes, the more likely the user will exceed the global limit.
There are two ways to solve for these problems:
*                 Sticky Session: Have a sticky session in your load balancers so that each consumer gets sent to exactly one node. The downsides include lack of fault tolerance & scaling problems when nodes get overloaded. You can read more about sticky sessions here
*                 Centralized Data Store: Use a centralized data store like Redis or Cassandra to handle counts for each window and consumer. The added latency is a problem, but the flexibility provided makes it an elegant solution.
Race Conditions
Race conditions happen in a get-then-set approach with high concurrency. Each request gets the value of counter then tries to increment it. But by the time that write operation is completed, several other requests have read the value of the counter(which is not correct). Thus a very large number of requests are sent than what was intended. This can be mitigated using locks on the read-write operation, thus making it atomic. But this comes at a performance cost as it becomes a bottleneck causing more latency.




补充内容 (2022-04-29 07:42 +8:00):
部分贴图来自这个网站

https://www.theinsaneapp.com/202 ... ion-algorithms.html
更多图片 小图 大图
组图打开中,请稍候......

评分

参与人数 83大米 +122 收起 理由
LouisT + 1 很有用的信息!
mikemike0 + 3 给你点个赞!
jacobxiong + 1 很有用的信息!
holdwill + 1 赞一个
小亩_og272mn + 1 很有用的信息!谢谢分享!

查看全部评分


上一篇:跟着Alex学系统知识- 图文并茂版
下一篇:设计calendar

本帖被以下淘专辑推荐:

全局:
我真心觉得一亩三分地也应该做一个ml recommendation system. 这样我给这个帖子点赞以后就会给我推更多这样的帖子。地理现在的信噪比太高了
回复

使用道具 举报

推荐
 楼主| Chasedream.df 2022-4-28 15:24:51 来自APP | 只看该作者
全局:
小亩_ll22qw3 发表于 2022-04-28 00:03:01
我被楼主在系统设计方面的知识体系震惊到了,太优秀了!
楼主可否有时间分享如何学习系统设计像楼主一样好
这不一直都在分享么 资料多着去了 我看过订阅大量youtube github开源系统 到技术博客 经典paper 书籍 少说也有上百吧 把多年的功力集中输出 已经是速成版了

评分

参与人数 1大米 +1 收起 理由
Assassin_c + 1 请加大力度!

查看全部评分

回复

使用道具 举报

推荐
 楼主| Chasedream.df 2022-4-29 15:03:56 来自APP | 只看该作者
全局:
yzhan322 发表于 2022-04-28 23:20:41
楼主有推荐的OOD资源吗,谢谢
不过我遇到的ood很少 好像是设计个餐厅等位系统 还有一个日历会议系统 我建议不用花太多精力 现在不流行这些了
回复

使用道具 举报

全局:
我被楼主在系统设计方面的知识体系震惊到了,太优秀了!
楼主可否有时间分享如何学习系统设计像楼主一样好😄😄
回复

使用道具 举报

全局:
Chasedream.df 发表于 2022-04-28 00:24:51
这不一直都在分享么 资料多着去了 我看过订阅大量youtube github开源系统 到技术博客 经典paper 书籍 少说也有上百吧 把多年的功力集中输出 已经是速成版了
非常感谢!
回复

使用道具 举报

🔗
initid 2022-4-28 21:10:27 | 只看该作者
全局:
小白被震惊到了,非常感谢楼主分享,而且居然没有设阅读限制,小白我也能看到,谢谢!
回复

使用道具 举报

全局:
码zszszszs
回复

使用道具 举报

全局:
太厉害了!码住 顺便问下ml system design会有什么特别大的不同吗
回复

使用道具 举报

全局:
二话不说,先收藏一波
回复

使用道具 举报

🔗
 楼主| Chasedream.df 2022-4-28 21:52:50 来自APP | 只看该作者
全局:
SakuraXTY 发表于 2022-04-28 06:30:38
太厉害了!码住 顺便问下ml system design会有什么特别大的不同吗
不太一样 我新一篇总结
回复

使用道具 举报

🔗
ConnieQ 2022-4-28 22:29:14 | 只看该作者
全局:
看这图,似乎是educative那门系统设计课里的内容?还是说,其实educative里的那些图也是copy自网上?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表