Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ravendb long-running sessions

We use RavenDB for our site. The site loads all its data into memory (not very much) to be able to handle massive loads. We load the data in a background-thread which regularly checks with the DB (RavenDB + sqlserver) whether any new data is present and if so loads that data into memory.

We have tried a lot of stuff to get around the annoying request limit of 30 queries into RavenDB per session. Since Raven doesn't have any mechanism to "reset" the session after we're done with one iteration of the check/load loop, and since there's no way to tell Structuremap we really want a new session even though we're still the same thread as before we're kind of stuck.

In the end I have rearchitected so that our repositories now use a RavenSessionProxy which structuremap loads for us, which can be reset by the load/fetch loop, (whichinstantiates up a new documentsession manually when we reset it).

Is this really the only way? Isn't there any mechanism within Raven to say "hey mr Session, I'm done with you for now, go flush yerself and be fresh and ready next time I call on you)" or to tell Structuremap "Hey, SM! Next time I ask you for a IDocumentSession, bring me a new one, I'm tired of this old one"

like image 541
AndreasKnudsen Avatar asked Dec 30 '25 01:12

AndreasKnudsen


2 Answers

As Ayende has pointed out, sessions should be short lived. Instead of your background job taking a dependency on a session, have it take a dependency on IDocumentStore and then create/dispose of the session for each run. IDocumentStore can be a singleton inserted into the container at startup.

like image 125
Phil Degenhardt Avatar answered Jan 01 '26 12:01

Phil Degenhardt


AndreasKnudsen, RavenDB session are designed to be relatively short lived. If you need to keep them around for a while, you are probably doing something wrong. Note that RavenDB is already doing a lot to make sure that it is speedy, so loading stuff into memory isn't required.

You can set session.Advanced.MaxNumberOfRequests, which would increase the amount of requests you can do, but it also mean that you'll be keeping more stuff in memory.

like image 21
Ayende Rahien Avatar answered Jan 01 '26 10:01

Ayende Rahien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!