Build a Chat Backend That Holds Up
Design and build a chat backend with auth, storage, streaming, quotas and cost tracking that survives real use.
A taste of a lesson
Sometimes users get two identical replies after a network hiccup. How do I prevent that?
The client is retrying a request your server already processed. Fix it with an idempotency key: the frontend generates a unique id per message and sends it with the post. Your server stores it with the message, and if the same key arrives again, it returns the existing message and reply instead of creating new ones. Add a unique constraint on conversation id plus key so even simultaneous retries cannot both insert. What should the server return if the first attempt is still generating?
Written with AI under Skill Lab's direction as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong. Right now the AI model is not connected: replies come from a built in test engine on this site.
What you will be able to do
- Design endpoints and tables for conversations and messages
- Wrap model access with timeouts, retries, logging and cost tracking
- Prevent duplicate and interleaved messages with idempotency and locking
- Protect budget and limits with per user quotas and input checks
- Track latency and cost per user and feature from usage data
Lesson plan
- 1 Endpoints and data model Sketch the API surface and tables for a chat feature. Start
- 2 One wrapper for model access Centralise prompts, retries, timeouts and logging in a single module. Start
- 3 The message flow with streaming Implement posting a message end to end, including partial failures. Start
- 4 Duplicates and concurrency Handle client retries and simultaneous requests safely. Start
- 5 Quotas, abuse and context budgets Protect your budget and keep requests within limits. Start
- 6 Observability and data care Measure cost and latency and handle stored conversations responsibly. Start
Try asking
About this tutor
For developers who can call a model API and now need a proper backend for a chat feature used by real people. You design endpoints for conversations and messages, store history safely per user, wrap the model client in one place, relay streamed replies, trim context, and add the protections a demo lacks: authentication, per user quotas, idempotent message posting, handling two requests on one conversation, timeouts and clear errors. You also track cost and latency per user and feature. Examples stay framework neutral, so the design fits whatever web stack you use.
Reviews
Students can review a tutor after a paid lesson. Nobody has yet.
More like this
Other tutors on the same or nearby topics.