0Pricing
GraphQL APIs with Spring Boot · レッスン

レート制限とクエリ深度の保護

クライアントからの呼び出し頻度とクエリのネスト深度を制限し、Spring Boot GraphQL APIを悪用やDoS攻撃から守る方法を学びます。

「レート制限とクエリ深度の保護」はCoddyKit上の無料GraphQL APIs with Spring Bootレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGraphQL APIs with Spring Boot学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why GraphQL Needs Protection

A single GraphQL endpoint accepts arbitrarily complex queries. A malicious or careless client can request deeply nested data or hammer the server, exhausting resources.

Rate limiting and depth protection defend against these attacks.

The Nested Query Threat

Because GraphQL allows cyclic relationships, a client could ask for an author's books, each book's author, that author's books, and so on. This recursion can balloon into an enormous, expensive query.

query {
  author {
    books { author { books { author { name } } } }
  }
}

Limiting Query Depth

graphql-java provides MaxQueryDepthInstrumentation, which rejects any query that nests deeper than a set limit before execution begins.

@Bean
public Instrumentation depthLimit() {
    return new MaxQueryDepthInstrumentation(10);
}

Limiting Field Count

Beyond depth, a broad query can request thousands of fields. MaxQueryComplexityInstrumentation caps the total complexity score of a query.

@Bean
public Instrumentation complexityLimit() {
    return new MaxQueryComplexityInstrumentation(200);
}

What Is Rate Limiting?

Rate limiting caps how many requests a client may make in a time window. It prevents abuse and ensures fair resource sharing across clients.

The Token Bucket Idea

A common algorithm is the token bucket: each client has a bucket that refills at a steady rate. Every request consumes a token; if the bucket is empty, the request is rejected.

Rate Limiting with Bucket4j

The bucket4j library implements token buckets in Java. Configure a bucket with a refill rate and capacity.

Bandwidth limit = Bandwidth.simple(100, Duration.ofMinutes(1));
Bucket bucket = Bucket.builder().addLimit(limit).build();

Enforcing the Limit

Before processing a request, try to consume a token. If none is available, return an error instead of executing the query.

if (!bucket.tryConsume(1)) {
    throw new RateLimitException("Too many requests");
}

Per-Client Buckets

Track a separate bucket per client, keyed by API key or authenticated user ID, so one heavy client cannot starve everyone else.

Bucket bucket = buckets.computeIfAbsent(userId, k -> newBucket());

Combining Defenses

Layer your protections for full coverage:

  • Depth limit stops recursive abuse
  • Complexity limit stops broad expensive queries
  • Rate limit stops request floods
  • Timeouts stop slow runaway operations

Best Practices

Tune limits to your real traffic:

  • Start strict, then relax based on monitoring
  • Return clear errors so clients can back off
  • Apply tighter limits to unauthenticated traffic
  • Log rejected queries to spot abuse patterns

Quick Check

Test your API protection knowledge.

Recap

You hardened your GraphQL API:

  • Depth and complexity instrumentation block expensive queries
  • Rate limiting caps request frequency per client
  • Token buckets (bucket4j) implement fair limits
  • Layer depth, complexity, rate, and timeout defenses

These guards keep your API available and resilient under abuse.

よくある質問

「レート制限とクエリ深度の保護」レッスンは無料ですか?

はい。「レート制限とクエリ深度の保護」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、GraphQL APIs with Spring Bootコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。

「レート制限とクエリ深度の保護」で何を学びますか?

クライアントからの呼び出し頻度とクエリのネスト深度を制限し、Spring Boot GraphQL APIを悪用やDoS攻撃から守る方法を学びます。 ブラウザで直接実行するハンズオンコードでGraphQL APIs with Spring Bootを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

GraphQL APIs with Spring Bootを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのGraphQL APIs with Spring Bootは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「レート制限とクエリ深度の保護」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このGraphQL APIs with Spring Bootレッスンでコードを書いて実行できますか?

はい。すべてのGraphQL APIs with Spring Bootレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. GraphQLのカスタムエラーハンドリング
  2. Spring Securityによる認証
  3. DirectiveとContextによる認可
  4. レート制限とクエリ深度の保護
← GraphQL APIs with Spring Bootに戻る