How Uber Used Golang To Build The Highest Query Per Second Service?
Golang is the fastest-growing programming language of 2021. The popularity of this programming language is exponential. Some of the most popular companies like the BBC, Soundcloud, Twitch, and many others use Go to develop their applications. Certainly, one of the top multinational ride-hailing companies, Uber, uses Go in many of its services.
In 2015, Uber created a microservice for Geofence searches and Uber's highest Query Per Second service (QPS) from the hundreds of other services that run in production. The following article outlines how Go helped Uber to build and scale the highest Query Per Second service.
However, to understand the concept, you should primarily start with the definition of GeoFence at Uber.
This image has an empty alt attribute; its file name is Uber-768x269_cbf1a1cb64e7911269a4518920c1f8d0.png
What is Geofence at Uber?
Geofence at Uber refers to the human-defined geographical area on the Earth’s surface to make geo-based configurations (Settings, Updates, Modifications, etc.). In the same vein, Geofence helps users by showing the cabs available at a particular location. This function defines specific areas (like airports) and implements dynamic prices in the areas where the demand is high.
When you search for a cab in Uber, your mobile needs to retrieve geolocation-based configuration. Uber uses these geo-location configurations to find out in which geofence your location is.
Firstly, this scattered and duplicated functionality was in multiple modules in a monolithic architecture. Considering Uber’s move from monolithic to a microservice architecture. So, they decided to centralize it to a new single microservice.
How Golang was useful at Uber
This image has an empty alt attribute; its file name is Golang-1-1.png
Node.js was the primary programming language of the team's real-time marketplace. Most of the programmers in Uber were Node JS experts with years of experience and knowledge.
However, they chose Golang for the following reasons and how it suited to cater to their unique requirements.
Go caters to high-performance and low-latency requirements
Every request from Uber's mobile apps needs Geofence lookups and quick responses. In this case, the system must respond in less than 100 milliseconds to hundreds and thousands of queries each second.
Intensive workload of CPU.
Geofence searches require CPU-intensive algorithms. While Node.js works excellent for Uber’s other I/O intensive services, it was not optimal in this case due to Node's interpretive and dynamic nature.
Non-disruptive initialization of the context
The QPS service conducts frequent searches and keeps updating in-memory geofences data from various data sources to ensure that the geofences data is most latest. Since Node.js is single-threaded, context refreshing will connect the Processor over a more extended period, triggering a spike in query answer times. It is not an issue for Go, as goroutines will run on several Processor cores and run background jobs parallel with foreground queries.
Geo Indexing with GO
To determine the given location’s latitude and longitude pairs where the position comes from other available geofences. The simple way to do this was to do a point-in-polygon search using an algorithm and go through all the available geofences. But this simple path was taking a long time. To borrow the space to search efficiently, Uber used a more straightforward observation-based road.
Certainly, Uber's business model is city-centric, the geofences and business rules are both correlated with the city.
Uber organizes its Geofences in a two-level hierarchy:
The first level - City geofences
The second level - Geofences within each city
With the help of this two-level hierarchy, a linear scan primarily scans the ideal city of all the city's geofences. It then locates the container of the geofences with another linear scan and inside that city.
Moreover, the services established needed to be stateless to offer services as per each request and anticipate a similar outcome. Each service instance should have the expertise of the entire system.
Uber developed a deterministic polling schedule for the synchronization of geofences data from service instances. Periodically context workers poll geofences data from different data stores. This data is stored in the main memory to serve requests and then serialized to the local file system for quicker bootstraps on service restarts.
how to build a uber eats clone
Uber's experience with the Golang Memory Model
The architecture required simultaneous read/write access to the in-memory geo index. Background polling workers had to be written to the index when the foreground query engine reads from the index. Idiomatic Golang's approach is to channel and synchronize read/write operations simultaneously using Goroutines and channels.
Initially, to handle the StorePointer Primitives from the sync/atomic kit, it was hard to preserve the code while using the read-write lock to synchronize access to the geo index.
To eliminate the lock contention, the team created a new portion of the index on the side before swapping it into the main index. This method of lock use has improved the latency of the demand. But simplicity and sustainability are the primary aspects of the codebase, so the limited cost of success was fair.
Why Uber Chose Golang?
Increased developer productivity
Easy Learning curve
High Performant in throughput
High Performant in latency
Uber uses Node.js & Python mainly. But, Golang is the language of choice for many of Uber Engineering's new offerings. There's a lot of traction behind Go at Uber.
To sum up, Uber uses GeoFence lookups to show its vehicle availability and implement dynamic prices according to the distances and demand. The GeoFence became Uber’s highest queries per second (QPS)service. Uber uses Golang to have high performance and latency in their application and their subsidiaries such as Uber Eats and Jump Bikes.
Talk To Us
Get end-to-end web application development solutions for an unparalleled back-end and front-end experience. With our top-of-the-line technology expertise, We provide you with the best software development that caters to various industries and clients worldwide.
Find the approximate cost to build your Application