PortfolioBlog

The Secret Sauce Behind TikTok's Algorithm: A Deep Dive into Hashing

1/20/2025 | By Saksham Adhikari

https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6bd6a76a-ed7b-49a3-b416-049481c1c49d_1120x1120.jpeg

Billions of Videos, Milliseconds to Decide

How TikTok serves the perfect video to over 1 billion users instantly

Ever opened TikTok and thought "How does it know exactly what I want to watch?" The answer isn't magic – it's math. Behind those addictively accurate video recommendations lies an elegant data structure called hashing, combined with sophisticated recommendation algorithms.

🔍 Understanding Hashing: The Digital Library Card System

Imagine walking into the world's largest library. Instead of searching through millions of books one by one, you have a magical index that instantly tells you where each book is located. That's essentially what hashing does - it creates direct pathways to data.

Imagine walking into the world's largest library. Instead of searching through millions of books one by one, you have a magical index that instantly tells you where each book is located. That's essentially what hashing does - it creates direct pathways to data.

Here's a simple example of how hashing works:

def simple_hash_example(key):
    """Simple demonstration of hashing concept"""
    total = 0
    for char in str(key):
        total += ord(char)
    return total % 10  # Ensures output is between 0-9

⚡ TikTok's Secret Weapon: Hashing at Scale

TikTok's recommendation system is like a massive library that needs to:

  • Remember what each of its billion+ users likes
  • Categorize millions of new videos daily
  • Match users with perfect video recommendations in milliseconds

Here's a peek at how they do it:

class TikTokRecommendationSystem:
    def __init__(self):
        # Store user interests in a hash table
        self.user_interests = SimpleHashTable()

        # Store video categories in a hash table
        self.video_categories = SimpleHashTable()

🎯 How TikTok Remembers What You Like

Every time you interact with a video, TikTok updates your personal interest profile:

def track_user_engagement(self, user_id, video_id, action):
    user_profile = self.user_interests.get(user_id) or {
        "interests": {},
        "watched_videos": set()
    }

    video_data = self.video_categories.get(video_id)

    # Update user interests based on action
    for category in video_data["categories"]:
        if category not in user_profile["interests"]:
            user_profile["interests"][category] = 0

        # Update interest scores
        if action == "like":
            user_profile["interests"][category] += 10
        elif action == "share":
            user_profile["interests"][category] += 15

Different actions carry different weights:

  • 💌 Sharing a video: 15 points (highest impact)
  • ❤️ Liking a video: 10 points
  • 📱 Watching to the end: 5 points
  • 👎 Skipping quickly: -3 points

📊 The Scale is Mind-Boggling

According to ByteDance's research:

  • Processes millions of video features per second
  • Delivers recommendations in under 100 milliseconds
  • Handles terabytes of new user data every day

🚀 Beyond Basic Recommendations

Here's how TikTok suggests your next video:

def recommend_videos(self, user_id):
    user_profile = self.user_interests.get(user_id)
    if not user_profile:
        return self.get_trending_videos()  # For new users

    # Sort interests by score
    top_interests = sorted(
        user_profile["interests"].items(),
        key=lambda x: x[1],
        reverse=True
    )[:3]  # Top 3 interests

But it gets even more sophisticated. TikTok's system also includes:

  • Multi-dimensional Feature Hashing: Videos are tagged with multiple characteristics
  • Time-Based Interest Decay: Your old interests gradually fade
  • New User Handling: Smart content mixing for new accounts

🔮 Looking Forward

TikTok's recommendation system shows us how computer science fundamentals can create seemingly magical user experiences. By combining efficient data structures with advanced algorithms, they've built one of the most engaging platforms in digital history.

What's Next in Our Series?

Stay tuned for Part 2, where we'll explore Graph Algorithms in Social Media Recommendations and see how platforms like TikTok map relationships between videos, users, and trends.


Enjoying this series on algorithms behind modern tech? Subscribe to get the next installment directly in your inbox!


References:

  • ByteDance Research Paper: "Monolith: Real Time Recommendation System With Collisionless Embedding Table"
  • TikTok Engineering Blog