Preventing Duplicate Attendee Check-Ins Using Database Constraints
Imagine two registration staff members scanning the same attendee's credential almost simultaneously.
If the application only checks whether an existing attendance record is present before inserting a new one, both requests might conclude that the attendee has not checked in.
The result could be two attendance records for one participant.
A database constraint can help prevent this race condition.
Step 1: Define what counts as a unique check-in
For this example, we want to record one initial check-in per attendee per event.
Repeated venue entries and individual checkpoint scans belong in a separate activity log.
Step 2: Create a PostgreSQL table
CREATE TABLE event_check_ins (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event_id TEXT NOT NULL,
attendee_id TEXT NOT NULL,
checked_in_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (event_id, attendee_id)
);
The combined uniqueness constraint prevents multiple records with the same event and attendee identifiers.
Step 3: Use an idempotent insert
Instead of performing a separate existence check followed by an insert, use PostgreSQL's ON CONFLICT clause.
INSERT INTO event_check_ins (
event_id,
attendee_id
)
VALUES ('event-001', 'attendee-101')
ON CONFLICT (event_id, attendee_id)
DO NOTHING
RETURNING id;
The first successful request creates a record.
Subsequent requests using the same identifiers do not create additional records.
Step 4: Connect the database operation to JavaScript
async function checkInAttendee(db, eventId, attendeeId) {
const result = await db.query(
`INSERT INTO event_check_ins
(event_id, attendee_id)
VALUES ($1, $2)
ON CONFLICT (event_id, attendee_id)
DO NOTHING
RETURNING id`,
[eventId, attendeeId]
);
if (result.rows.length === 0) {
return { status: "already_checked_in" };
}
return {
status: "checked_in",
checkInId: result.rows[0].id
};
}
This approach allows the application to distinguish between a newly created check-in and an attendee who has already checked in.
What about repeated venue access?
Some events allow attendees to enter and leave the venue multiple times.
The unique check-in record should not prevent legitimate repeat access.
Maintain a separate activity table for individual entry and exit records. This allows the reporting system to distinguish unique attendance from total checkpoint activity.
Production considerations
A production implementation should also include:
- Server-side attendee and credential verification.
- Authentication and authorization.
- Appropriate foreign key relationships.
- Audit records and error handling.
- Event-specific attendance rules.
- Controlled synchronization for delayed records.
This example assumes that attendee identity has already been verified before the function is called.
Applying the concept to event reporting
Accurate attendance measurement is an important part of conference operations.
For business context, StampIQ provides information about real-time event analytics, including registration and check-in reporting.
For further technical reading, see the PostgreSQL INSERT documentation.
Database constraints are not a complete event management architecture, but they are a useful foundation for protecting the integrity of unique attendance records.
This educational example is contributed by the StampIQ team. It is not a representation of StampIQ's production source code.
Top comments (1)
the check-then-insert is the race. two scans both see nothing, both write, and the constraint is the only thing that says no.