为什么在本地主机上运行 API 的速度比在 AWS EC2 实例上慢那么多呢?

huangapple go评论72阅读模式
英文:

Why is running an API on localhost so much slower than on AWS EC2 instance?

问题

我是一名新的Java开发者,我一直在开发一个用于跟踪我们学校跑步队员里程的小应用程序。后端使用Java编写,服务器上最复杂的操作是获取所有队员的列表,然后计算他们上周的里程总和,并按从高到低的顺序对他们进行排名。当我在本地主机上调用这个端点时,耗时11.5秒!我发现使用并行流可以将时间缩短到约8.5秒,但我仍然担心这个时间太长了。我将新的JAR文件上传到AWS EC2实例后,只需要不到一秒的时间!所以我的问题是,为什么在我的本地主机上(四核i7 7600k,16GB内存)执行相同的操作时比AWS EC2实例慢这么多?顺便提一下,EC2实例连接到运行MySql的RDS实例。谢谢!

英文:

I'm a new Java developer and I've been developing a small app for tracking my runners' mileage at our school. The back end is in Java and the most intense operation for the server is getting the list of all the teammates, then finding the sum of their mileage for the last week and ranking them in order from highest to lowest. When I make a call to this endpoint while running on localhost, this took 11.5 seconds! I found with parallel streams it dropped the time to about 8.5 seconds, but I was still nervous that this was way too long. I uploaded the new JAR to the AWS EC2 instance and it takes less than a second! So my question is, why is my localhost (quad-core i7 7600k with 16 gb ram) so much slower than the AWS EC2 instance for the same operation? The EC2 instance connects to an RDS instance running MySql btw. Thanks!

答案1

得分: 0

我遇到了相同的问题。这可能是往返时间,而不是处理时间造成的。我的笔记本电脑在英国,而我所有的AWS服务都在us-east-1区域。

应用程序中的一个“动作”可能涉及对多个AWS服务的多次请求,例如将令牌发送到Cognito,从DynamoDB获取一些数据,然后根据第一个响应获取更多的数据等等。这都需要在英国和美国之间进行多次往返。您的服务器可能与您调用的服务部署在相同的位置,因此往返时间要少得多。

英文:

I get the same issue. It's probably the round trip time, rather than processing time. My laptop is in the UK, and all my AWS services are in us-east-1.

One 'action' in application might involve several trips to AWS services, for example sending a token to Cognito, getting some data from DynamoDB, then getting some more data based on the first response etc etc. It all involves several round trips from the UK to the US. Your server is probably co-located with the services you call so much less RTT.

huangapple
  • 本文由 发表于 2020年9月18日 20:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定