0
0
Raspberry Piprogramming~5 mins

HTTPS for web server in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: HTTPS for web server
O(n)
Understanding Time Complexity

When setting up HTTPS on a Raspberry Pi web server, it's important to understand how the time to handle requests grows as more users connect.

We want to see how the server's work changes when it encrypts and decrypts data for each visitor.

Scenario Under Consideration

Analyze the time complexity of the HTTPS handshake and request handling code.


import ssl
import socket

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile='cert.pem', keyfile='key.pem')

with socket.socket() as sock:
    sock.bind(('0.0.0.0', 443))
    sock.listen(5)
    while True:
        client, addr = sock.accept()
        with context.wrap_socket(client, server_side=True) as ssock:
            data = ssock.recv(1024)
            ssock.sendall(b'HTTP/1.1 200 OK\r\n\r\nHello HTTPS')
    

This code sets up a simple HTTPS server that accepts connections, performs SSL handshake, receives data, and sends a response.

Identify Repeating Operations

Look at what repeats as more clients connect.

  • Primary operation: The server accepts connections and performs SSL handshake for each client.
  • How many times: Once per client connection, repeating indefinitely.
How Execution Grows With Input

Each new client causes the server to do a handshake and handle a request.

Input Size (n)Approx. Operations
10 clients10 handshakes + 10 request processes
100 clients100 handshakes + 100 request processes
1000 clients1000 handshakes + 1000 request processes

Pattern observation: The work grows directly with the number of clients; more clients mean more handshakes and processing.

Final Time Complexity

Time Complexity: O(n)

This means the server's work grows linearly with the number of client connections it handles.

Common Mistake

[X] Wrong: "The SSL handshake happens once and then all clients share it."

[OK] Correct: Each client must do its own handshake to securely connect, so the server repeats this work for every new client.

Interview Connect

Understanding how HTTPS affects server workload helps you explain real-world server behavior and scalability in interviews.

Self-Check

"What if the server used persistent connections to keep clients connected longer? How would the time complexity change?"