0Pricing
Clojure Functional Programming & JVM Backend Development · Lesson

Service Discovery & API Gateways

Learn how Clojure microservices find each other dynamically and how an API gateway provides a single, secure entry point to your system.

Service Discovery & API Gateways is a free Clojure Functional Programming & JVM Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clojure Functional Programming & JVM Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Discovery Problem

In a microservice system, instances start, stop, and move between hosts. Hardcoding IP addresses breaks quickly. Service discovery lets services locate each other dynamically.

Service Registries

A service registry is a database of available services and their network locations. Popular options include Consul, etcd, and Eureka.

Registering a Service

On startup, a service registers itself with the registry, including a health-check endpoint so the registry can drop it if it dies.

(defn register! [consul-url name host port]
  (http/put (str consul-url "/v1/agent/service/register")
    {:body (json/write-str
             {:Name name :Address host :Port port
              :Check {:HTTP (str "http://" host ":" port "/health")
                      :Interval "10s"}})}))

Client-Side Discovery

In client-side discovery, the calling service queries the registry for healthy instances and picks one itself.

(defn lookup [consul-url name]
  (-> (http/get (str consul-url "/v1/health/service/" name "?passing"))
      :body
      json/read-str))

Server-Side Discovery

In server-side discovery, the client calls a load balancer or gateway, which consults the registry and forwards the request. The client stays simple.

What Is an API Gateway?

An API gateway is a single entry point that routes incoming requests to the right backend service. It hides internal topology from clients.

Gateway Responsibilities

Beyond routing, a gateway often handles cross-cutting concerns:

  • Authentication and authorization
  • Rate limiting
  • Request/response transformation
  • Aggregating multiple service calls

A Simple Gateway in Ring

You can build a basic gateway in Clojure by matching the path and proxying to the discovered service.

(defn gateway [request]
  (let [service (route-for (:uri request))
        target  (lookup-instance service)]
    (http/request
      (assoc request :url (str target (:uri request))))))

Authentication at the Edge

Centralizing auth at the gateway means individual services trust the gateway and avoid duplicating login logic.

(defn wrap-gateway-auth [handler]
  (fn [request]
    (if (valid-token? (get-in request [:headers "authorization"]))
      (handler request)
      {:status 401 :body "Unauthorized"})))

Rate Limiting

The gateway is the natural place to throttle abusive clients before requests reach your services.

(defn wrap-rate-limit [handler limit]
  (fn [request]
    (if (under-limit? (client-id request) limit)
      (handler request)
      {:status 429 :body "Too Many Requests"})))

Putting It Together

A production setup: services self-register in a registry with health checks, and a gateway discovers them, enforces auth and rate limits, then routes. This keeps clients decoupled from internal changes.

Quick Check

Test your discovery knowledge.

Recap

You learned how services discover each other and how a gateway unifies access.

  • Registries like Consul track instances via health checks
  • Discovery can be client-side or server-side
  • Gateways centralize routing, auth, and rate limiting

Frequently asked questions

Is the “Service Discovery & API Gateways” lesson free?

Yes — the full text of “Service Discovery & API Gateways” is free to read here on the web, and the Clojure Functional Programming & JVM Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clojure Functional Programming & JVM Backend Development course, upgrade to CoddyKit PRO.

What will I learn in “Service Discovery & API Gateways”?

Learn how Clojure microservices find each other dynamically and how an API gateway provides a single, secure entry point to your system. You practise Clojure Functional Programming & JVM Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Clojure Functional Programming & JVM Backend Development?

No prior experience is required. Clojure Functional Programming & JVM Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Service Discovery & API Gateways” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Clojure Functional Programming & JVM Backend Development lesson?

Yes. Every Clojure Functional Programming & JVM Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Designing Clojure Microservices
  2. Containerization with Docker
  3. Deploying to Cloud Platforms
  4. Service Discovery & API Gateways
← Back to Clojure Functional Programming & JVM Backend Development