I’m building a survey service using Next.js 15 with App Router. One of the features is auto-saving form questions—similar to how Google Forms works.
Right now, I’m considering how to implement this auto-save functionality. Since it’s user-triggered and runs on the client side, I can’t directly use Prisma from the client. So I see two main options:
- API route handler – creating a traditional REST API endpoint (e.g. POST /api/questions/[id]) and calling it from the client.
- Server Actions – using a server function and calling it from a client component.
I’m leaning toward Server Actions, but I have concerns:
- Since the input is being saved on every keystroke, I’m worried about race conditions, where a slower request might override a more recent one.
- Debouncing helps reduce request count, but doesn’t completely prevent race conditions.
- I’m wondering if there’s a recommended pattern or best practice to avoid race conditions when using Server Actions for auto-saving.
My questions:
- Is it okay to use Server Actions for this kind of real-time update use case?
- How should I manage race conditions in Server Actions, especially when updates are frequent and time-sensitive?
- Would it be better to use API routes instead?