Optimising API Latency: Session Retention Best Practices

When working with the Tiliter API, one of the key factors that can influence the speed of image recognition is how you manage API sessions. This guide will help you understand the importance of session retention and provide best practices to ensure minimal latency in your requests.

Why Session Retention Matters

Every time an API session is instantiated, there’s an inherent delay associated with establishing a new connection. This delay, although minor on an individual basis, can add up significantly, especially in high-frequency or time-sensitive applications. By retaining a session, you avoid this overhead, resulting in faster response times and more efficient use of resources.

Best Practices for Session Retention

1. Reuse the Session Object

The requests library in Python provides a Session object, which allows you to persist certain parameters across requests. This is particularly useful for retaining cookies, headers, and other settings. By reusing the same Session object, you ensure that the connection remains open, thereby reducing the time taken for each request.

Example: Correct Way to Retain a Session

import requests  
from datetime import datetime

# Set your API key

TILITER_API_KEY = "your_api_key_here"

# Create a session

session = requests.Session()

# Set up headers and data template

headers = {  
    "tiliter-api-key": TILITER_API_KEY,  
    "Content-Type": "application/json"  
}

data_template = {  
    "device_id": "demo",  
    "images": [{  
        "camera_type": "generic",  
        "image": "base64_encoded_image_data",  
        "capture_time": datetime.now().isoformat()  
    }]  
}

# Function to send a request

def send_request(session, url):  
    data = data_template.copy()  
    data["images"][0]["capture_time"] = datetime.now().isoformat()  
    response = session.post(url, headers=headers, json=data)  
    return response

# Reuse the session for multiple requests

for \_ in range(10):  
    response = send_request(session, "<https://recognition.services.tiliter.com/recognition/">)  
    print(response.status_code)

2. Avoid Creating New Sessions for Each Request

Creating a new session for each API call can significantly slow down your application, as the session instantiation adds unnecessary latency.

Example: Incorrect Way to Handle Sessions

import requests  
from datetime import datetime

# Set your API key

TILITER_API_KEY = "your_api_key_here"

# Set up headers and data template

headers = {  
    "tiliter-api-key": TILITER_API_KEY,  
    "Content-Type": "application/json"  
}

data_template = {  
    "device_id": "demo",  
    "images": [{  
        "camera_type": "generic",  
        "image": "base64_encoded_image_data",  
        "capture_time": datetime.now().isoformat()  
    }]  
}

# Function to send a request (creates a new session each time)

def send_request(url):  
    session = requests.Session()  
    data = data_template.copy()  
    data["images"][0]["capture_time"] = datetime.now().isoformat()  
    response = session.post(url, headers=headers, json=data)  
    return response

# Creating a new session for every request - avoid this!

for \_ in range(10):  
    response = send_request("<https://recognition.services.tiliter.com/recognition/">)  
    print(response.status_code)

3. Refreshing the Session

In some cases, sessions may need to be refreshed after a period of time or a certain number of requests. You can handle this by periodically closing the session and creating a new one, but ensure this is done sparingly to avoid introducing unnecessary delays.

# Example with session refresh logic

session = requests.Session()  
for i in range(100):  
    if i % 20 == 0:  # Refresh session every 20 requests  
        session.close()  
        session = requests.Session()  
    response = send_request(session, "<https://recognition.services.tiliter.com/recognition/">)  
    print(response.status_code)

Conclusion

By following these best practices, you can significantly reduce latency in your interactions with the Tiliter API. Retaining a session across multiple requests ensures that your application runs more efficiently, providing faster responses and a better overall user experience.