Context
I am building an application for searching places 'Nearby' akin to Yelp/Google.
Objective for this question
Recommend a strategy to do paginated searches based on location and ask for advice on the same. Further, ask for frameworks and tooling that support the design.
Please note: I do not want to complicate this by doing any form of ranking, etc. This is simply 'Nearby' search.
Architecture
Requirement: User opens the app and requests restaurants in a 5 mile radius. The server may send 20 results back. User may exhaust these 20 results and request for another set of restaurants. The server may send another 20 results (that are not duplicate).
Solution
I propose a strategy that is based on 2 simple data stores - Redis to enable Geospatial Search & a generic data store for storing Restaurants (MongoDB/PostgreSQL).
Client (1) -> Server (2) -> Redis (3) -> Mongo (4) -> Client (5)
- Client sends a request (REST/GraphQL) to search for restaurants nearby;
params: location, distance, skip: 0
. - Server handles the request and fires up a request to Redis to get 'locations' that match the query.
- Redis stores data like "location -> restaurantID". Redis does a spatial search and finds all locations that satisfy the query.
- Server then takes the first 20 in this list and batch queries Mongo to get a list of restaurants and sends it back to the client.
- Clients handle any deduplication as I am unable to understand how to enable cursor based pagination for search.
For the next 20 results:
- Server skips the first 20 from Redis and handles pagination accordingly.
Considerations
- Simple indexing built without cost for exhaustive search indexing.
- 2 roundtrips to datastores per query.
- Same query for redis for pagination
- Don't know if possible but short-term redis caching may help.
- Current backend:
- NodeJS + GraphQL + Prisma + MongoDB
Requires Advice On
- Is this conceivable for a medium sized production solution?
- Do these roundtrip costs make this service 'slow'?
- How to achieve pagination generally for geospatial searches? Is there a form of cursor based pagination when it comes to searches?
- What are other ways to accomplish the same? Happy to read through more complex topics and build a slightly more complicated solution.