A Web2 Backend Pattern Migrated to Blockchain
An on-chain rate limiting system built with native Solana Rust SDK. This project demonstrates how traditional backend systems can be redesigned using Solana's account model and distributed architecture.
| Aspect | Web2 Backend | Solana On-Chain |
|---|---|---|
| State Storage | Redis/Memory (volatile) | Program Accounts (persistent) |
| Consistency | Single server (SPOF) | Distributed consensus |
| Trust Model | Central authority | Trustless verification |
| Latency | ~1-10ms | ~400ms (slot time) |
| Cost Model | Server costs | Transaction fees |
| Availability | Single point of failure | 99.9%+ uptime |
| Scalability | Vertical scaling | Horizontal (validators) |
Client Request → API Gateway → Redis (check counter) → Allow/Deny
↓
Increment counter
Set TTL for window
Problems:
- Single point of failure (Redis down = service down)
- Trust required in infrastructure provider
- Cannot verify rate limit decisions off-chain
Client Request → Transaction → Solana Program → Check Account State
↓
Update Account
Return Success/Error
Advantages:
- No single point of failure
- Verifiable on-chain
- Works across multiple services
- No need for separate infrastructure
solana-rate-limiter/
├── Cargo.toml # Project configuration
├── src/
│ └── lib.rs # Main program (rate limiter logic)
├── client/
│ └── cli.ts # CLI client (TypeScript)
├── tests/
│ └── integration.ts # Integration tests
└── README.md # This file
- Solana CLI 2.0+
- Rust 1.70+
- Node.js 18+ (for client)
cargo build-bpfsolana program deploy ./target/deploy/solana_rate_limiter.so# Create a rate limit: 100 requests per 60 seconds
solana program call <PROGRAM_ID> init_config 100 60# Check if client can make request (client_id = sha256("user@example.com"))
solana program call <PROGRAM_ID> check_rate_limit <CLIENT_ID>- API Rate Limiting - Protect public APIs from abuse
- DApp Throttling - Prevent spam in decentralized applications
- Fair Resource Access - Ensure equitable access to shared resources
- Anti-Bot Protection - Rate limit suspicious clients
✅ Cross-service rate limiting needed
✅ Verifiable rate limit decisions required
✅ Anti-censorship requirements
✅ No infrastructure to maintain
❌ Sub-second latency required
❌ Very high throughput needed
❌ Cost-sensitive applications
❌ Simple single-service use case
max_requests: Maximum requests allowed in windowwindow_seconds: Time window durationis_active: Enable/disable rate limiting
client_id: 32-byte identifier (hash of IP/API key/user)request_count: Current count in windowwindow_start: Unix timestamp of window start
| Operation | Compute Units | Fee |
|---|---|---|
| Init Config | ~50,000 | ~0.000005 SOL |
| Check Rate Limit (new) | ~100,000 | ~0.00001 SOL |
| Check Rate Limit (existing) | ~30,000 | ~0.000003 SOL |
# Unit tests
cargo test
# Integration tests (requires local validator)
npm testMIT
PRs welcome! This is an educational project demonstrating Web2 → Solana migration patterns.