Device registry creates duplicate entries when syncing from multiple gateways

Our device registry is creating duplicate entries when the same device is discovered and registered by multiple edge gateways simultaneously. We have 20 gateways in a mesh configuration, and devices can connect to multiple gateways for redundancy.

The issue occurs when a device comes online and is visible to 2-3 gateways at the same time. Each gateway attempts to register the device via REST API, and we end up with 2-3 registry entries for the same physical device. This breaks our device management and causes billing inconsistencies.

{
  "error": "Duplicate device entries detected",
  "device_mac": "00:1A:2B:3C:4D:5E",
  "registry_ids": ["dev-12345", "dev-12387", "dev-12401"],
  "registered_by": ["gateway-03", "gateway-07", "gateway-11"]
}

I need to implement distributed locking or idempotent API design to prevent concurrent registrations. What’s the best approach for handling gateway sync coordination with the Cisco IoT Device Registry? Should I implement database constraints or application-level deduplication?

Idempotent API design is critical here. Use PUT instead of POST for device registration, with the MAC address as part of the URL path (PUT /devices/{mac_address}). This makes registration naturally idempotent - multiple identical requests result in the same outcome. The API should return 200 OK with the existing device ID if already registered, 201 Created for new registrations.

Database constraints are your first line of defense. Add a unique constraint on the device MAC address field. This prevents duplicates at the storage layer regardless of application logic. However, you’ll also need to handle the constraint violation gracefully and return the existing device ID to the gateway.

Implement optimistic locking with retry logic. When a unique constraint violation occurs, catch the exception, query for the existing device by MAC address, and return that record. The gateway sees a successful response either way. For high-concurrency scenarios, add distributed locking using Redis with a short TTL (5-10 seconds) to serialize registration attempts for the same device.

Consider using eventual consistency for device registry. Allow duplicate registrations initially but run a background reconciliation job that merges duplicates based on MAC address. This avoids blocking gateway operations but requires robust merge logic. Tag each registry entry with the registering gateway ID so you can track which gateways have seen the device.