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

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay