System Design Interview: Yelp, TripAdvisor or nearby Places
In this article, we will discuss about Proximity Service. A Proximity Service is used to discover nearby places such as hotels, museums or restaurants. It is core component that powers the features like finding the best restaurants nearby on Yelp or finding k-nearby gas stations on Google Maps.
Step 1:- Requirement Gathering
Functional Requirement:-
- Returns all the businesses based on user’s location. Business user can add, delete or update the business, but information doesn’t need to be reflected in real time.
- Customer can view detailed information about a business.
- User can change the radius from UI.
Non-Functional Requirements:-
- Low Latency:- User should be able to fetch the businesses quickly.
- High Availability:- System should be able to handle the traffic during peak hours
- High Scalability:- System can be scalable across multiple region and availability zones.
Back-of-the-envelope Estimation:-
Assume we have 10 million daily active users and 20 million businesses. We can round up seconds in a day to 10⁵. Assume a user makes 5 search queries per day.
Search QPS = (10 million * 5)/10⁵ = 500 QPS.
Step 2:- High Level Design:-
API Design:-
- Search nearby businesses:- This endpoint returns businesses based on search criteria.
Request Parameter:-
a. latitude
b. longitude
c. radiusResult:-
{ "total": 10,
"businesses: [{business objects}] }
2. API for business:- The APIs related to business object for CRUD Operations.
API Detail
----- ---------GET:- /businesses/:id Return Detailed Info about business
POST:- /businesses Add a new Business
PUT:- /businesses/:id Update details about business
DELETE:- /businesses/:id Delete a Business
Data Model:-
Read/Write Ratio:- Read volume is high because of the following two reasons.
- Search for near by businesses
- Detailed information about individual business.
On the other hand, write ratio is very low as compared to read ratio because updating business information, deleting a business and adding a new business is in-frequent.
For a ready heavy system, a relational database system such as MySQL can be a good fit and it is tried and tested technique in industry.
High Level Design Diagram:-
The system divides in two primary components:-
- Location Based Service.
- Business Related Service.
API Gateway:- The API Gateway acts as an entry point in our system and routes the request to corresponding components/microservices.
Location based Service:- The LBS is core part of the system which finds the nearby businesses for given radius and location. It is read-heavy service with no write request, QPS is high in peak hours in dense areas. Moreover, this service is state-less so it will be easy to scale horizontally.
Business Service:- The Business service mainly deals two type of the requests.
- Customers wants to see detailed information about business. QPS is high during peak hours.
- Create, Update and Delete the businesses. Those requests are mainly write operations, QPS is not high.
Database Cluster:- The Database cluster can be set-up in primary-secondary setup. The Primary Database handles all the write operations. Data is saved in primary database and then using DB Replication technique, replication of the database created. Due to replication delay, there might be discrepancy between primary and replicated database, but this inconsistency is not an issue, as we don’t have to reflect the changes in real-time.
Scalability:-
Both the LBS and Business Service are stateless services, so it’s easy to automatically add more servers during peak time and remove the servers during off-peak hours. If the system operates on cloud, we can set up different regions and availability zones to further improve availability.
In the next article, we will discuss about different approaches to search the nearby places using various algorithms, Caching, Scaling the database and Final Design Diagrams.