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条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法