How we built rearranging Pins

Pinterest Engineering
4 min readbeginner
--
View Original

Overview

This article discusses the implementation of a feature that allows users to reorder Pins on Pinterest boards, addressing the technical challenges involved in scaling the backend service. It explores the requirements, data storage solutions, and various approaches to efficiently manage Pin sequences in a high-volume environment.

What You'll Learn

1

How to implement a Pin reordering feature in a scalable backend service

2

Why using gap bisection can optimize database writes during reordering

3

How to increase sequence resolution to prevent collisions in a database

Prerequisites & Requirements

  • Understanding of database management and SQL
  • Familiarity with MySQL and backend service architecture(optional)

Key Questions Answered

What are the requirements for the Pin reordering backend service?
The requirements include allowing Pins to be dragged between two other Pins on the same board, enabling repeated reordering within seconds, and ensuring the solution scales across boards with many Pins.
How does gap bisection improve the Pin reordering process?
Gap bisection reduces the number of database writes by allowing a reordered Pin to take on a midpoint value between its neighboring Pins, thus requiring only one row update instead of multiple updates for each Pin in the range.
What improvements were made to the sequence resolution for Pins?
The sequence resolution was improved by increasing the timestamp granularity from seconds to milliseconds and expanding the sequence column from 10 to 38 digits, allowing for significantly more bisections before collisions occur.
What common pitfalls should be avoided when implementing Pin reordering?
One common pitfall is not accounting for sequence collisions when using integer-based sequences. This can lead to performance issues and incorrect ordering if not managed properly through techniques like gap bisection.

Key Statistics & Figures

Maximum bisections supported
log
10²⁵
Initial sequence value type
INT
11

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Implement gap bisection in your database design to optimize write operations during reordering.
This technique minimizes the number of database updates required for each reorder, which is crucial in high-volume environments where performance is a concern.
2
Increase the resolution of your sequence identifiers to prevent collisions.
By using milliseconds instead of seconds and expanding the sequence column, you can support a greater number of reorders without running into issues of overlapping sequence values.
3
Regularly monitor and adjust sequence gaps to maintain optimal performance.
Creating an offline job to re-space sequences when gaps become too small ensures that your system remains efficient and responsive to user actions.

Common Pitfalls

1
Failing to account for the potential for sequence collisions when using integer-based sequences.
As the number of Pins increases, the likelihood of collisions also rises, which can lead to incorrect ordering and performance degradation. Implementing strategies like gap bisection can mitigate this risk.

Related Concepts

Database Scaling
Backend Service Architecture
Pin Management Strategies