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';
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';
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!
Top comments (0)