'Write events to the database
Args:
events_and_contexts: list of tuples of (event, context)
backfilled: ?'

def persist_events(self, events_and_contexts, backfilled=False):

    partitioned = {}
    for (event, ctx) in events_and_contexts:
        partitioned.setdefault(event.room_id, []).append((event, ctx))
    deferreds = []
    for (room_id, evs_ctxs) in partitioned.iteritems():
        d = preserve_fn(self._event_persist_queue.add_to_queue)(room_id, evs_ctxs, backfilled=backfilled)
        deferreds.append(d)
    for room_id in partitioned:
        self._maybe_start_persisting(room_id)
    return preserve_context_over_deferred(defer.gatherResults(deferreds, consumeErrors=True))
