Sign in

System Design

Client-server architecture, from raw sockets to scaled distributed systems

Learn how client-server architecture works, implement custom TCP communication in Python, and scale services with connection handling and load distribution.

Plain words. One step at a time. Nothing here assumes you already know the idea.

The idea

Client-server architecture is a distributed computing model where workloads are partitioned between service requesters, called clients, and service providers, called servers. Clients initiate communication sessions to request specific resources or operations, and servers listen for incoming requests, process them, and return appropriate responses.

See it

Consider a seated restaurant.

1 of 4

You will need. Basic knowledge of Python syntax and data structures · Familiarity with network concepts such as IP addresses and ports

One pass

Step 1 of 3

At its core, client-server communication relies on two distinct roles operating over a network. The server is a continuously running process that binds to a known network address and port, listening passively for incoming connections. The client is an active process that initiates contact whenever it requires data or needs to trigger an action on the server.

Smallest version

Minimal synchronous client and server communicating over a TCP socket

import socket
import threading

def run_server():
    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_sock.bind(("127.0.0.1", 65432))
    server_sock.listen(1)
    
    conn, addr = server_sock.accept()
    data = conn.recv(1024).decode("utf-8")
    
    store = {"alice": "Active, Admin", "bob": "Inactive, Member"}
    response = store.get(data.strip(), "Not Found")
    
    conn.sendall(response.encode("utf-8"))
    conn.close()
    server_sock.close()

def run_client():
    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_sock.connect(("127.0.0.1", 65432))
    client_sock.sendall(b"alice")
    response = client_sock.recv(1024).decode("utf-8")
    client_sock.close()
    return response

if __name__ == "__main__":
    t = threading.Thread(target=run_server)
    t.start()
    result = run_client()
    t.join()
    print("Client received:", result)

Keep these

  • Servers wait passively for incoming connections, while clients initiate requests.
  • A connection requires both an IP address to locate the host and a port number to address the specific process.
  • Data sent over raw network sockets must be serialized into bytes before transmission and deserialized upon receipt.

Try one

Echo Server with Graceful Shutdown

easy

Start the five-step lesson

Also on MonksBit