Performance issue in getting nearest location in realtime app

97 Views Asked by At

Currently, I'm working on a project like Uber. It means there are two apps: one for driver and one for customer.

The issue is: the driver need to update their location per 2 seconds. And the customer pull all the nearest drivers per 2 seconds for realtime. It cause bad performance in database query. I use cube & earthdistance extension of PostgreSQL for caculating the nearest.

Could anyone show me what's the best way to solve this issue? Thanks a lot!

1

There are 1 best solutions below

0
wolframhempel On

You could use deepstream.io for realtime data submission, e.g. from Android

public void onLocationChanged(Location location) {
  // get the record
  Record record = client.record.getRecord( "driver/14" );
  // any kind of data path works
  record.set( "position.x", location.getLongitude() );
  //as well as any kind of serializable datatype
  record.set( "position.y", location.getLatitude() );
}

to JavaScript

// get the record
var driver = client.record.getRecord( 'driver/14' );

// subscribe to any changes within position
driver.subscribe( 'position', function( position ){
  updateMarker( position.x, position.y );
});

Combined with e.g. RethinkDBs geospacial queries which deepstream integrates with this might be a scalable solution.

Here's an example repo demonstrating this