2

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.

azekirel555
  • 577
  • 2
  • 8
  • 25

2 Answers2

5

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?

rsp
  • 107,747
  • 29
  • 201
  • 177
1

First of all using the "net/http" package has everything you need to impelement a simple router and even custom middlewares (like auth), using routing frameworks for simple routing is not very efficient. And you could design the application to support multiple instances on multiple servers.

Here is an example of mine in Go: https://github.com/efimovalex/EventKitAPI/tree/master/consumerapi that uses an in memory worker pool

Alex Efimov
  • 3,335
  • 1
  • 24
  • 29