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.
Consider a seated restaurant.
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
Also on MonksBit
- System design fundamentals, from single servers to distributed architectures Learn how to architect scalable systems by progressing from single-node web servers to load-balanced, cached, and partitioned distributed services. System Design · 9 min
- Client-server architecture, from raw sockets to scalable communication Understand how clients and servers exchange data, implement networking protocols from scratch, and scale communications using multiplexing and buffering. System Design · 8 min
- Architecting for non-functional requirements: scalability, availability, reliability, and performance Master the principles of non-functional requirements to design software systems that handle high traffic, withstand node outages, and maintain low latency. System Design · 8 min
- Why system design matters, from single servers to distributed scale Learn how software architectures evolve from single-node scripts to resilient distributed systems capable of handling millions of concurrent users. System Design · 8 min