New contribution - production-worthy in-memory backend with no external dependencies
We would like to contribute an improved implementation of in-memory backend for JanusGraph.
Background/Rationale There are many possible applications for both embedded and standalone JanusGraph with a 100% in-memory backend – i.e. cases where the graph can potentially fit within the boundaries of one JVM, is built dynamically and in parallel transactions, and the performance during build-out of the graph and querying it is critical. A quick search in the issues and mailing list seems to confirm that there is general interest in this kind of use case, both for JanusGraph specifically and for an open-source in-memory graph in general (e.g. TinkerGraph may not fit the bill in all cases as it is not transactional etc).
However there seems to be a bit of a gap in JanusGraph offering in this space. Current implementation of JanusGraph’s in-memory backend has not changed since Titan times, and is still declared as “for testing only”, “not ready for production use”.
Contribution We have done some analysis which shows that the main obstacle for production use of current in-memory backend is enormous overhead when storing key values (likely a trade-off vs simplicity of implementation for something that was only intended for testing purposes). Quite often the total overhead of wrapping data structures and references is bigger than the actual data being stored. After a series of improvements this overhead was significantly reduced (e.g. 2x-5x depending on the “shape” of the data stored and the size of individual data entries).
Our locally modified version of in-memory backend is successfully used in production, handling up to 70+ concurrent read/write transactions at any one moment, 10+ millions of vertices (of which quite a few have up to 60 properties) and about 3x more edges, within one JVM.
Further analysis and memory footprint/performance comparison details to be posted in a follow-up comment.
Analysis details To illustrate the difference, I wrote a small program which populates an in-memory graph with some mock data and then queries it (included in the PR #1483 ).
Memory footprint comparison Attached are the screenshots of a 2-minute flight recording captured while the test program was populating the graph.
We can see that, because each individual piece of data (byte[]) is wrapped in Entry object and is stored directly in a sorted Entry[] array, this creates 6 million individual byte[] refs and StaticArrayEntry objects, totalling around 1G of heap mempry, per about 260k ColumnValueStores. Overall heap usage graph shows that the “baseline” of used heap after GCs (basically, old gen heap) grows steadily from 0 to about 1.5G in line with the above observations.

In contrast, with “compact” in-memory backend storing all data in a single shared byte[] per column and creating Entry wrappers on the fly when asked, the stats look much better. For about 317k ColumnValueStores it managed to create within the same 2 mins, it created about 88M of byte[] arrays, and 32M of int[] for indexing inside the shared byte[] arrays, with the rest of overhead comparable to the “classic” case. The usage graph confirms that the "baseline" is now <500M.

So new “compact” backend took around 100M of heap to hold 317k columns vs the “classic” implementation taking 1G to store 260K columns.
Performance comparison Using the same program, I timed graph creation and querying using the two versions of in-memory backend, for different volumes.
On relatively small volumes, they show comparable performance, with writes slightly faster and reads about 1.25 time slower in “compact” version. Slightly slower read performance is to some extent expected as it has to create Entry wrappers around the data for results on the fly, whereas the “classic” implementation is returning them directly. However it seems like a fairly small price to pay for drastic improvement of memory footprint.
100tx * 100 vertices * 20 properties, link every 20th classic run1 Write: 21003ms, Read: 2920 run2 Write: 22368ms, Read: 2931
compact run1 Write: 20485ms, Read: 3743 run2 Write: 21965ms, Read: 4020
This is illustrated on bigger volumes, where “compact “ version “outperforms” the classic one by a large margin, most probably due to frequent GCs when running "classic" backend resulting from much higher memory footprint.
1000tx * 1000 vertices * same classic Write: 231558ms, Read: 10214 (probably due to GC-related delays)
compact Write: 205052ms, Read: 3702
NOTE: the PR #1483 currently suggests adding the new backend rather than modifying the existing one, as it seemed cleaner and easier to compare performance within one codebase branch. However, having read recent discussions about adding more backends into main repository vs maintenance costs, I am starting to think that actually modifying the existing in-memory backend could be a better idea. This is because:
- the modifications are fairly straightforward and do not change the general structure of existing backend
- don’t add too much extra code or any external dependencies
- don’t require any additional documentation or setup instructions, at least initially
- all existing (and a few extra) integrity tests pass and using it in place of current version is unlikely to create any issues for current (test-only) uses
- the same backend will simply be fit for production use, no new backends to maintain
Some thoughts
BerkeleyDb may work as in memory store https://github.com/JanusGraph/janusgraph/issues/1128 and has DbLoad/DbDump utils. Additionally bdb has compression too
JanusGraph has in-memory store backed by aerospike https://github.com/Playtika/aerospike-janusgraph-storage-backend
JanusGraph may be based on IMDG (in memory data grid). Some implementation exists but outdated https://github.com/thinkaurelius/titan-experimental
I didn't check performance and memory overhead for
Thanks for the thoughts @mad . Correct me if I am wrong, but they seem to revolve around the idea that we don't really need any improvements to the existing in-memory backend, we should leave it alone, in "test-only" mode, and direct people wanting in-memory graphs in production to other backends?
This is a fair enough point of view!
However I would like to collect a few more opinions as to whether this one is a useful contribution or not, and if yes - what needs to be done to accept it. I think I will offer a few options for people to "vote" for in a separate post/email to dev group, in the hope that it will encourage people to take an opinion.
Specifically on your suggestions:
Aerospike backend and IMDG backend, and indeed any other possible solutions based on out-of-process caches and grids, i.e. Redis etc - sure, they may prove to be good options for some, and provide better scalability/functionality than the suggested "improved" in-memory backend. However these come at a cost - cost of setting up a separate grid behind the JanusGraph engine, cost of maintaining and updating the associated external dependencies, and in some cases - direct costs of licensing these solutions for commercial use.
Compared to that, cost of adding a little bit more complexity to existing in-memory backend, without adding any new external dependencies and capable of running right out of just the janusgraph-core package, is arguably quite small. Or at least this was the conclusion of my colleagues a few years back who did the initial cost and performance analysis, and chose Titan+inmemory backend as a result.
Just to reiterate, this contribution leaves most of the logic and implementation of existing in-memory backend intact, it just improves it slightly and adds a few more features helpful for debugging etc, with the result of it becoming capable of running in production and serving tens of millions of vertices. It certainly took much less time (at least calendar time) to develop it than to get it this far in the contribution process :), so we shouldn't overestimate the complexity of the change or associated risks vs its benefits.
BerkeleyDb in "memory-only" mode - this one is actually interesting. Browsing our project history, I found some mentions of the default bdb disk-based backend configuration being considered much slower than in-memory in their tests, and also some licensing considerations which may have led to discarding this option. However I found no mention of testing "memory-only" configuration, so I have ran a quick test based on the same simple test utility as used for comparison above. I haven't performed any memory footprint and scaling analysis yet, and no tests with production-like parameters. Will probably only get time to do it if licensing checks pass. But just looking at basic performance measurements, memory-only bdb backend seems to show roughly same or slightly better performance! Update: I have no recent experience with BerkeleyDB JE, so not sure how it behaves in long-running processes, but this quote from SO makes me think that actually there might be more to investigate:
Note this proviso in the docs: "The system operates until it runs out of memory, at which time an OutOfMemoryError is thrown. Because the entire log is kept in memory, this mode is normally useful only for testing."
So regardless of what happens to this contribution, I would vote for revisiting the #1128 you mentioned, verifying what happens to the heap after say a couple of days of production-like environment i.e. executing many transactions per second, and if indeed it is possible to use as 100% in-memory backend.
Any further thoughts welcome.
Hi All,
thank you for all the comments so far on the issue, PR and in the dev maillist. Unfortunately I was not able to obtain from these comments a clear understanding of how to proceed with this contribution, and there doesn't seem to be a lot of traffic on it in general. I would really like to get a better understanding of where we stand in general before I proceed to address the cosmetic/style/refactoring comments.
So I have collated a few options based on the feedback so far, and I would like to ask everyone, especially the previous commenters and also preferably active developers/TSC members to look through them and cast their "vote" as comments on this issue page.
Hopefully the fact that one just has to choose an option will increase participation :), but if you have more to say on the matter than just choose from the options, then by all means please do so.
1. I think we should leave in-memory backend as a basic "test-only" implementation whose main quality is simplicity. Any users wanting to use JanusGraph for commercial/production purposes without a disk-backed storage should be directed to other custom backends maintained separately from main Janusgraph project. In other words - I think we should say thanks, reject this contribution and close the issue, no hard feelings. 1a. + Maybe open another issue to improve the documentation and point people to alternatives.
2. I think the description of improvements sounds useful, and it would be good to "upgrade" our in-memory backend and offer Janusgraph users an embedded in-memory backend fit for production use. I have read the analysis summary, checked the logic, [ran some tests to verify] and I am generally happy for it to be approved once all the cosmetic/style/technical comments are addressed. In other words - please address those existing comments and I will approve.
3. I think the description of improvements sounds useful in general, and it would be good to "upgrade" our in-memory backend and offer Janusgraph users an embedded in-memory backend fit for production use. However I don't have time/interest/expertise to dig into the logic and verify the performance/memory footprint claims. So I propose to wait until somebody has done it. In other words - please be patient, the core logic and analysis will get reviewed sooner or later, and if they are proved correct, we will take it from there. 3a. + actually I think you should address all the technical comments before you can hope for an in-depth review
4. something else - please elaborate
Thank you, Dmitry
@dk-github Thank you for your work and really sorry that the logic isn't reviewed yet. It happens when there are no available commiters who can review the logic. In most cases commiters have knowledge only of a specific scope in the code and it is hard to review the logic when you are not familiar with another scope of the code. Personally I didn't work with in-memory backends for JanusGraph, so it would be hard and time consuming for me to review the logic. I think that if you can support this new updated backend for JanusGraph then I would vote for 2nd option. We can just review the code from technical side, you resolve all those cosmetic/style/technical comments and we can merge it with the hope that you will be able to maintain it.
About 2 options suggested adding a new backend or updating the existing one.
If a new backend is just a better version of the old backend and it is backward compatible to the old one (i.e. all current parameters / queries / configurations / etc. will work for the new backend) then I would suggest to update the existing backend because there will be no sense in maintaining the old backend which is just a worse version of the new backend.
If the new backend has some breaking changes or the old backend may be better to be used in some situations then I would be interested to hear those breaking changes or those use cases. If community decides that they need the old backend then we should add a new backend in such case.
In short, I think if everything is OK with backward compatibility to the old (i.e. current) in-memory backend then I would suggest updating the current in-memory backend, resolve cosmetic comments and we are good to merge this PR.
It is my thoughts, maybe other members have something to add.
@porunov, thanks for the feedback.
Just to confirm - after some earlier comments I have already updated the PR to make changes directly to the existing backend rather than create a separate backend aside the old one. There are no breaking changes, and last time I checked, all the tests passed with new implementation.
It was requested elsewhere that we need to move existing in-memory implementation out of the core into a separate project, so I will wait until this happens and then rebase my PR there and move forward with addressing the comments.
Fixed in #1483
@dk-github Thank you for your contribution! I've added a new issue to track documentation parts: #1929