Friday, March 26, 2021

System design: Comment system like Twitter | 评论系统架构设计

March 26, 2021

Here is the article. 

发布于: 2021 年 03 月 18 日





架构设计

本质:理解客户真正的需求,解决其问题。

写评论的压力问题:通过kafka消峰、分区扩容的能力解决。

数据设计



mysql一定需要配置主键,且数据必须顺序写,避免IO分裂影响写性能;

考虑评论内容比较大,mysql IO每次只有16KB数据,考虑读写性能,将评论内容、评论元数据独立成两个表,评论的点赞、更新评论数等操作只需要更新元数据表,且每次IO可以捞出更多的评论ID(评论内容表的主键),再通过ID点查到评论内容。另外内容和元数据独立也符合读写分离的业务场景。

for update容易死锁的原因:多个更新同时依赖多个数据,可能会交叉锁对方的资源,导致死锁,可以在操作前先保证操作顺序,避免同一数据行下的多个锁同时出现。

分页:使用流标接口。

思考:图存储更好?



缓存设计

缓存重建问题:一般会采用预加载数据到缓存,但如果预加载过多可能会产生惊群现象,这里采用预加载请求到kafka,然后通过job消费处理预加载。

redis sorted set冗余多份排序后的数据,解决排序数据的快速加载;

评论内容优化:protobuf序列化后存入缓存;

可用性设计

缓存穿透问题:

单飞的方式,避免一个进程里多个请求同时去回源数据。

go的单飞支持:https://pkg.go.dev/golang.org/x/sync/singleflight

缓存抖动:参考https://www.usenix.org/system/files/conference/nsdi13/nsdi13-final170_update.pdf

热点

写热点:消息队列缓冲压力

读热点:local cache,因为内存限制,可以用环形数组+滑动窗口统计数据,再取topk出来做local cache。

加餐

GC优化:

小对象初始化时用内联方式,避免多次new以减少内存小对象。

type Group struct {
s sync.Mutex
}



内联方式,如果用指针的话,得new 外层的Group,再new sync.Mutex,这里没加指针就是自动创建,且跟外层在同一个内存对象。



mono_repo

基础库

CI/CD

System design: One case study - shopping in seconds | in Chinese | 秒杀活动

Here is the article. 

本篇文章我们开始我们来聊下秒杀架构的相关内容,秒杀架构可以说是综合性非常强的一个场景,而且面试官特别喜欢基于这个场景进行提问,因此有必要好好了解下。

在正式开始之前,我们先来回顾下前面几篇文章的内容。

在第五篇中我们聊了缓存,我们先把数据存放到缓存中,每次请求通过缓存读取数据,大大减少了数据库读请求的压力。第六篇文章里我们聊了写缓存,流量洪峰时,我们先将数据写入缓存中,再逐步搬运数据到数据库,大大较少了数据库写请求的压力。第七篇文章里我们聊了数据收集,利用消息队列我们可以把缓存中的数据搬运到数据库中。而这几篇文章中涉及的架构设计思路,本篇文章中我们都会用到。

一、业务场景七

有一个秒杀活动,该活动提供了100件特价商品(商品价格非常低)供用户于10月10日22点10分0秒正式开始秒杀。

当时,平台已经积累了几千万的用户量,预计数十万的用户对此特价商品感兴趣。按照秒杀活动的调性,特价商品一般会在1-2秒内被一抢而光,剩余时间涌进来的流量只能看到秒杀结束界面,因此我们预测秒杀开启那一瞬间会出现一个瞬间流量峰值。

这也是一场短暂性的活动,不能添加太多的服务器,也不能花太多的时间重构,说白了也就是需要以最小的技术代价搞定这次秒杀活动。

因此,我们这次秒杀架构的设计目标是以较小的改动保证秒杀时间的流量洪流不会冲垮服务器。

对于秒杀架构设计而言,其难点在于僧多粥少,因此在设计秒杀架构时,一般要遵循东西不能超卖、下单成功的订单数据不能丢失、服务器和数据库不能挂、尽量别让机器人抢走商品这4个原则。

那如何做到这4个原则呢?我们先从整体思路入手吧。

二、整体思路



其实,秒杀架构的设计方案就是一个不断过滤请求的过程。从系统架构层面来说,秒杀系统的分层设计思路如下图所示:



在上图中可以发现,秒杀系统的架构设计目标是尽量在上层把用户的请求处理掉,不让其往下层游动,那具体如何实现呢?



由于整个秒杀系统涉及多个用户操作步骤,因此解决如何将请求拦截在系统上层这个问题时,我们需要结合实际业务流程,将用户的每个操作步骤考虑在内。



为了方便理解,我们通过一张图来描述秒杀系统的具体业务流程,如下图所示:





接下来,我们按照秒杀架构系统的业务流程来一步步说明如何将请求拦截在系统上游。



(一)浏览页面如何将请求拦截在上游?



在以往的秒杀系统架构经历中,曾出现过这么个幺蛾子:当时我们把系统的方方面面都考虑到了,检查完自我感觉棒棒哒,可是活动一上线,系统立马显示异常,一通检查后,我们发现所有服务器的性能指标都没问题,唯独出口带宽出问题了。(它被占满了)



结果大家也都知道,参与活动时页面出现严重卡顿,用户吐槽不断。



通过这次惨痛经历,我们设计思路进行了相关调整,后期对于静态资源能上CDN就上CDN。如果涉及到PC网站,首先必须前后端分离,然后静态资源能上CDN就上CDN。



看到这里,我们有必要先了解一下什么是CDN。



比如我们平时访问的请求是https://static.resource.xx/1.jpg,这个地址指向就是自己的服务器,经过改造后,我们将static.resouce.xx这个域名解析交给CDN服务商。因CDN服务商在全国各地都有服务器,服务器中存放着我们想要的静态资源的缓存。CDN收到这个域名后,首先会寻找一台响应最快的服务器,并指向这个服务器的IP。(大体意思就是这样,如果大家对细节有兴趣,可以留言告诉我,我们用一篇文章来介绍。关注公众号:服务端技术精选留言就行)



因此,使用CDN的好处是不浪费自己服务器资源和带宽,且响应速度快。通过这种方式,我们可以把静态资源的压力拦截在系统分层的外面。



那如果是动态的请求该怎么办?有如下三种实现方式。



  1. 比如评论、商品属性详情、购买数等请求,平时我们都是通过JS后台动态调用。在这个场景中,我们可以把动态的数据与页面进行整合,比如把每个秒杀商品的详情页面变成静态页面,然后再放入CDN。如果嫌改造太大,我们也可以把它放在Redis缓存中,不过我更倾向于CDN。

  2. 判断服务器时间开启秒杀标识:一般页面中都有一个JS,它通过访问服务器获取服务器时间,然后根据时间开启秒杀下单的按钮,即判断秒杀开始时,我们会将下单按钮设置为可以购买。针对获取服务器时间的这个请求,我们把它放在静态资源或负载均衡那层即可,这样用户请求就不会进入系统下游。

  3. 判断秒杀结果:我们的做法是将秒杀结束的标识放在cookie中,如果cookie中没有结束标识,请求就会进入后台服务器,后台服务器判断本地内存没有结束标识,就会进入缓存中,要是缓存中也没有结束标识,那就说明秒杀没结束。



总的来说,对于浏览页面的用户行为,我们需要把用户请求尽量拦截在CDN、静态资源或负载均衡侧,实在不行可以拦截在缓存中。



(二)下单页面如何将请求拦截在上游?



用户进入下单页时,主要分为2个操作动作:进入下单页、订单提交。下面我们来聊下如何在这两个环节中将请求拦截在系统上游。



(1)进入下单页



为了防止别人通过爬虫抓取下单页面信息,从而给服务器增加压力,因此我们需要在下单页中做以下2层防护,以防止(恶意)请求重复提交。



  1. 页面URL后台动态获取:按照正常的活动设计流程,用户只有在秒杀活动开启后才可进入下单页,但难免有同行在活动开启前直接获取下单页的URL并不断刷新,这样恶意请求就跑到了后台服务器中。虽然后台服务器也可以拦截恶意请求,但是徒增了不少压力。此时我们主要使用一个诡异的URL进行处理。(我们不把它放在静态页面中,而是通过后台动态获取。)前面我们介绍了JS可以用来判断秒杀开始时间,秒杀时间一到,它便可以通过另一个请求获取这个URL。

  2. 用户点击下单页的购买按钮直接disable,因此我们还需要防止用户不断点击购买按钮。



(2)订单提交



秒杀系统架构方案的重心是订单提交,因为订单提交这个步骤最复杂,其他步骤仅是页面展示的逻辑,针对高并发问题使用缓存或者CDN进行处理难度不大。



因此,在订单提交环节,我们要想尽一切办法在系统各个分层中把一些不必要的请求过滤掉。



网关层面过滤请求



对系统而言,如果我们可以在网关层面拦截掉用户请求,可以说这个方案的性价比很高。要是能在这层过滤95%以上的请求,整个系统也就很稳定。



那在网关层面如何实现请求过滤呢?这里我分享三种方式。



  1. 限定每个用户访问频率:比如每5秒下单1次。

  2. 限定每个IP访问频率:这种方式我们担心有些人通过机器人自动下单,因此错杀真实用户。

  3. 把一个时间段内的请求拦截掉一个百分比,或者只允许特定数量的请求进入后台服务器。(这里我们可以使用限流的漏桶或令牌算法,在后面的文章中再详细展开)



后台服务器过滤请求



请求进入后台服务器后,我们的目标已经不是如何过滤请求了,而是如何保证特价商品不超卖,以及如何保证特价商品订单数据的准确性。



那具体如何实现呢?我们可以通过以下三种方式。



  1. 商品库存放入缓存中:如果每个请求都前往数据库查询商品库存,数据库肯定扛不住,因此我们需要把库存存放在缓存中,这样每次用户下单前,先使用decr扣减库存,判断返回值。如果Redis的库存扣减后<0,说明秒杀失败,库存incr回去;如果Redis的库存扣减后>=0,说明秒杀成功,开始创建订单。

  2. 订单写入缓存中:在第六篇写缓存时,我们提及过1个方案,即订单数据先不放入数据库,而是先放到缓存中,然后每隔一段时间(100ms)批量插入订单。我们知道用户下单后,首先进入一个等待页面,然后这个页面向后台定时轮询订单数据。轮询订单数据的过程中,后台先在Redis中查询订单数据,查不到说明已经落库,再去数据库查询订单数据,查到后直接返回给用户,用户收到消息通知后可以直接进入付款页面支付了。在数据库查询订单数据时,查不到说明秒杀失败。(理论上不会查不到,你要是一直查不到记得抛个异常后续跟踪处理下。)

  3. 订单批量落库:我们需要定期将订单批量落库,且在订单落库时扣减数据库中的库存。



以上就是订单提交操作的架构设计,不难看出我们主要是在网关层和后台服务器进行相关设计。


(三)付款页如何将请求拦截在上游?



在付款页面,我们基本不需要再过滤用户请求了。在这个环节,我们除了保障数据的一致性,还需要注意一个要点:如果业务逻辑上出现一个订单未及时付款而被取消,记得把数据库及Redis的库存加回去。



三、整体服务器架构



我们再来回顾下之前的秒杀系统架构的分层设计思路,这也是秒杀系统的整体服务器架构方案。





为了保障秒杀系统的高可用,在整体服务器架构中,我们需要保证上图中所有的层级都是高可用。因此,静态资源服务器、网关、后台服务器均需配置负载均衡,而缓存Redis和数据库均需要配置集群模式。



整体服务器架构中还有一个重要组成部分——MQ,因为这次的秒杀架构方案中不涉及它的设计逻辑,所以我们并未在上面的分层中提及它。不过,服务间触发通知时,我们就需要使用它了,因此我们也需要保证它是高可用的。(这里我们要把主从、分片、failover机制都考虑进去。)



四、总结



说到这里,秒杀架构的注意事项就说完了。因很多注意点在第五、六、七章都介绍过,所以本文中的内容就比较简练。



下表中整理了一份秒杀架构check list,在你设计秒杀系统时,这部分知识对你可能有用。



流程事项浏览页面静态资源放CDN浏览页面秒杀期间一些动态数据请求放进静态页面浏览页面秒杀开始的时间获取依赖服务端浏览页面秒杀结束的标识放在各个地方下单下单URL动态后台获取下单购买按钮点击完置灰下单网关从三个方面过滤请求:1、用户访问频率。2、IP访问频率。3、整体流量控制。下单库存放Redis,每次判断缓存防止超卖。下单订单先放缓存再批量落库。付款订单取消记得加回数据库和Redis的库存。服务器架构静态资源服务器负载均衡。服务器架构网关负载均衡服务器架构后台服务负载均衡服务器架构Redis集群服务器架构MQ集群服务器架构数据库集群



在本篇中,还有两个点我们没有说到。



  • 网关层的限流。

  • 假设后台某服务因秒杀宕机了,如何避免其他服务雪崩。

System design: 消息队列 | Message queue

March 26, 2021

I just quickly copied some good arguments about system design, message queue. I will look into more through Google search. 


既然考察到让你对消息队列进行架构设计,不是说你一定要研究过消息队列的源码,其实从根本上说,就是考察你了不了解消息队列的基本原理、核心组成部分、基本架构构成,同时再结合一些开源技术做参考理解消息队列的架构设计思路。

说到消息队列的架构设计,我们可以从以下几个角度来思考:

首先是消息队列要支持可伸缩性。在需要的时候能够快速扩容,提高吞吐量和容量,针对这一点,就是要设计一套分布式的系统。这里可以参考kafka的设计理念,也就是broker->topic->partition这种模式,每个partition放到每一台机器里,只存一部分的数据。如果资源不够了,就给topic增加partition,再做数据迁移,增加机器,这样就可以存放更多数据,提供更高的吞吐量。

然后要考虑消息队列的数据有没有必要持久化到磁盘里。这是肯定的,持久化到磁盘里才能保证线程挂的时候消息不会丢失。这里也参考kafka,顺序写进磁盘,这样就没有磁盘随机读写的寻址开销,磁盘顺序读写的性能也是很高的。

还有就是消息队列的可用性。这点可以通过kafka的高可用保障机制来实现。思路就是多副本->leader&follower->broker挂了重新选举leader即可对外服务。

最后如果要支持数据0丢失。也是可以参考kafka数据零丢失方案。

消息队列

消息队列确实是很复杂的,但其实考察这种开放性问题就是考察你从架构角度整体构思和设计的思维以及能力。

MX stock | Semiconductor sector | SYNA stock | My research and prediction - next 12 months 100% return

 March 26, 2021

Steps to work on my short research. Here are steps:

  1. MX stock went up 26% percent. I got a tip from a friend in wechat investment group. 
  2. Go to Finviz -> Screener -> Find all past quarter gains more than 25%, find those stocks in uptrend to 100% possible.
  3. Import those stocks into Yahoo -> Finance -> portfolio
  4. Go to detail view to check last 12 months gains, volumes, shares outstanding, look for a small or medium cap with growth potential
  5. Narrow down three stocks, MX, SYNA and MRAM



NOK stock: Yahoo -> Finance -> Conversation | ClearWorld

 Nokia Corporation NOK recently inked a reseller agreement with ClearWorld to offer smart city solutions across the United States. The deal with the alternate energy systems provider will facilitate the company to provide a comprehensive portfolio of smart urban infrastructure solutions to various cities.


Per the deal, Nokia will sell ClearWorld’s smart poles as part of its smart city offering to provide a sustainable, resilient, and redundant power source for uninterrupted power supply to keep critical infrastructure and networks live through power outages and natural disasters. The solar wrapped poles further reduce utility operating and maintenance costs and boast built-in IoT capabilities. Leveraging such unique capabilities, the telecommunications equipment provider can deploy 4G and 5G radios on the poles for seamless connectivity options as well as for innovative IoT-based city services.

The smart poles can be installed in various locations such as parks, parking lots and roadways, supporting diverse applications including video analytics, Wi-Fi access points, smart lighting, gun-shot detection and license plate reading. Such infrastructure solutions are likely to pave the way for intelligent, integrated city platforms that utilize 5G, industrial IoT and machine learning technologies to provide foundations for safe and secure urban innovations.

Nokia is well positioned for the ongoing technology cycle, given the strength of its end-to-end portfolio. The company is driving the transition of global enterprises into smart virtual networks by creating a single network for all services, converging mobile and fixed broadband, IP routing and optical networks with software and services to manage them. Leveraging state-of-the-art technology, Nokia is transforming the way people and things communicate and connect with each other. These include seamless transition to 5G technology, ultra-broadband access, IP and Software Defined Networking, cloud applications and IoT.

Yahoo -> Finance -> Portfolio: I like to learn better to manage the risk to invest on GTE.TO, GTE, GRAY and other biotech stocks

March 26, 2021

I like to spend less time to invest on stock market, and spend more time to learn coding and system design. 





Thursday, March 25, 2021

How I manage Money in Relationships & Marriage. (as a millionaire)

March 25, 2021

Here is the link. 

Ex-Google TechLead on managing money in relationships. Check out Ting Mobile for a cheaper cellphone plan and get your $25 credit: http://techlead.ting.com​ (new and improved)! Join ex-Google/ex-Facebook engineers for my coding interview training: https://techinterviewpro.com/

System design: How do I learn websockets in 2021?

March 25, 2021

Introduction

I did not know my weakness to learn system design. I learned by reading a few articles about websockets, but last five years I did not learn enough for me to pass Microsoft onsite interview. I have to review my failure and see what I should learn better in 2021. I spent two weeks to learn websockets. 

Google search - Keywords - Websockets - Image search

I did search Google using keyword: Websockets, and then I tried to read as many as 20 articles, and I found out that image search is more efficient. I had chance to read webSockets in C#, Java and another language as well. 


I spent two weeks to read all articles and good thing is to read some code implementation in C# as well. I also noticed that I should learn more about some basic concepts as well. 


Ameritrade.com | IRA | unrealized gain | biotech gains -$1030 | March 25, 2021

 March 25, 2021

Introduction

It is surprising to learn something from the market. The stock went down and then changed direction to go up after less than three hours. 

My update on March 25, 2021




Actionable Items

I like to learn from my own experience. Work hard, be consistent, and do not set up my positions when SPX is too high. 

Wait for market crash and then add more positions. Buy low and sell high. 


Potential 2X -5X - $5 Stock Dip - Opportunity to BUY - ATNX Athenex, Inc.

March 25, 2021

Here is the link. 

Athenex, Inc., a biopharmaceutical company, engages in the discovery, development, and commercialization of various therapies for the treatment of cancer and related conditions in North America and Asia. Analyst average twelve-month price target is $15.00, predicting that the stock has a possible upside of 192.11%. The high price target for ATNX is $33.00 and the low price target for ATNX is $5.00. There are currently 6 hold ratings and 3 buy ratings for the stock, resulting in a consensus rating of "Hold." DISCLAIMER: I am not a financial advisor, nor do I claim to be. Videos are for entertainment and educational purposes only. Investing of any kind involves risk. Everything you watch on my channel is my opinion. These are not suggestions or directions as to how to handle your money. It is important that you conduct your own research.

ATNX stock: Athenex Stock Analysis - Buy Hold or Sell - ATNX Stock Analysis FDA Rejection? Drug Pipeline?

March 25, 2021

Here is the link. 

Should you Buy Hold or Sell or Athenex Stock (ATNX Stock) today? As a former Wall Street Stock broker I go over a highly detailed analysis and list my recommendation. #ATNX​ #Stocks​ #Athenex​ I am former Wall Street stock broker that has worked for one of the largest financial firms on the market back in 2007-2008 market crisis and I am here to give you my analysis of Workhorse Stock analysis going into 2021 and beyond. I will go over a summary of the company, its product offerings, recent news, financials, analyst projections and

Microservices Patterns by Chris Richardson

March 25, 2021

Here is the link. 

I spent whole day to read about Microservice, and I think that it is better for me to learn from Chris Richardson and watch some videos first. 



Watch Congress grill tech CEOs over misinformation

March 25, 2021

Here is the link. 

Congress questioned Facebook CEO Mark Zuckerberg, Google CEO Sundar Pichai, and Twitter CEO Jack Dorsey over misinformation on their platforms and how it contributed to the Capitol riot.

Oracle: Chris Richardson on Microservices Architecture Strategy

March 25, 2021

Here is the link. 

Chris Richardson, Developer and Architect, Eventuate, and Creator of microservices.io, discusses the importance of a microservice architecture strategy to build applications so you can build better software faster.