Spring data mongodb or java drivers?

2,130 views
Skip to first unread message

Abhi

unread,
Apr 28, 2014, 5:51:00 AM4/28/14
to mongod...@googlegroups.com
Hi,
Should i use spring data mongodb or mongodb java driver for development? Will there be too much performance overhead in case of spring data?
How stable is spring data for mongodb?
What are the pros & cons of choosing one over the other? 

Thanks,
Abhi

Trisha Gee

unread,
Apr 29, 2014, 7:00:28 AM4/29/14
to mongod...@googlegroups.com
Hi Abhi,

Both are perfectly good ways to interact with MongoDB.  If you're already using Spring in your application, it makes sense to try out Spring data, it's a project that's being actively supported by the Spring guys and is used by a large number of projects.  Alternatively, if you want lower-level control of your interaction with the database, you can use the Java driver directly, but you'll have to manually convert the data that comes back from MongoDB into your own application objects (whereas Spring data does a lot of this magically for you).

It's worth pointing out that Spring data uses the MongoDB Java Driver under the covers, and if you need to get to the lower-level driver you can get to it via Spring, so you can use both (if you need to).

As for performance, this really does depend - on your application, your data, your usage, your configuration.  The best thing to do here is to write some performance tests that reproduce the behaviour you'd expect to see in "real life" (e.g. data structure, usage like number of reads vs number of writes) and compare the two approaches against your non-functional requirements.  Remember that the fastest is not necessarily the best - if both meet your speed requirements, you might pick the slower solution if it gives you something else (e.g. less development time required)

Hope that helps,

Trisha

Trisha Gee

unread,
Apr 29, 2014, 7:02:22 AM4/29/14
to mongod...@googlegroups.com
Hi Abhi,

Both are perfectly good ways to interact with MongoDB.  If you're already using Spring in your application, it makes sense to try out Spring data, it's a project that's being actively supported by the Spring guys and is used by a large number of projects.  Alternatively, if you want lower-level control of your interaction with the database, you can use the Java driver directly, but you'll have to manually convert the data that comes back from MongoDB into your own application objects (whereas Spring data does a lot of this magically for you).

It's worth pointing out that Spring data uses the MongoDB Java Driver under the covers, and if you need to get to the lower-level driver you can get to it via Spring, so you can use both (if you need to).

As for performance, this really does depend - on your application, your data, your usage, your configuration.  The best thing to do here is to write some performance tests that reproduce the behaviour you'd expect to see in "real life" (e.g. data structure, usage like number of reads vs number of writes) and compare the two approaches against your non-functional requirements.  Remember that the fastest is not necessarily the best - if both meet your speed requirements, you might pick the slower solution if it gives you something else (e.g. less development time required)

Hope that helps,

Trisha

On Monday, 28 April 2014 11:51:00 UTC+2, Abhi wrote:

Abhi

unread,
Apr 29, 2014, 8:00:13 AM4/29/14
to mongod...@googlegroups.com
Thanks for the reply Trisha.

Right now I am working on generating and putting a test dataset into mongodb. The size of test data will be 1 TB. I will be using multiple threads to generate and insert the test dataset. 
For this task using spring data will be fine? Is there any chance of running into thread safety issues with spring data?
Or should i use java driver as it is thread-safe and support for the new bulk api?

Will inserting test dataset into mongodb with spring data take too much time as compared to using java driver?

Thanks,
Abhi

Trisha Gee

unread,
Apr 29, 2014, 10:17:08 AM4/29/14
to mongod...@googlegroups.com

"Will inserting test dataset into mongodb with spring data take too much time as compared to using java driver?"
Writing a simple performance test using both methods should give you the answer to that question, but be aware that you also need to understand what "fast enough" is. In high-frequency trading, we're talking nano-seconds, but for overnight batch jobs we might be talking minutes or hours. Find out the performance requirements first.

Secondly, since Spring Data is based on the Java driver, it's using the same thread-safe methods to access the database, so I can't foresee a problem, but I would check the documentation, stackoverflow and, of course, Google if I were you.

Finally, you mention the new bulk API - if this is what gives you the best performance, according to your tests, and Spring does not yet make use of it, then maybe you do need to use the driver itself. But honestly, no-one can answer the question "which is fast enough" except you - find out what "fast enough" means, and write some simple perf tests to compare the two approaches.

Trisha
Reply all
Reply to author
Forward
0 new messages