douniang3866 2017-04-04 10:55
浏览 53
已采纳

构建具有高可伸缩性的RESTful API [关闭]

I'm looking to build a RESTful API that would be in charge of inserting datas based on the data sent by multiple mobile apps (stored in an Amazon redshift database).

I've already developed an API that you can find here: https://github.com/Noeru14/fms. It uses Gin: https://github.com/gin-gonic/gin. If I opened too many parallel connections, it used to crash / not work properly.

A friend of mine has talked about using Node instead as it allows to have really short client-server interaction.

I'd like to know which factors I'd have to take into account to build a RESTful API that could handle up to hundreds of thousands requests per second with Go and Node. Do you also know if it's doable in Golang ?

Thanks a lot.

  • 写回答

2条回答 默认 最新

  • doushuo8677 2017-04-04 11:24
    关注

    It is doable in Go and it is doable in Node. It is doable in other languages like Erlang or Python as well. But since you're asking about Node then this is what I'll answer about.

    The most important thing for high concurrency in Node is to never block the event loop or do any blocking operation ever (unless it's the first tick of the event loop). This is the number one thing that people do and ruin the concurrency - like putting a little innocent-looking fs.statSync() now and then (see this answer for examples of such mistakes right in the answers on Stack Overflow). Using any blocking operation (including long running for or while loop) after the first tick is always a mistake and in fact I think it should throw exceptions.

    Another thing that while not being an outright mistake in all situations may still harm the scalability is storing any state in your application. If you need to have any persistent state (and try to minimize that need at all cost) then you should use a database for that. For data that needs to be shared between requests quickly like session data you should use a fast database like Redis, but only if you cannot achieve the same with things like JWT etc.

    Prefer horizontal instead of vertical scalability, because at some point there will be no bigger server, but there will always be more servers.

    To sum it up:

    1. Never block the event loop
    2. Do all CPU-heavy computations in external processes
    3. Never block the event loop
    4. Use Redis or Memcached for shared state
    5. Never block the event loop
    6. Use clustering for horizontal scalability
    7. Never block the event loop

    Did I mention never blocking the event loop?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题