URI Register
⚠️ Beta Software: This library is in active development and the API may change. While it's being used in production environments, you should pin to a specific version and test thoroughly before upgrading.
A high-performance, async-first PostgreSQL-backed URI register service for assigning unique integer IDs to URIs. Perfect for string interning, deduplication, and systems that need consistent global identifier mappings.
Note: This library is async-only and requires an async runtime (tokio).
Overview
The URI Register provides a simple, fast way to assign unique integer IDs to URI strings. Once registered, a URI always returns the same ID, making it ideal for string interning and deduplication in distributed systems.
Features
- Simple API: Just 2 methods -
register_uri()andregister_uri_batch() - Async-only: Built on tokio/sqlx for high concurrency
- Batch optimised: Process thousands of URIs in a single database round-trip
- LRU caching: In-memory cache for frequently accessed URIs (configurable size)
- Order preservation: Batch operations maintain strict order correspondence
- PostgreSQL backend: Durable, scalable, with connection pooling
- Thread-safe: Designed for concurrent access from multiple threads/processes
Use Cases
- String interning systems: Reduce memory footprint by storing strings once and referencing by ID
- URL deduplication: Assign unique IDs to URLs across distributed crawlers
- Global identifier systems: Centralised ID assignment for URIs/strings in microservices
- Data warehousing: Efficient storage of repeated string values
- Distributed caching: Consistent ID assignment across cache nodes
Installation
Rust
Add to your Cargo.toml:
[]
= "0.1.2"
Or use as a git dependency:
[]
= { = "https://github.com/telicent-oss/uri-register" }
Python
Install from TestPyPI (during beta):
Requirements: Python 3.8+
Note: The package is currently published to TestPyPI for testing. Once stable, it will be available on the main PyPI repository.
Setup
1. Database Initialisation
Before using the URI Register service, you must initialise the PostgreSQL schema.
Run the schema creation script:
Or execute the SQL directly:
(
id BIGSERIAL PRIMARY KEY,
uri TEXT NOT NULL UNIQUE
);
(uri);
2. Database Configuration
The service requires a PostgreSQL connection string. Set it as an environment variable or pass it directly:
Usage
Rust Example
use ;
async
Python Example
# Connect to PostgreSQL
= await
# Register a single URI
= await
# Register the same URI again - returns the same ID
= await
assert ==
# Register multiple URIs in batch (much faster!)
=
= await
# IDs maintain order: ids[i] corresponds to uris[i]
# Get statistics
= await
API Reference
The UriService trait provides two methods:
register_uri(uri: &str) -> u64
Register a single URI and return its ID.
- If the URI exists, returns the existing ID
- If the URI is new, creates a new ID and returns it
- Uses LRU cache for fast repeated lookups
let id = register.register_uri.await?;
register_uri_batch(uris: &[String]) -> Vec<u64>
Register multiple URIs in batch and return their IDs.
- Order preserved:
ids[i]corresponds touris[i] - Much faster than calling
register_uri()multiple times - Handles duplicate URIs in input correctly
- Cache-optimised: only queries database for cache misses
let uris = vec!;
let ids = register.register_uri_batch.await?;
// Access by index
assert_eq!;
Statistics
Get information about the register:
let stats = register.stats.await?;
println!;
println!;
Performance
Logged Tables (Default)
With default logged tables on typical hardware:
- Single registration: ~500-1K URIs/sec (with cache: 100K+/sec)
- Batch registration: ~10K-50K URIs/sec
- Batch lookup (cached): ~1M+ URIs/sec (no DB round-trip)
- Batch lookup (uncached): ~100K-200K URIs/sec
Unlogged Tables (Optional)
For 2-3x faster writes at the cost of durability:
uri_register SET UNLOGGED;
Performance with unlogged tables:
- Batch registration: ~30K-150K URIs/sec
WARNING: Unlogged tables lose all data if PostgreSQL crashes. Only use this if you can rebuild the register from source data.
To revert back to logged mode:
uri_register SET LOGGED;
LRU Cache Configuration
The service includes an in-memory LRU cache that dramatically improves performance for repeated URI lookups. Configure the cache size via environment variable:
# Default: 10,000 entries
The cache provides near-instant lookups for frequently accessed URIs, completely bypassing database queries.
Performance Tips
- Always use batch operations when processing multiple URIs
- Connection pooling is configured by default (20 max connections)
- Batch size: Optimal batch size is typically 1K-10K URIs per operation
- Indexing: The URI index is essential for lookup performance
- Consider unlogged tables for initial bulk loading, then switch to logged
- Tune cache size based on your working set size and available memory
Architecture
Application
↓
UriService trait (2 methods)
↓
PostgresUriRegister impl
↓ ↓
LRU Cache Connection Pool (20 connections)
↓ ↓
└─────→ PostgreSQL Database
Schema Details
The register uses a simple two-column table:
id: BIGSERIAL primary key (auto-incrementing u64)uri: TEXT with UNIQUE constraint (indexed)
The UNIQUE constraint prevents duplicate URIs, and the index provides fast lookups.
Testing
For testing purposes, an in-memory implementation is available:
use InMemoryUriRegister;
async
Examples
Rust
use ;
async
Python
# Connect to PostgreSQL with full connection parameters
# Format: postgres://user:password@host:port/database
=
= 20 # Connection pool size
= 50_000 # LRU cache size
= await
# Register single URIs
= await
= await
# Register the same URI again - returns the same ID
= await
assert ==
# Batch registration (faster for multiple URIs)
=
= await
# Order preserved: ids[i] corresponds to uris[i]
assert == # Duplicates get same ID
# Batch registration with hashmap (automatic deduplication)
= await
# 3 unique URIs
# Get statistics
= await
Error Handling
The library uses custom error types for better error handling:
use ;
match register.register_uri.await
License
Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.