当前位置: 首页 > 编程日记 > 正文

Go处理百万每分钟的请求

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

I have been working in the anti-spam, anti-virus and anti-malware industry for over 15 years at a few different companies, and now I know how complex these systems could end up being due to the massive amount of data we handle daily.

Currently I am CEO of smsjunk.com and Chief Architect Officer at KnowBe4, both in companies active in the cybersecurity industry.

What is interesting is that for the last 10 years or so as a Software Engineer, all the web backend development that I have been involved in has been mostly done in Ruby on Rails. Don’t take me wrong, I love Ruby on Rails and I believe it’s an amazing environment, but after a while you start thinking and designing systems in the ruby way, and you forget how efficient and simple your software architecture could have been if you could leverage multi-threading, parallelization, fast executions and small memory overhead. For many years, I was a C/C++, Delphi and C# developer, and I just started realizing how less complex things could be with the right tool for the job.

I am not very big on the language and framework wars that the interwebs are always fighting about. I believe efficiency, productivity and code maintainability relies mostly on how simple you can architect your solution.

The Problem

While working on a piece of our anonymous telemetry and analytics system, our goal was to be able to handle a large amount of POST requests from millions of endpoints. The web handler would receive a JSON document that may contain a collection of many payloads that needed to be written to Amazon S3, in order for our map-reduce systems to later operate on this data.

Traditionally we would look into creating a worker-tier architecture, utilizing things such as:

  • Sidekiq
  • Resque
  • DelayedJob
  • Elasticbeanstalk Worker Tier
  • RabbitMQ
  • and so on…

And setup 2 different clusters, one for the web front-end and another for the workers, so we can scale up the amount of background work we can handle.

But since the beginning, our team knew that we should do this in Go because during the discussion phases we saw this could be potentially a very large traffic system. I have been using Go for about 2 years or so, and we had developed a few systems here at work but none that would get this amount of load.

We started by creating a few structures to define the web request payload that we would be receiving through the POST calls, and a method to upload it into our S3 bucket.

type PayloadCollection struct {WindowsVersion  string    `json:"version"`Token           string    `json:"token"`Payloads        []Payload `json:"data"`
}type Payload struct {// [redacted]
}func (p *Payload) UploadToS3() error {// the storageFolder method ensures that there are no name collision in// case we get same timestamp in the key namestorage_path := fmt.Sprintf("%v/%v", p.storageFolder, time.Now().UnixNano())bucket := S3Bucketb := new(bytes.Buffer)encodeErr := json.NewEncoder(b).Encode(payload)if encodeErr != nil {return encodeErr}// Everything we post to the S3 bucket should be marked 'private'var acl = s3.Privatevar contentType = "application/octet-stream"return bucket.PutReader(storage_path, b, int64(b.Len()), contentType, acl, s3.Options{})
}

Naive approach to Go routines

Initially we took a very naive implementation of the POST handler, just trying to parallelize the job processing into a simple goroutine:

func payloadHandler(w http.ResponseWriter, r *http.Request) {if r.Method != "POST" {w.WriteHeader(http.StatusMethodNotAllowed)return}// Read the body into a string for json decodingvar content = &PayloadCollection{}err := json.NewDecoder(io.LimitReader(r.Body, MaxLength)).Decode(&content)if err != nil {w.Header().Set("Content-Type", "application/json; charset=UTF-8")w.WriteHeader(http.StatusBadRequest)return}// Go through each payload and queue items individually to be posted to S3for _, payload := range content.Payloads {go payload.UploadToS3()   // <----- DON'T DO THIS}w.WriteHeader(http.StatusOK)
}

For moderate loads, this could work for the majority of people, but this quickly proved to not work very well at a large scale. We were expecting a lot of requests but not in the order of magnitude we started seeing when we deployed the first version to production. We completely understimated the amount of traffic.

The approach above is bad in several different ways. There is no way to control how many go routines we are spawning. And since we were getting 1 million POST requests per minute of course this code crashed and burned very quickly.

Trying again

We needed to find a different way. Since the beginning we started discussing how we needed to keep the lifetime of the request handler very short and spawn processing in the background. Of course, this is what you must do in the Ruby on Rails world, otherwise you will block all the available worker web processors, whether you are using puma, unicorn, passenger (Let’s not get into the JRuby discussion please). Then we would have needed to leverage common solutions to do this, such as Resque, Sidekiq, SQS, etc. The list goes on since there are many ways of achieving this.

So the second iteration was to create a buffered channel where we could queue up some jobs and upload them to S3, and since we could control the maximum number of items in our queue and we had plenty of RAM to queue up jobs in memory, we thought it would be okay to just buffer jobs in the channel queue.

var Queue chan Payloadfunc init() {Queue = make(chan Payload, MAX_QUEUE)
}func payloadHandler(w http.ResponseWriter, r *http.Request) {...// Go through each payload and queue items individually to be posted to S3for _, payload := range content.Payloads {Queue <- payload}...
}

And then to actually dequeue jobs and process them, we were using something similar to this:

func StartProcessor() {for {select {case job := <-Queue:job.payload.UploadToS3()  // <-- STILL NOT GOOD}}
}

To be honest, I have no idea what we were thinking. This must have been a late night full of Red-Bulls. This approach didn’t buy us anything, we have traded flawed concurrency with a buffered queue that was simply postponing the problem. Our synchronous processor was only uploading one payload at a time to S3, and since the rate of incoming requests were much larger than the ability of the single processor to upload to S3, our buffered channel was quickly reaching its limit and blocking the request handler ability to queue more items.

We were simply avoiding the problem and started a count-down to the death of our system eventually. Our latency rates kept increasing in a constant rate minutes after we deployed this flawed version.

The Better Solution

We have decided to utilize a common pattern when using Go channels, in order to create a 2-tier channel system, one for queuing jobs and another to control how many workers operate on the JobQueue concurrently.

The idea was to parallelize the uploads to S3 to a somewhat sustainable rate, one that would not cripple the machine nor start generating connections errors from S3. So we have opted for creating a Job/Worker pattern. For those that are familiar with Java, C#, etc, think about this as the Golang way of implementing a Worker Thread-Pool utilizing channels instead.

var (MaxWorker = os.Getenv("MAX_WORKERS")MaxQueue  = os.Getenv("MAX_QUEUE")
)// Job represents the job to be run
type Job struct {Payload Payload
}// A buffered channel that we can send work requests on.
var JobQueue chan Job// Worker represents the worker that executes the job
type Worker struct {WorkerPool  chan chan JobJobChannel  chan Jobquit    	chan bool
}func NewWorker(workerPool chan chan Job) Worker {return Worker{WorkerPool: workerPool,JobChannel: make(chan Job),quit:       make(chan bool)}
}// Start method starts the run loop for the worker, listening for a quit channel in
// case we need to stop it
func (w Worker) Start() {go func() {for {// register the current worker into the worker queue.w.WorkerPool <- w.JobChannelselect {case job := <-w.JobChannel:// we have received a work request.if err := job.Payload.UploadToS3(); err != nil {log.Errorf("Error uploading to S3: %s", err.Error())}case <-w.quit:// we have received a signal to stopreturn}}}()
}// Stop signals the worker to stop listening for work requests.
func (w Worker) Stop() {go func() {w.quit <- true}()
}

We have modified our Web request handler to create an instance of Jobstruct with the payload and send into the JobQueue channel for the workers to pickup.

func payloadHandler(w http.ResponseWriter, r *http.Request) {if r.Method != "POST" {w.WriteHeader(http.StatusMethodNotAllowed)return}// Read the body into a string for json decodingvar content = &PayloadCollection{}err := json.NewDecoder(io.LimitReader(r.Body, MaxLength)).Decode(&content)if err != nil {w.Header().Set("Content-Type", "application/json; charset=UTF-8")w.WriteHeader(http.StatusBadRequest)return}// Go through each payload and queue items individually to be posted to S3for _, payload := range content.Payloads {// let's create a job with the payloadwork := Job{Payload: payload}// Push the work onto the queue.JobQueue <- work}w.WriteHeader(http.StatusOK)
}

During our web server initialization we create a Dispatcher and call Run()to create the pool of workers and to start listening for jobs that would appear in the JobQueue.

dispatcher := NewDispatcher(MaxWorker) 
dispatcher.Run()

Below is the code for our dispatcher implementation:

type Dispatcher struct {// A pool of workers channels that are registered with the dispatcherWorkerPool chan chan Job
}func NewDispatcher(maxWorkers int) *Dispatcher {pool := make(chan chan Job, maxWorkers)return &Dispatcher{WorkerPool: pool}
}func (d *Dispatcher) Run() {// starting n number of workersfor i := 0; i < d.maxWorkers; i++ {worker := NewWorker(d.pool)worker.Start()}go d.dispatch()
}func (d *Dispatcher) dispatch() {for {select {case job := <-JobQueue:// a job request has been receivedgo func(job Job) {// try to obtain a worker job channel that is available.// this will block until a worker is idlejobChannel := <-d.WorkerPool// dispatch the job to the worker job channeljobChannel <- job}(job)}}
}

Note that we provide the number of maximum workers to be instantiated and be added to our pool of workers. Since we have utilized Amazon Elasticbeanstalk for this project with a dockerized Go environment, and we always try to follow the 12-factor methodology to configure our systems in production, we read these values from environment variables. That way we could control how many workers and the maximum size of the Job Queue, so we can quickly tweak these values without requiring re-deployment of the cluster.

var ( MaxWorker = os.Getenv("MAX_WORKERS") MaxQueue  = os.Getenv("MAX_QUEUE") 
)

Immediately after we have deployed it we saw all of our latency rates drop to insignificant numbers and our ability to handle requests surged drastically.

Minutes after our Elastic Load Balancers were fully warmed up, we saw our ElasticBeanstalk application serving close to 1 million requests per minute. We usually have a few hours during the morning hours in which our traffic spikes over to more than a million per minute.

As soon as we have deployed the new code, the number of servers dropped considerably from 100 servers to about 20 servers.

After we had properly configured our cluster and the auto-scaling settings, we were able to lower it even more to only 4x EC2 c4.Large instances and the Elastic Auto-Scaling set to spawn a new instance if CPU goes above 90% for 5 minutes straight.

Conclusion

Simplicity always wins in my book. We could have designed a complex system with many queues, background workers, complex deployments, but instead we decided to leverage the power of Elasticbeanstalk auto-scaling and the efficiency and simple approach to concurrency that Golang provides us out of the box.

It’s not everyday that you have a cluster of only 4 machines, that are probably much less powerful than my current MacBook Pro, handling POST requests writing to an Amazon S3 bucket 1 million times every minute.

There is always the right tool for the job. For sometimes when your Ruby on Rails system needs a very powerful web handler, think a little outside of the ruby eco-system for simpler yet more powerful alternative solutions.

Before you go…

I would really appreciate if you follow us on Twitter and share this post with your friends. You can find me on Twitter at http://twitter.com/mcastilho

转载于:https://my.oschina.net/lemonwater/blog/1526925

相关文章:

data pump工具

expdp和impdp的用法ORCALE10G提供了新的导入导出工具&#xff0c;数据泵。Oracle官方对此的形容是&#xff1a;OracleDataPump technology enables Very High-Speed movement of data and metadata from one database to another.其中Very High-Speed是亮点。先说数据泵提供的主…

游标对于分页存储过程

1。我个人认为最好的分页方法是: Select top 10 * from table where id>200写成存储过程,上面的语句要拼一下sql语句,要获得最后大于的哪一个ID号 2。那个用游标的方式,只适合于小数据量的表,如果表在一万行以上,就差劲了 你的存储过程还比不上NOT IN分页,示例: SELECT …

混沌、无序、变局?探索之中,《拟合》开启

从无序中寻找踪迹&#xff0c;从眼前事探索未来在东方的神话里&#xff0c;喜鹊会搭桥&#xff0c;银河是条河&#xff0c;两端闪耀的牵牛星和织女星&#xff0c;则是一年一相会的佳偶&#xff0c;他们彼此间的惦念&#xff0c;会牵引彼此在七夕那天实现双星聚首。在西方的世界…

linxu 下安装mysql5.7.19

2019独角兽企业重金招聘Python工程师标准>>> 1、首先检查是否已经安装过mysql,查找mysql相关软件rpm包 # rpm -qa | grep mysql 2、将所有与mysql相关的东西删除 #yum -y remove mysql-libs-5.1.66-2.el6_3.x86_64 3、安装依赖包 #yum -y install make gcc-c cmake …

C#技术内幕 学习笔记

引用类型是类型安全的指针&#xff0c;它们的内存是分配在堆&#xff08;保存指针地址&#xff09;上的。String、数组、类、接口和委托都是引用类型。 强制类型转换与as类型转换的区别&#xff1a;当类型转换非法时&#xff0c;强制类型转换将抛出一个System.InvalidCastExce…

java的深度克隆

原文&#xff1a;http://blog.csdn.net/randyjiawenjie/article/details/7563323javaobjectinterfacestringclassexception先做个标记 http://www.iteye.com/topic/182772 http://www.blogjava.net/jerry-zhaoj/archive/2009/10/14/298141.html 关于super.clone的理解 http://h…

持续推进预估时间问题研究,滴滴盖亚计划开放ETA数据集

4月29日消息&#xff0c;为持续推进行程时长预估问题研究&#xff0c;滴滴联合GIS(地理信息系统)领域国际顶会ACM SIGSPATIAL发布ACM SIGSPATIAL GISCUP 2021比赛&#xff0c;鼓励研究者们基于滴滴新开放的行程时长数据集&#xff0c;进一步提升时间预估准确性。 预估到达时间…

3.Java集合-HashSet实现原理及源码分析

一、HashSet概述&#xff1a; HashSet实现Set接口&#xff0c;由哈希表&#xff08;实际上是一个HashMap实例&#xff09;支持&#xff0c;它不保证set的迭代顺序很久不变。此类允许使用null元素 二、HashSet的实现&#xff1a; 对于HashSet而言&#xff0c;它是基于HashMap实现…

一个函数返回多个值

有两种方法&#xff1a;1.使用指针变量声明函数&#xff08;或者使用数组变量&#xff09;2.使用传出参数 第一种方法&#xff1a;函数返回的是一个指针地址&#xff08;数组地址&#xff09;&#xff0c;这个内存地址有多个变量寄存在里面。这个方法我不太会用&#xff0c;传…

4月30日或为上半年“最难打车日”

滴滴出行昨日发布预测&#xff0c;称由于周五通勤晚高峰及假期启程高峰叠加&#xff0c;4月30日下午或将成为今年上半年“最难打车日”&#xff0c;用户将遇到叫车排队甚至打不到车的情况。滴滴呼吁&#xff0c;请提前规划行程&#xff0c;预留充足时间&#xff0c;大家五一快乐…

exchange 2003配置ASSP 反垃圾邮件

Exchange上第三方反垃圾邮件用得比较多的是ORF&#xff0c;它直接运行在虚拟SMTP服务上&#xff0c;配置非常的方便。ASSP&#xff08;https://sourceforge.net/projects/assp/&#xff09; 是一个开源的反垃圾邮件代理&#xff0c;反垃圾效果也非常好&#xff0c;这里不讲如何…

中国人工智能学会通讯——人工智能在各医学亚专科的发展现状及趋势 1.3 人工智能在各医学亚专科的发展态势...

1.3 人工智能在各医学亚专科的发展态势 1. 人工智能在眼科领域的应用 2016年11月&#xff0c;Google的研究者Gulshan博士等人在美国医学协会杂志“Journal of the American Medical Association”上发表的一篇文章&#xff0c;运用deep learning算法&#xff08;卷积神经网络&a…

在ASP.NET中如何用C#.NET实现基于表单的验证

这篇文章引用到了Microsoft .NET类库中的以下名空间&#xff1a; System.Data.SqlClient System.Web.Security &#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff…

PHP学习笔记 第八讲 Mysql.简介和创建新的数据库

八、Mysql.简介和创建新的数据库1、mysql简介与概要mysql是一个小型关系型数据管理系统&#xff0c;开发者为瑞典mysqlab公司现在已经被sun公司收购1.可以处理拥有上千万条记录的大型数据2.支持常见SQL语句规范3.可移植高&#xff0c;安装简单小巧4.良好的运行效率&#xff0c;…

摆脱 FM!这些推荐系统模型真香

‍‍作者 | 梁唐来源 | TechFlow之前我们介绍了推荐当中应用得非常广泛的FM大家族&#xff0c;从FM这个模型衍生出了一系列的模型&#xff0c;从纯FM&#xff0c;到AFM、FFM、DeepFM等等一系列的FM模型&#xff0c;最后的终极版本是xDeepFM。这个模型非常复杂&#xff0c;可以说…

新技术、新思维开创公共安全管理新模式

智慧城市的建设在国内外许多地区正如火如荼的进行中&#xff0c;在为期六天的第十七届中国国际高新技术成果交易会&#xff08;高交会&#xff09;上&#xff0c;智慧城市这一话题再次引发观众及城市建设者们的热议。 尤其是高交会期间召开的“2015亚太智慧城市发展高峰论坛”&…

.Net 中字符串性能

Introduction 你在代码中处理字符串的方法可能会对性能产生令人吃惊的影响。在本文中&#xff0c;我需要考虑两个由于使用字符串而产生的问题&#xff1a;临时字符串变量的使用和字符串连接。 Background 每个项目都有需要你为其考虑编码标准的时候。使用 FxCop 是一个好的开…

Lambda表达式可以被转换为委托类型

void Main() { //向Users类中增加两人; List<Users> usernew List<Users>{ new Users{ID1,Name"Jalen",Age23}, new Users{ID12,Name"Administrator",Age32}, }; //接下来就是利用Linq提供的新的方法来进行相关操作; var userslistuser.Wher…

人工干预如何提高模型性能?看这文就够了!

作者 | Preetam Joshi译者 | 吴家帆出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;有一些行业对误报非常敏感&#xff0c;如金融行业&#xff0c;在对信用卡欺诈检测时&#xff0c;如果检测系统将用户的行为错误地分类为欺诈&#xff0c;这将对该金融机构的声誉产生…

一种无需留坑为页面动态添加View方案

在Activity或Fragment页面动态添加View&#xff0c;有其应用场景&#xff0c;比如配合运营在首页动态插入H5活动页&#xff08;如下图手淘的雪花例示[1]&#xff09;,在页面头部插入通知View等。本文结合ActivityLifecycleCallbacks[2]及DecorView使用&#xff0c;为类似需求提…

边缘加速创新和AI应用,Xilinx推出Kria自适应系统模块产品组合

为了帮助开发者更容易使用FPGA和SoC的功能&#xff0c;赛灵思在开发工具上做了不少的投入&#xff0c;自适应系统模块&#xff08;SOM&#xff09;产品组合就是其中之一。 近日&#xff0c;赛灵思宣布推出Kria™自适应系统模块&#xff08; SOM &#xff09;产品组合&#xff…

windows计算器

using System; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Collections; using System.ComponentModel; using System.Data; namespace comput{ /// <summary> /// 这是一个计算器的简单实现。 /// </summary&…

哈夫曼树的构造

[转载于网易博客&#xff0c;具体地址不详] 构造哈夫曼树的过程是这样的 一、构成初始集合 对给定的n个权值{W1,W2,W3,...,Wi,...,Wn}构成n棵二叉树的初始集合F{T1,T2,T3,...,Ti,...,Tn}&#xff0c;其中每棵二叉树Ti中只有一个权值为Wi的根结点&#xff0c;它的左右子树均为空…

物联网时代全面降临

从智能建筑到零售&#xff0c;英特尔物联网解决方案可以说是华丽丽地惊艳着大家的大脑和眼球&#xff0c;一切的不可能似乎都在朝着可能的方向努力着。在2015 MWC上&#xff0c;英特尔再次用各种神奇的物联网设备告诉大家&#xff1a;物联网时代已经来临。 “半边天”的力量&am…

Linux C++/Java/Web/OC Socket网络编程

一&#xff0c;Linux C Socket网络编程 1.什么是TCP/IP、UDP&#xff1f; TCP/IP&#xff08;Transmission Control Protocol/Internet Protocol&#xff09;即传输控制协议/网间协议&#xff0c;是一个工业标准的协议集&#xff0c;它是为广域网&#xff08;WANs&#xff09;设…

ASP.NET抓取其他网页代码

在.Net 平台下&#xff0c;创建一个ASP.Net的程序 1、引用两个NAMESPACE using System.Text //因为用了Encoding类 using System.Net //因为用了WebClient 类 2、整个程序用了三个控件 txtUrl //输入你要获取的网页地址 TEXTBOX控件 txtBody //得到你要获取的网…

特斯拉遇上 CPU:程序员的心思你别猜

作者 | 码农的荒岛求生来源 | 码农的荒岛求生图源 | 视觉中国18世纪流水线的诞生带来了制造技术的变革&#xff0c;人类当今拥有琳琅满目物美价廉的商品和流水线技术的发明密不可分&#xff0c;因此当你喝着可乐、吹着空调、坐在特斯拉里拿着智能手机刷这篇文章时需要感谢流水线…

《算法技术手册》一2.4.6 二次方的算法性能

2.4.6 二次方的算法性能 现在考虑一个类似的问题&#xff1a;两个n位的整数相乘。例2-4展示了使用小学课堂上学过的算法实现的乘法运算&#xff0c;其中n位数字的表示方法与之前的加法一样。 例2-4&#xff1a;mult乘法的Java实现 public static void mult (int[] n1, int[] n2…

如何使用 OpenCV 实现图像均衡?

来源 | 小白视觉志头图 | 下载于视觉中国我们已经练习了很多图像处理——操作图像&#xff08;精确地说是图像矩阵&#xff09;。为此&#xff0c;我们探索了图像的均衡方法&#xff0c;以便在一定程度上增强对比度&#xff0c;以使被处理的图像看起来比原始图像更好&#xff0…

《中国人工智能学会通讯》——1.42 理解情感

1.42 理解情感 安德鲁摩尔认为&#xff0c;人工智能能“感受”人类情感是人工智能研究领域最重要、也最先进的一个方向。扬波利斯基认为&#xff0c;计算机能够理解语言的能力最终会向人和计算机“无缝沟通”的方向发展。 越来越精准的图像、声音和面部识别系统能让计算机更好探…