DeployU
Interviews / Databases / What are capped collections and when would you use them in MongoDB?

What are capped collections and when would you use them in MongoDB?

conceptual Core Concepts Interactive Quiz Code Examples

The Scenario

You are a backend engineer at a social media company. You are building a new service that needs to store a log of the most recent user activity.

You need to find a way to store the logs in a way that is efficient and that automatically removes old logs.

The Challenge

Explain what capped collections are in MongoDB and how you would use them to solve this problem. What are the key benefits of using capped collections?

Wrong Approach

A junior engineer might try to solve this problem by using a regular collection and then writing a separate script to periodically remove old logs. This would be a complex and inefficient solution.

Right Approach

A senior engineer would know that a capped collection is the perfect tool for this job. They would be able to explain what a capped collection is and how to use it to automatically remove old logs.

Step 1: Understand What Capped Collections Are

A capped collection is a fixed-size collection that automatically overwrites its oldest entries when it reaches its maximum size.

Step 2: The Key Features of Capped Collections

FeatureDescription
SizeCapped collections have a fixed size, which you specify when you create the collection.
OrderingCapped collections maintain the insertion order of the documents.
TTLCapped collections do not have a TTL (Time To Live) index.
UpdatesYou cannot update documents in a capped collection if the update would cause the document to grow in size.
DeletesYou cannot delete documents from a capped collection.

Step 3: Use a Capped Collection

Here’s how we can create a capped collection to store our logs:

db.createCollection("my_logs", { capped: true, size: 100000 })

In this example, we create a new capped collection called my_logs with a maximum size of 100,000 bytes.

The Benefits of Using Capped Collections

BenefitDescription
PerformanceCapped collections are very performant, because they are designed for high-throughput logging.
SimplicityCapped collections are easy to use, and they automatically handle the removal of old logs.

Practice Question

You are building a service that needs to store a log of the most recent user activity. Which of the following would be the most appropriate?