← All AI Engineer talks

AI Engineer World's Fair 2026

Serving 2 Million Models Without Melting: Scaling the Hugging Face Hub

About this talk

Hugging Face engineer Arek Borucki explains how the Hub supports a rapidly growing catalog of approximately three million models and more than 14 million users. He describes prioritizing P99 search latency, separating MongoDB Atlas metadata from model artifacts in AWS S3, using denormalized read collections and Apache Lucene-powered Atlas Search for tokenized autocomplete and relevance ranking, and scaling Kubernetes workloads with Horizontal Pod Autoscaler.

Chapters

  1. 0:00Speaker introduction and Hugging Face Hub scale
  2. 3:03Catalog growth, search bottlenecks, and P99 latency
  3. 5:16Kubernetes request flow, MongoDB Atlas metadata, and S3 artifacts
  4. 8:12Denormalized search, Lucene autocomplete, and trending scores
  5. 15:15Expensive database aggregation workloads
  6. 21:10Closing remarks

Talk transcript

  1. 0:00

    [on hold music] Good afternoon, everyone. I have a question.

  2. 0:19

    How many of you knows Hugging Face? Nice.

  3. 0:27

    How many of you already use Hugging Face?

  4. 0:33

    Amazing. Almost everyone. But I think we still have opportunity to grow our usage. My name is Arek Borucki. I work as machine learning platform and database engineer at Hugging Face.

  5. 0:49

    Today, I would like to walk you through how Hugging Face scaled

  6. 0:57

    infrastructure, and how we ended up serving three million models to developers around the world.

  7. 1:09

    I would like to share architectural decisions we made, challenges we faced, and lessons we learned while scaling one of the fastest growing open source AI communities in the world.

  8. 1:29

    I hope you will enjoy it, and let's get started.

  9. 1:37

    Before I dive into technical details, let's talk about scale.

  10. 1:44

    Today, Hugging Face serves more than fourteen million users, and this number is growing very fast, especially in the last couple of months.

  11. 1:57

    We host three million public models, one million datasets,

  12. 2:07

    ten-- fifty thousand organizations. And not only hobbyists or scientists.

  13. 2:16

    More than thirty percent of Fortune five hundred use Hugging Face as a part of AI workflows.

  14. 2:27

    Just to give you some perspective, three years ago, we had twenty thousand models. Today, three million.

  15. 2:39

    It is around one hundred fifty x increase in just last couple of years.

  16. 2:48

    And this growth is exactly why I'm here today talking about infrastructure decisions that keep the hub healthy at scale.

  17. 3:03

    This is how fast the number of public models is growing on the hub. Every big release like Llama or DeepSeek generated thousands of new models on top,

  18. 3:20

    and our infrastructure needs to handle that. And it is not only models,

  19. 3:28

    also datasets. In twenty twenty-two, we had ten K.

  20. 3:36

    In twenty twenty-four, one hundred K. Less than a year ago, we had five hundred K. Today, one million.

  21. 3:49

    All this data must be stored, indexed, and also must be searchable. And that's the hardest part.

  22. 4:02

    And this is also the reason why we had to rethink our search.

  23. 4:09

    At twenty thousand models, any query is fast, even without an index. Trust me, no one would notice. At three million, same approach breaks.

  24. 4:23

    Imagine what would you do if the hub search would be slow.

  25. 4:30

    You would just leave and go somewhere else, and this is also what user are, are doing. They expect fast, instant results.

  26. 4:39

    With fourteen million users, even one percent is a not small number. It is one hundred and forty thousand of people hitting slow search.

  27. 4:56

    At scale, P99 is much more important than P50, and we are paying a lot of attention to P99.

  28. 5:09

    And that's the reason why we invest in pre-compute tokens,

  29. 5:16

    denormalize, optimize for read, collection in MongoDB, full-text search based on Apache Lucene, Kubernetes autoscaling,

  30. 5:30

    and soon in database sharding. The next slides will show you how.

  31. 5:41

    High-level architecture. When user interact with the Hugging Face Hub, his request flows from the front end to the Hub API.

  32. 5:54

    The hub is running on Kubernetes. Currently, we are using Horizontal Pod Autoscaler.

  33. 6:03

    During spikes, new pods scale up automatically to handle the load and scale back down when traffic drops.

  34. 6:14

    This help us to keep the hub healthy without manual intervention. Next, the request

  35. 6:26

    goes to MongoDB Atlas, which is source of true for our metadata. And there is one point that sometimes surprise people. MongoDB does not store the models themselves.

  36. 6:44

    It stores everything about the models. What does it mean in practice?

  37. 6:52

    In MongoDB, we hold all the metadata, users, repositories, models, data sets, buckets, spaces information, configuration data, billing data, access control, and more.

  38. 7:17

    The actual models artifacts, tokenizer files, card assets, and configuration files are stored separately in cloud object storage, such as AWS S3.

  39. 7:34

    This separation of concern let us scale metadata independently from binary storage and compute independently from both.

  40. 7:46

    We can optimize each component individually for specific workload.

  41. 7:55

    Now let's check how search works in details. For example, someone wants to search the model on the hub, and let's say that's Llama. So someone type Llama into Hugging Face search bar.

  42. 8:12

    His request flows through the hub to an optimized read collection on MongoDB. And this is not our main repo collection when we keep all the data. It's a separate denormalized copy

  43. 8:30

    only for reads and listings. The key information is on the left.

  44. 8:40

    We tokenize model names on insert time, not at query time.

  45. 8:47

    For example, someone wants to publish model meta-llama/llama3.18b.

  46. 8:58

    We split the long model name into small tokens like meta, llama, three dot one, eight b, and we store them in an array in MongoDB document. Next, Atlas Search, which is using Apache Lucene under the hood, use autocomplete to find matching models

  47. 9:23

    instantly. This is example of single document from our model collection. In this example, I'm using findOne method. I want to find model ID meta-llama/llama3.18b.

  48. 9:43

    So that's the model from the previous slide.

  49. 9:48

    And I'm projecting only searchToken array. And we see that all those precomputed tokens are part of this array. So we have meta, llama, three dot one, meta, llama, et cetera.

  50. 10:03

    Next, there must be a query. In the past, we were using classical MongoDB find method on models collection with regex operator.

  51. 10:20

    And this regex operator were searching in search tokens arrays models which are equal to llama.

  52. 10:30

    And then we were set-- sorting results by trending score, which is calculated every five minutes. This is number of downloads and number of, of likes.

  53. 10:43

    It is, as far as I remember, from the last seven days.

  54. 10:48

    This solution was working well as long as data set was small.

  55. 10:55

    Regex doesn't scale well, so when our data set started to grow very quickly, we started to having problems with latency. So we decided to switch to Atlas Search.

  56. 11:11

    That's a feature which is using Apache Lucene under the hood.

  57. 11:18

    So MongoDB doesn't provide in core MongoDB server full-text search. There is additional process, mongot.

  58. 11:29

    Uh, this MongoDB process is a wrapper around Apache Lucene.

  59. 11:37

    For end user, the-- users, this is transparent. You are just using unified MongoDB query API. And

  60. 11:47

    when you use aggregation pi-pipeline together with dollar search operator, MongoDB will know that you would like to search Apache Lucene index.

  61. 12:02

    Obviously, you need to put the name of this index, which is in this scenario, model search.

  62. 12:11

    Auto-complete model equal to Llama, path search tokens,

  63. 12:20

    and we still sort results by trending score. And this solution is much more efficient,

  64. 12:28

    and is so far scale well. So we don't have any more latency issues in our search bar.

  65. 12:41

    First two results returned by previous query. First Meta Llama has trending score thirty-three, second one fourteen.

  66. 12:56

    But Hugging Face Hub is not only search. We have hundreds of different services in Hugging Face which are utilizing, which are using MongoDB.

  67. 13:08

    To handle million of queries, we use seven nodes MongoDB clusters, cluster.

  68. 13:17

    With multiple machines, we can distribute queries across multiple nodes, so no single node become read bottleneck.

  69. 13:29

    This is how it works. Application talk to the MongoDB cluster. All inserts, deletions or updates goes to single primary because only primary can handle them.

  70. 13:43

    However, we are distributing reads across multiple machines.

  71. 13:51

    We also have one analytic hidden node. What does it mean? This me- this node is invisible from application. Mong- MongoDB driver is not routing any queries to this hidden node.

  72. 14:10

    This node is still replicating data from primary, but it's not interacting with, interacting with production traffic. We are connecting directly to this node, and we use him for any kind of

  73. 14:28

    reporting traffic on re- or any kind of really heavy queries.

  74. 14:35

    All secondaries continuously tail the oplog from primary, keeping cluster in sync.

  75. 14:45

    Now let's have a look what actually is running on secondaries. First, all queries which doesn't require the latest data go to secondaries.

  76. 14:58

    Only queries that must have strong consistency stay on primary, and we are paying a lots of attention to this. We are paying lots of attention to the queries which must run on primary.

  77. 15:15

    Second, complex aggregations. Aggregations pipelines that scan large amount of data, sort, group, or transform the data

  78. 15:31

    should not go on primary. They are heavy. Secondaries are better place for them.

  79. 15:38

    Third, change streams. We react to data changes in real-time for several reaso- reasons. For example, cache invalidation, sync to different data store technologies like, for example, AWS Redshift, or for event-driven workloads.

  80. 16:00

    Those kind of operations are also not very light, and they should stay impossible on secondaries. Fourth,

  81. 16:10

    all ad-hoc queries, reporting queries, maybe some experimental queries go to hidden MongoDB replica set member which is isolated from production traffic.

  82. 16:23

    The pattern is simple. Primary should focus on what only primary can do. Anything else can be pushed to different machines.

  83. 16:38

    However, with forty million users, three million models and our grow, soon single MongoDB replica set will not be enough.

  84. 16:52

    The next step is sharding. Sharding means scaling your database horizontally. Instead of putting full dataset on one repli- on one replica set cluster, we are going to cut data into pieces

  85. 17:13

    and put each piece on separate shard. Each shard will have his own replication, primary and secondary. So we will keep replication just multiplied.

  86. 17:26

    The key difference between replica set cluster and sharded cluster is replica set keep full dataset on each node. Sharded cluster keep only part of the data on each shard.

  87. 17:39

    And then if you want to scale horizontally more, you are just adding more shards, and then MongoDB balancer will balance data across all those shards. There is also shard key which must be selected.

  88. 17:53

    This is not trivial operation, but this talk is not about choosing shard key.

  89. 18:00

    This way we are going to scale everything,

  90. 18:04

    CPU, memory, storage, reads and writes Now let's have a look what is going on the hub level.

  91. 18:19

    The hub is running on Kubernetes. S- currently, we see are using Horizontal Pod Autoscaler. When CPU or memory threshold goes above target, Kubernetes adds new pod automatically to handle the spike and scale them back down when traffic drops.

  92. 18:41

    Our deployment, hub deployment can scale from ten to five hundred pods, depends on, on the traffic. This is how we keep the hub healthy without manual interventions and without infrastructure overprovisioning.

  93. 18:59

    So this is also cost-effective solution. However, what happens if Horizontal Pod Autoscaler want to add new pods but Kubernetes does not have free nodes anymore?

  94. 19:17

    This is where second layer comes in. We are using Cast AI for Kubernetes node autoscaling. When pods are pending because there is no capacity and Kubernetes sched- scheduler is not able to schedule them, Cast AI is adding new nodes, and then scheduler is able to schedule those

  95. 19:41

    pods. So we have two layers of scaling. First one is at deployment level, second one is at infrastructure level via Cast AI.

  96. 19:54

    But we are going to migrate Horizontal Pod Autoscaler to KEDA, Kubernetes Event-Driven Autoscaling. The difference,

  97. 20:07

    HPA scale only based on CPU and memory. KEDA scale on real application metrics like request per second or

  98. 20:19

    event loop utilization. It means scaling is driven by actual workload, not by resource utilization only. For example, pod can have low CPU but high request queue, KEDA can see it, HPA not.

  99. 20:43

    The best part of this architecture, you never have to think about it. When you pop-- When you push the model, search the hub, or download the model, it just works.

  100. 20:58

    This is what scaling million models is really about, keeping the user experience simple no matter how complex it gets under the hood.

  101. 21:10

    Thank you very much. It was pleasure for me to be a here today, and I wish nice day for all of you. [audience applauding]

  102. 21:21

    Thank you. [outro music]