import uuid
request_id = str(uuid.uuid4())
POST /api/v1/resource HTTP/1.1
Host: example.com
X-Request-ID: 123e4567-e89b-12d3-a456-426614174000
POST /api/v1/resource?request_id=123e4567-e89b-12d3-a456-426614174000 HTTP/1.1
import requests
headers = {"X-Request-ID": request_id}
try:
response = requests.post("https://example.com/api/v1/resource", headers=headers, timeout=5)
except requests.exceptions.Timeout:
response = requests.post("https://example.com/api/v1/resource", headers=headers, timeout=5)
CREATE TABLE processed_requests (
request_id VARCHAR(36) PRIMARY KEY,
status VARCHAR(20),
response TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
def handle_request(request_id, request_data):
# Check if the request_id is already processed
existing_request = db.get("SELECT * FROM processed_requests WHERE request_id = ?", (request_id,))
if existing_request:
return existing_request["response"] # Return cached response
# Process the new request
response = process_request(request_data)
db.execute("INSERT INTO processed_requests (request_id, status, response) VALUES (?, ?, ?)",
(request_id, "processed", response))
return response
import redis
cache = redis.StrictRedis(host='localhost', port=6379, db=0)
def handle_request(request_id, request_data):
if cache.get(request_id):
return "Duplicate request: already processed"
# Process the new request
response = process_request(request_data)
cache.set(request_id, "processed", ex=3600) # Cache for 1 hour
return response
def create_resource(request_id, resource_data):
existing_resource = db.get("SELECT * FROM resources WHERE request_id = ?", (request_id,))
if existing_resource:
return existing_resource # Return existing resource
# Create new resource
new_resource = create_new_resource(resource_data)
db.execute("INSERT INTO resources (request_id, data) VALUES (?, ?)", (request_id, new_resource))
return new_resource
클라이언트 요청
POST /api/v1/resource HTTP/1.1
Host: example.com
X-Request-ID: 123e4567-e89b-12d3-a456-426614174000
서버 동작
1. 123e4567-e89b-12d3-a456-426614174000이 이미 기록되어 있는지 확인
2. 기록되어 있으면
{
"status": "success",
"message": "Request already processed",
"data": {...}
}