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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay