I'm using mongodb in an enterprise application and for some use cases, I have to use transactions.
It seems like at first I wasn't using mongodb's
ClientSessions correctly. Each time I was trying to start a transaction, I would also start a session but this turned out to be a mistake. At one point when I was testing the app with multiple concurrent requests, I got
WriteConflict errors which apparently means I was executing concurrent transactions on a single collection on two separate sessions.
So now it seems that for each tenant of our app, I need to have a pre-created session and any time I want to execute a transaction, I just use that existing session. But it seems like there can only be one active transaction on a session, which means I need to also maintain a queue for my transactions so that I can execute the next transaction as soon as the previous transaction is done and the session is free. But this will make some parts of the app really slow.
So my question is, is there any alternative to this approach so that I can use transactions but also be able to respond to requests concurrently so that I don't get a performance penalty?