Be the center of attention!

    Badoo's mission is to help people find new friends around the world. For this, there are many tools and services on our website that allow the user to stand out among others and increase their popularity. One of them is the 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 so that among them appears his photo in the first place on the left. Then all the others are shifted to the right, and the latter is eliminated.

    How this seemingly simple service is structured, read on ...

    Today Badoo supports more than 30 languages, including languages ​​with the direction of writing from right to left (RTL languages: Hebrew, Arabic, etc.). This support is not limited only to the translation of text materials, but also includes optimization of interface elements. Our web technologists have made sure that Spotlight is no exception. In the case of RTL languages, it is mirrored, and photos move from right to left.

    For each country, the central cities are determined, to which all the rest are adjacent, forming districts. The central cities are those whose population exceeds a predetermined threshold, which is determined empirically for each country. Districts usually intersect, and a user from one district can simultaneously get into 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 individually configured for each country.

    Some central cities are set manually, for example on the islands. Districts for all settlements are selected exclusively within the countries in which they are located. A city on the border will not fall into the okrug of a foreign country, although technically nothing prevents us from doing this within each continent. Below you can see the visualization of the counties of France:

    On the technical side, the main part of the Spotlight service is the C program - a daemon that accepts applications, distributes it among districts and rotates users. Several Spotlight daemons work simultaneously on our servers, distributing the load among themselves. Each of them is responsible for its 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 all in a row. The table below is an example:

    A young man from Paris who wants to meet a girl will be sent to server number 1. At the same time, in the block of photographs he will see men and women from the metropolitan area with the same interests.

    Only those who have at least one photo approved by our moderators can get into Spotlight, but we tried to make this process as quick and invisible as possible. Even if the user has just uploaded his photo, it will go for accelerated extraordinary moderation. No more than a minute will pass when he sees himself in first place among other photos. If a user deletes a photo, it will immediately be reflected in the service - among all his photos, the first one suitable for replacement will be found. For all current applications, the relevance of photos and profile is constantly checked. For example, if a user violates the rules of the site and is blocked, then his application will be deleted.

    Initially, Spotlight was based on Memcache, and the MySQL database was used as a permanent application store. The front-end JavaScript service accessed Memcache directly through a special module for the nginx web server. The PHP scripts that processed the queues in the database and updated the data in Memcache were fully responsible for processing applications. 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 daemon in C.

    One of the interesting tasks 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 former was at least 25%. To do this, they introduced invitations to use the service for free.

    On each page where there is Spotlight and the current user meets certain requirements, we calculate the number of free spaces missing up to 25%. If there is the right amount of free space, we show the invitation to the female user. The difficulty of the task is that the number of invitations is limited (we cannot show invitations to the whole site at the same time), in addition, the user can think for a while and subsequently refuse the service.

    The first thing that comes to mind is to check the availability of every request. If they are, then show the invitation. Suppose we have 10 seats and we begin to invite all suitable users, which may be, say, 1000 at one point in time, and they all agree. And then the next minute, users of the site will see a machine-gun line of photos on the page. Therefore, it is necessary to keep a record of free places and correctly handle simultaneous requests. Memcache is used for this.

    The algorithm is as follows:
    1. Each time you request a page with Spotlight, we get a list of existing applications. We take statistics on the number of users (M \ F), based on which we determine the number of possible free invitations.
    2. If the user meets certain criteria, we immediately try to 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_in memcache. There are exactly as many such keys as there are available counties. If the value of this key is greater than the number of free places, then we 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.
    3. If you managed to reserve a place, show the user an invitation and put him the appropriate flag in the session. We are waiting for the user's decision.
    4. In case of user failure, we decrement.
    It is quite possible that the user closes the page without performing any action. In this case, we will have an issued but not implemented invitation. This does not present any 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 there are people who want to use the service. But, on the other hand, there is higher competition for the time during which the user's photo will be in the center of attention. The service will not allow the photos to quickly disappear, 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 get to know each other.

    Alexander Treg Treger, developer of Badoo.

    Also popular now: