Topics / 🌐 API & Software Architecture / 🚪 Level 92: API Architecture: Gateways, Mesh & Releases

Python exercise · about 5 minutes · free

API Gateway: Longest-Prefix Routing

One public entry point forwards /api/users to the user service and everything else under /api to the core service.

An API gateway is the single front door: it routes requests to services and can handle authentication, rate limiting, TLS, caching and logging in one place. Routing usually picks the longest matching prefix on a segment boundary. Pitfalls: turning the gateway into a business-logic monolith, and making it a single point of failure without redundancy.

Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.

Diagram. Client; Gateway (longest prefix); users service (/api/users); core service (/api). Links: Client → Gateway; Gateway → users service; Gateway → core serviceClientGatewaylongest prefixusers service/api/userscore service/api
Your quest: Write route(routes, path). routes maps a prefix to a service name. Find the longest prefix that equals the path or is followed by / in the path (so /api/users does not match /api/usersX). Return (service, remaining_path), where the remainder keeps its leading slash ("/" if empty), or None when nothing matches.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
HintA prefix matches if <code>path == prefix or path.startswith(prefix + '/')</code>. Choose the longest, then slice it off.
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 92: API Architecture: Gateways, Mesh & Releases

Share on LinkedIn