DEV Community

Cover image for The Justin Beiber database problem!
Usman Zahid
Usman Zahid

Posted on

1

The Justin Beiber database problem!

Ever heard of the Justin Beiber problem in database design? You might also be wondering what a celebrity has to do with the database world.

I was also shocked by this but the problem is actually pretty interesting and will give you insight into another database problem that you may optimize.

The problem began on Instagram when Justin Bieber posted photos on Instagram and he would get millions of likes in a short period of time. And each time the system had to show a view count it would go for counting the likes count in the database which would be something like this (Just a simple representation):

SELECT * FROM likes WHERE post_id = 'e9677138-48cf-47eb-9306-db6994956e9e';
Enter fullscreen mode Exit fullscreen mode

Now when we have this query run a million times, it's not a great thing for your database server. It would suck on the resources for a simple task that could be used for better things.

So how this is solved is simple. Rather than counting by querying the likes table, we have a likes_count column in the posts table which we can directly access. Each time a post is liked it creates a like record in the database and also increments the likes_count column in the main posts table.

So each time the application needs to query the likes_count it's far simpler and faster. We can just query for the likes count like this:

SELECT likes_count FROM posts WHERE id = 'e9677138-48cf-47eb-9306-db6994956e9e';
Enter fullscreen mode Exit fullscreen mode

That's Justin Beiber's problem in Database design. Knowing it would greatly impact your database design and make you a better backend engineer.

Follow for more!

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more