Badoo's mission is to help people find new friends around the world. To do this, on our website there are many tools and services that allow the user to stand out among others and increase their popularity. One of them is Spotlight service, in the Russian version - “Spotlight”. It is present on almost all pages of our site and is a block of 12 photos. The concept of the service is such that the user sees photos of the geographical neighbors closest to him. He can send a request to appear among them and his picture in the first place on the left. Then all the others move to the right, and the last one is eliminated.
How does this, at first glance, simple service, read on ...
To date, Badoo supports more than 30 languages, including right-to-left languages ​​(RTL languages: Hebrew, Arabic, etc.). This support is not limited to the translation of text materials, but also includes the optimization of interface elements. Our web technologists have tried to ensure that Spotlight is no exception. In the case of RTL languages, it is mirrored, and the photos move from right to left.
')
For each country, the central cities are determined, to which all the others adjoin, forming districts. The central cities are those whose population exceeds a given threshold, determined empirically by each country. Districts usually intersect with each other, and a user from one district can enter simultaneously several others. According to the algorithm, the nearest district is initially searched, the distance to it is calculated - D. Then we find all other districts, the distance to which is no more than D + X km, which is also adjusted individually for each country.
Some central cities are set manually, for example on islands. Districts for all localities are selected exclusively within the countries in which they are located. The city on the border will not fall into the district of a foreign country, although technically nothing prevents us from doing this within the framework of each continent. Below you can see the visualization of the districts of France:
On the technical side, the main part of the Spotlight service is a C program - a daemon that receives applications, distributes them into districts and rotates users. Several Spotlight daemons work simultaneously on our servers, distributing the load among themselves. Each of them is responsible for their own set of countries and a group of users of a certain sexual orientation. The latter is another advantage of the service, because users see only those who are interested in them, and not everyone. As an example, the following table:
A young man from Paris who wants to get acquainted with a girl will get to server number 1. At the same time in the block of photos, he will see men and women from the capital district with the same interests.Only those who have at least one photo approved by our moderators can get into the Spotlight, but we have tried to make this process as quick and invisible as possible. Even if the user has just uploaded his photo, she will be sent to the accelerated extraordinary moderation. It will take no more than a minute as he sees himself in the first place among other photos. If the user deletes the photo, it will immediately be reflected in the service - among all his photos, the first one that is suitable for replacement will be found. For all current applications are constantly checked the relevance of the photos and profile. For example, if a user violates the rules of the site and is blocked, his application will be deleted.
Spotlight was originally based on Memcache, and the MySQL database was used as a permanent store of requests. The front-end of the JavaScript service requested updates directly to Memcache through a special module for the nginx web server. PHP scripts were fully responsible for processing applications, which processed the queues in the database and updated the data in Memcache. Then users were grouped exclusively by country and sexual orientation, so they saw people from all cities of their country at once. To make the service faster and more flexible, as well as expand the functionality, it was decided to write a special demon in C.
One of the interesting tasks that we had to solve was balancing the number of men and women in Spotlight. In many cities, men for a number of reasons, use the service much more actively than girls. It was necessary to balance the ratio of girls and young people so that the number of the first was at least 25%. To do this, enter the invitation for free to use the service.
On each page where there is Spotlight and the current user fits certain requirements, we calculate the number of empty seats, missing up to 25%. If there is a necessary amount of free places, we show an invitation to a female user. The difficulty of the task lies in the fact that the number of invitations is limited (we cannot show invitations to the whole site at the same time), and the user can think about it for some time and subsequently refuse the service.
The first thing that comes to mind is to check availability with each request. If they are, then show the invitation. Suppose that we have 10 places and we begin to invite all suitable users, which may be, say, 1000 at one time, and they all agree. And then the next minute, site users will see on the page a machine-gun burst of photos. Therefore, it is necessary to keep records of free places and correctly handle simultaneous requests. To do this, use Memcache.
The algorithm is as follows:
- With each request for a page with Spotlight, we get a list of existing applications. We take statistics on the number of users (M \ F), on the basis of which we determine the number of possible free invitations.
- If the user fits certain criteria, try to immediately take a place in the service. We save not the number of free, but the number of occupied places, making the key increment used_slots_ <area_id> in Memcache. There are exactly as many keys as there are available counties. If the value of this key is more than the number of free places, then decrease the value back by one and do not show the invitation. The key is stored for 30 seconds and is responsible, in fact, for the frequency of balancing.
- If it was possible to book a place, we show the user an invitation and put the corresponding flag in the session. We are waiting for the decision of the user.
- In case of user failure, we are decrementing.
It is quite possible that the user closes the page without performing any action. In this case, we will have an issued, but not realized invitation. This is not a problem, because balancing happens quite often - as soon as the key expires in Memcache.
The essence of Spotlight as a paid service is that the user receives the attention for which he paid. The larger the settlement, the more willing to use the service. But, on the other hand, there is more competition for the time during which the user's photo will be in the very center of attention. The service will not let the photo disappear quickly, but it will not stand still if there are too few applications. Thus, a balance is achieved between the money spent and the number of users who can find each other and want to meet.
Alexander
Treg Treger, developer of Badoo.