anchor.diff:  
   init(_) ->
       process_flag(trap_exit, true),
  -    ?DOC_ID_TO_REP_ID = ets:new(?DOC_ID_TO_REP_ID, [named_table, set, private]),
  -    ?REP_ID_TO_DOC_ID = ets:new(?REP_ID_TO_DOC_ID, [named_table, set, private]),
  +    _ = ets:new(?DOC_ID_TO_REP_ID, [named_table, set, protected]),
  +    _ = ets:new(?REP_ID_TO_DOC_ID, [named_table, set, private]),
       Server = self(),
       ok = couch_config:register(
           fun("replicator", "db", NewName) ->
  -            ok = gen_server:cast(Server, {rep_db_changed, ?l2b(NewName)})
  +            ok = gen_server:cast(Server, {rep_db_changed, ?l2b(NewName)});
  +        ("replicator", "max_replication_retry_count", NewMaxRetries1) ->
  +            NewMaxRetries = list_to_integer(NewMaxRetries1),
  +            ok = gen_server:cast(Server, {set_max_retries, NewMaxRetries})
           end
       ),
       {Loop, RepDbName} = changes_feed_loop(),
       {ok, #state{
           changes_feed_loop = Loop,
           rep_db_name = RepDbName,
  -        db_notifier = db_update_notifier()}
  -    }.
  +        db_notifier = db_update_notifier(),
  +        max_retries = list_to_integer(
  +            couch_config:get("replicator", "max_replication_retry_count", "10"))
  +    }}.
   
   
   handle_call({rep_db_update, Change}, _From, State) ->
       {reply, ok, process_update(State, Change)};
   
   handle_call({triggered, {BaseId, _}}, _From, State) ->
  -    case ets:lookup(?REP_ID_TO_DOC_ID, BaseId) of
  -    [{BaseId, {DocId, true}}] ->
  -        true = ets:insert(?REP_ID_TO_DOC_ID, {BaseId, {DocId, false}});
  -    _ ->
  -        ok
  -    end,
  +    [{BaseId, {DocId, true}}] = ets:lookup(?REP_ID_TO_DOC_ID, BaseId),
  +    true = ets:insert(?REP_ID_TO_DOC_ID, {BaseId, {DocId, false}}),
       {reply, ok, State};
   
   handle_call({restart_failure, {Props} = RepDoc, Error}, _From, State) ->
       DocId = get_value(<<"_id">>, Props),
  -    [{DocId, {BaseId, _} = RepId}] = ets:lookup(?DOC_ID_TO_REP_ID, DocId),
  +    [{DocId, {{BaseId, _} = RepId, MaxRetries}}] = ets:lookup(
  +        ?DOC_ID_TO_REP_ID, DocId),
       ?LOG_ERROR("Failed to start replication `~s` after ~p attempts using "
           "the document `~s`. Last error reason was: ~p",
  -        [pp_rep_id(RepId), ?MAX_RETRIES, DocId, Error]),
  +        [pp_rep_id(RepId), MaxRetries, DocId, Error]),
       couch_rep:update_rep_doc(
           RepDoc,
           [{<<"_replication_state">>, <<"error">>},
  
positive.code: 
  -define(DOC_ID_TO_REP_ID, rep_doc_id_to_rep_id).
  -define(REP_ID_TO_DOC_ID, rep_id_to_rep_doc_id).
  -define(MAX_RETRIES, 10).
  -define(INITIAL_WAIT, 5).
  
  -record(state, {
      changes_feed_loop = nil,
      db_notifier = nil,
      rep_db_name = nil,
      rep_start_pids = []
  }).
  
  -import(couch_util, [
  
negative.code: 
  handle_cast({rep_db_created, _NewName}, State) ->
      {noreply, restart(State)};
  
  handle_cast({set_max_retries, MaxRetries}, State) ->
      {noreply, State#state{max_retries = MaxRetries}};
  
  handle_cast(Msg, State) ->
      ?LOG_ERROR("Replicator DB listener received unexpected cast ~p", [Msg]),
  