Skip to main content

Command Palette

Search for a command to run...

How Twitter stores Articles in Database

Published
3 min read
How Twitter stores Articles in Database

In this image (and even in this blog), how do you think the images and texts are being stored together? Not just together, but placed at custom positions instead of being dumped on separate sides.

This is not the typical post/entity type we design while learning. When we usually study about designing database for a social media app, the first db design comes to our mind is this:

But twitter also has an entity called 'Articles' that has it's own way of dictating how content should be shown to users, and that is - however user wants!

Which means user decides if the image 1 should go on top or bottom if text should be placed 2nd or 3rd or 5th and where should heading be added. Everything is so custom that it cannot be dumped simply as text and media classification.

What unique does twitter and blogging platforms do internally to store this kind of content on their database?

I was watching a video on database design which was creating a simple database for Twitter and explaining why we should have a separate media table... as a tweet can have multiple media. Which got me thinking that articles have multiple media at multiple positions, if I were to add that table in the model, how would I have done it? I asked AI about it and here is what I learned-

They simply break that one single post into blocks. Blocks that can be assigned position (1, 2, 3...) to dictate what would be the order of their placement. Something like this:

And NoSQL can do the same thing in document model, like this:

Pretty interesting, right?

Our database knows that our post has multiple blocks, and it knows what the type is (for e.g, paragraph, image, heading, quote), it also knows what the position would be, each content block has a unique identity linking it to id and also a foreign key to link it to the actual article . So when you ask for a specific article it looks up for the content blocks with the same id and arranges it in order according to their positions.

So, this is the design came up with on top of the same twitter like app design I watched in the tutorial, ofc I avoided tables like Follow, Repost, Share, etc here to avoid adding to the complexity and distracting focus from Articles and Content Blocks.

Hope this was a good read, it was my attempt to document my learning. See you again with some other interesting topic.