Skip to main content
Clean Code 70 XP · 7 min

Concurrency: Client-Server Model

Structure client-server concurrent systems to isolate shared state and avoid subtle threading bugs.

Showing
Ad (728×90)

Separate Concurrency from Logic

A server that handles HTTP requests is a concurrency concern. The business logic that processes each request is a separate concern. Mixing them creates untestable code. Extract the business logic into pure, single-threaded functions — easy to unit test, easy to reason about.

Limit Scope of Shared Data

The more threads share the same data, the higher the risk of race conditions. Keep shared data to a minimum. When sharing is unavoidable, use the smallest possible critical section. Prefer immutable data — data that cannot be changed cannot have a race condition.

Code Challenge

Fix the unprotected shared state in the server handler.

Key takeaway

Concurrency and business logic are separate concerns. Keep them in separate classes; test the logic without threads.

Done with this lesson?

Mark it complete to earn XP and track your progress.