Realm-java: Add multi process support

Created on 6 May 2015  路  22Comments  路  Source: realm/realm-java

Currently Realm for Java only support multi threading but not multi process.

T-Feature

Most helpful comment

@cmelchior Any update on this? On iOS it is already working...

All 22 comments

Relevant Realm-Cocoa issue is here: https://github.com/realm/realm-cocoa/issues/824

Main issues:

  • Make sure that schema validation/initialisation is happening inside a write transaction to prevent other processes from doing the same, ie. grab write transaction before querying on schema.
  • Interprocess notifications. What is required to make this work on Java?

See https://github.com/realm/realm-cocoa/pull/2877

We might want to consider waiting to implement this until we have ObjectStore support.

@cmelchior Any update on this? On iOS it is already working...

@leisim We made a PR some months ago https://github.com/realm/realm-java/pull/1300
It is not merged since it would have more fundamental changes and more testing.

Can you please share your use case with us? It would be helpful for us to build more test cases. Thanks!!

@leisim If you are considering this because you want to use SyncAdapter. I originally starred this issue because of it but, I instead changed my implementation with GCMNetworkManager (Uses JobScheduler for newer API level). It requires less boilerplate compared with SyncAdapter. Hope that helps you.

@beeender I created multiple apps for my company which extend each other, but they can be installed in various combinations. (Every employee has different apps)
But they all rely on the same data and it would be really convenient if they could just use the same database...

Thanks for your hard work btw ;)

@beeender I am using this with react-native. My project needs background android service, that runs some js and captures some data into realm. And there is also an activity with react-native ui, that should be able to display results from realm. I run the service and activity in different processes, as I've had some problems creating two react-native contexts in the same process.

@kabbi So you are using the Realm React Native as well?

@beeender Yes. Forgot to mention this.

@alazier This would be an interesting and quite useful use case. User can use our notification system work across native android and react native.

@beeender is there still progress on this feature?

I also would like to use the same realm files from a service.

@rimeissner After #3370 merged, our master branch actually supports inter-processes notifications. You can try it with the snapshot release.
There are some corner cases:

  1. The Realm.deleteRealm() doesn't check if the Realm is opened in another process right now.
  2. Migrations happens in another process might not be handled well.
    So you may have to take care of those if you are going to use it in multi-processes.

So before we solve above issues, there are risks to use it in multi processes.

Also

  1. master branch should support notification between realm-java/realm-dotnet/realm-reactvie , but we need more tests on them.
  2. encrypted Realm is still NOT supported in multi-processes.

Is there any progress on this? We're using a remote service, too.

@MariusBoepple Our current release is actually supporting notifications through multi-processes, but there are some limitations to use realm-java in multi-processes applications:

  • Encryption is not supported for multi-processes.
  • Realm.deleteRealm() is not multi-processes safe.
  • Migrations are not handled well. So you need to avoid migrating happens while there is Realm instance opened in the other process.

If you have two two processes running and you can guarantee that the parent process has an active Realm realm instance (since that would run all required migrations), then Realm should be process safe right now, but as @beeender mentioned there are also other edges cases. This is the primary reason we don't claim to be interprocess safe just yet.

@cmelchior would object store schema integration fix cross-process migration?

It would probably get us close enough that we choose to go the last mile. Encrypted Realms and using Realm.deleteRealm() will not be supported in the first iteration though.

@beeender as you said:

Migrations are not handled well. So you need to avoid migrating happens while there is Realm instance opened in the other process.

I'm making use of a FileLock before getting instances of Realm to avoid having multiple Realminstances opened across processes while a RealmMigration is running.

However, I noticed that the RealmMigration always runs in the main application process, even if the Realm instance being created is from a different process. Is this something I should be worried about, or I'll be fine as long as I don't have multiple instances opened while migrating?

Thanks in advance!

This is definitely something I would worry about because I feel like that might be related to what would explain the IncompatibleLockFile exceptions?

Then again, soon the object store will handle table creation and all this will be solved.

@Zhuinden I'm locking on a file other than the Realm database file. Do you think I could still have issues?

@jpmcosta

However, I noticed that the RealmMigration always runs in the main application process, even if the Realm instance being created is from a different process.

The migration will happen when the first process create the Realm instance. The migration itself is actually protected by our write locks among processes.

The risk is if your two processes have different schemas, that would be problematic.

We do support officially support multi-processes since 4.2.0
https://realm.io/docs/java/latest/#multiprocess

Was this page helpful?
0 / 5 - 0 ratings