Should I use GenServer for this?

I would not use GenServer for this. What happens if the GenServer crashes before its batch is dispatched? Also, running multiple app instances would require your app to know on which node a specific GenServer is running, making it more difficult to scale horizontally. In general, I would avoid singleton GenServers (in your case, the correspondence between store and GenServer) unless there’s a good reason for them.

One possibility would be to save orders in the database, and grouping “lazily” upon receiving an order. Something like this:

  • Save orders in the database as soon as they are placed, including all the data necessary for batching
  • When a new order is placed, compute its batch. If the batch can be considered complete (for example because the max number of orders per batch is reached), then dispatch it. Otherwise, schedule a job after an appropriate timeout to batch it anyway in case no orders for the same batch were placed within the timeout.

The advantage of this is that all the state stays in the database, so the app can be trivially scaled, and a crash of a GenServer will not loose orders.

Does this make sense, or did I misunderstand your problem?

3 Likes